一、PagerAdapter的方法

1.getCount() 获得viewpager中有多少个view

2.instantiateItem()

  1. 1)将给定位置的view添加到ViewGroup(容器)中,创建并显示出来<br /> 2)返回一个代表新增页面的Object(key),通常都是直接返回view本身就可以了,当然你也可以自定义自己的key,但是key和每个view要一一对应的关系。

3.isViewFromObject() 判断instantiateItem(ViewGroup,int)函数返回来的Key与一个页面视图是否是代表的同一个视图(即它俩是否对应的,对应的表示同一个View),通常我们直接写return view==object

4.destroyItem() 移除一个给定位置的页面。适配器有责任从容器中删除这个视图。这是为了确保在finishUpdate(viewGroup)返回时视图能够被移除。而另外两个方法则是涉及到一个key的东东

二、实现步骤

1.创建布局主页面和滑动子页面,绑定控件

image.png

2.创建适配器

image.png

3.渲染布局,添加集合,将页面放进集合,创建适配器,将适配器放入viewpager控件

image.png

4.添加方法,将页面放入容器中,判断,然后将当前不用的销毁。

image.png

三、整体代码

1.布局activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <androidx.viewpager.widget.ViewPager
  8. android:id="@+id/vp_item"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. />
  12. </LinearLayout>

2.布局layout_one.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:gravity="center"
  8. android:orientation="vertical">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="测试1"
  13. android:textColor="#287348"
  14. android:textSize="40sp" />
  15. </LinearLayout>

3.布局layout_two.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:gravity="center"
  8. android:orientation="vertical">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="测试2"
  13. android:textColor="#CC972C"
  14. android:textSize="40sp" />
  15. </LinearLayout>

4.布局layout_two.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:gravity="center"
  8. android:orientation="vertical">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="测试3"
  13. android:textColor="#8A2C80"
  14. android:textSize="40sp" />
  15. </LinearLayout>

5.MainActivity文件代码

  1. package com.example.myviewpager;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.viewpager.widget.ViewPager;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import com.example.myviewpager.adapter.MyAdapter;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class MainActivity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. //4.渲染布局,创建三个页面
  16. LayoutInflater layoutInflater = getLayoutInflater().from(this);
  17. View view1 = layoutInflater.inflate(R.layout.layout_one, null);
  18. View view2 = layoutInflater.inflate(R.layout.layout_two, null);
  19. View view3 = layoutInflater.inflate(R.layout.layout_three, null);
  20. //5.添加数据的集合,将三个页面添加到集合内
  21. List<View> viewList=new ArrayList<>();
  22. viewList.add(view1);
  23. viewList.add(view2);
  24. viewList.add(view3);
  25. //1.绑定viewpager控件
  26. ViewPager viewPager = findViewById(R.id.vp_item);
  27. //6.创建适配器,将适配器放进viewpager
  28. MyAdapter myAdapter = new MyAdapter(viewList);
  29. viewPager.setAdapter(myAdapter);
  30. }
  31. }

6.MyAdapter文件代码

  1. package com.example.myviewpager.adapter;
  2. import android.view.View;
  3. import android.view.ViewGroup;
  4. import androidx.annotation.NonNull;
  5. import androidx.viewpager.widget.PagerAdapter;
  6. import java.util.List;
  7. public class MyAdapter extends PagerAdapter {
  8. //2.定义集合list,创建构造方法,将三个页面的布局放入
  9. private List<View> list;
  10. public MyAdapter(List<View> list) {
  11. this.list = list;
  12. }
  13. //7.添加instantiateItem方法,将view添加到viewGroup(容器)中
  14. @NonNull
  15. @Override
  16. public Object instantiateItem(@NonNull ViewGroup container, int position) {
  17. container.addView(list.get(position),0);
  18. return list.get(position);
  19. }
  20. //3.返回页面的数量
  21. @Override
  22. public int getCount() {
  23. return list.size();
  24. }
  25. //8.判断页面是否相等
  26. @Override
  27. public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
  28. return view==object;
  29. }
  30. //9.添加destroyItem方法,当前不用的,通过这个方法进行销毁
  31. @Override
  32. public void destroyItem(@NonNull ViewGroup container,
  33. int position, @NonNull Object object) {
  34. container.removeView(list.get(position));
  35. }
  36. }

7.效果图:

image.png
image.png
image.png