亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)之ViewPager實(shí)現(xiàn)滑動切換頁面

 更新時間:2022年09月19日 11:54:51   作者:ShadyPi  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之ViewPager實(shí)現(xiàn)滑動切換頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android開發(fā)之ViewPager實(shí)現(xiàn)滑動切換頁面的具體代碼,供大家參考,具體內(nèi)容如下

基本構(gòu)件

activity_main.xml

依然是在簡單的線性布局里放個ViewPager組件,注意該組件是在androidx中的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_height="match_parent"
? ? android:layout_width="match_parent"
? ? android:orientation="vertical">

? ? <androidx.viewpager.widget.ViewPager
? ? ? ? android:id="@+id/viewpager"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"/>


</LinearLayout>

次級頁面的布局資源

這次我們的ViewPager要實(shí)現(xiàn)三個頁面間的滑動切換,所以要預(yù)先設(shè)置好三個頁面的布局,這里就用非常簡單的帶背景色的TextView作為布局。

layout1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
? ? xmlns:android1="http://schemas.android.com/apk/res/android"
? ? android1:layout_height="match_parent"
? ? android1:layout_width="match_parent"
? ? android1:background="@color/purple_200"
? ? android1:orientation="vertical">

? ? <TextView
? ? ? ? android1:textSize="30sp"
? ? ? ? android1:text="layout1"
? ? ? ? android1:layout_width="wrap_content"
? ? ? ? android1:layout_height="wrap_content"/>


</LinearLayout>

layout2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
? ? xmlns:android1="http://schemas.android.com/apk/res/android"
? ? android1:layout_height="match_parent"
? ? android1:layout_width="match_parent"
? ? android1:background="@color/purple_500"
? ? android1:orientation="vertical">

? ? <TextView
? ? ? ? android1:textSize="30sp"
? ? ? ? android1:text="layout2"
? ? ? ? android1:layout_width="wrap_content"
? ? ? ? android1:layout_height="wrap_content"/>


</LinearLayout>

layout3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
? ? xmlns:android1="http://schemas.android.com/apk/res/android"
? ? android1:layout_height="match_parent"
? ? android1:layout_width="match_parent"
? ? android1:background="@color/purple_700"
? ? android1:orientation="vertical">

? ? <TextView
? ? ? ? android1:textSize="30sp"
? ? ? ? android1:text="layout3"
? ? ? ? android1:layout_width="wrap_content"
? ? ? ? android1:layout_height="wrap_content"/>


</LinearLayout>

MainActivity.java

跟ListView類似,我們把三個頁面從xml中渲染出來,成為java代碼中的一個View類型變量,然后將其裝入一個List中,最終通過adapter裝填進(jìn)viewpager里。

package com.example.myviewpager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? LayoutInflater inflater = getLayoutInflater().from(this);
? ? ? ? View view1 = inflater.inflate(R.layout.layout1,null);
? ? ? ? View view2 = inflater.inflate(R.layout.layout2,null);
? ? ? ? View view3 = inflater.inflate(R.layout.layout3,null);

? ? ? ? List<View> viewList = new ArrayList<>();
? ? ? ? viewList.add(view1);
? ? ? ? viewList.add(view2);
? ? ? ? viewList.add(view3);

? ? ? ? ViewPager viewpager = findViewById(R.id.viewpager);
? ? ? ? MyAdapter myadapter = new MyAdapter(viewList);
? ? ? ? viewpager.setAdapter(myadapter);

? ? }
}

adapter

ViewPager的adapter類繼承自PagerAdapter。

首先,需要有一個構(gòu)造器,使得主函數(shù)中的數(shù)據(jù)能夠傳進(jìn)adapter:

private List<View> listview;

? ? public MyAdapter(List<View> listview) {
? ? ? ? this.listview = listview;
? ? }

在MyAdapter中,需要實(shí)現(xiàn)四個方法:
getCount()
獲得viewpagert中有多少個view,這個很簡單,返回列表大小就行了。

@Override
?public int getCount() {
? ? ? ? return listview.size();
? ? }

instantiateItem()
1.將給定位置的view添加到ViewGroup(容器)中,創(chuàng)建并顯示出來。
2.返回一個代表新增頁面的Object(key),通常都是直接返回view本身就可以了,當(dāng)然也可以自定義自己的key,但是key和每個view要一一對應(yīng)的關(guān)系。

在寫instantiateItem()時,可以用快捷鍵Alt+Insert選擇Override Methods,在彈出的界面中輸入字母就可以查找對應(yīng)的函數(shù),選中即可生成一個框架(博主這里是因?yàn)橐呀?jīng)實(shí)現(xiàn)了該方法,所以被劃掉了):

@NonNull
@Override
? ? public Object instantiateItem(@NonNull ViewGroup container, int position) {
? ? ? ? container.addView(listview.get(position),0);
? ? ? ? return listview.get(position);
? ? }

isViewFromObject()

判斷instantiateltem(ViewGroup,int)函數(shù)所返回來的Key與一個頁面視圖是否是代表的同一個視圖(即它倆是否是對應(yīng)的,對應(yīng)的表示同一個view),通常我們直接寫return view==object。

@Override
?public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
? ? ? ? return view == object;
? ? }

destroyltem()

移除一個給定位置的頁面。適配器有責(zé)任從容器中冊刪除這個視圖。這是為了確保在finishUpdate(viewGroup)返回時視圖能夠被移除。

@Override
?public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
? ? ? ? container.removeView(listview.get(position));
? ? }

實(shí)現(xiàn)效果

可以快樂地在三個layout間劃來劃去(動態(tài)效果不太好展示,各位自己實(shí)現(xiàn)出來觀感更佳)~

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論