3.ViewPager

ViewPager是什么东西

主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff"
    tools:context=".MainActivity">

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

layout1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff"
    tools:context=".MainActivity">

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

LinearLayout>

 

layout2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0f0f"
    tools:context=".MainActivity">

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

LinearLayout>

 

layout3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000ff"
    tools:context=".MainActivity">

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

LinearLayout>

适配器 MyAdaper.class

package com.example.viewpage;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class MyAdapter extends PagerAdapter {

    private List myList;

    //建一个构造方法,用于放数据
    public MyAdapter(List myList) {
        this.myList = myList;
    }

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

    //instantiate实例化的意思
    //在容器中放页面,然后返回
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(myList.get(position),0);
        return myList.get(position);
    }

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

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

MainACtivity.claspackage com.example.viewpage;

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是一个用于将xml布局文件加载为View或者ViewGroup对象的工具,我们可以称之为布局加载器
LayoutInflater lf = getLayoutInflater().from(this); View view1 = lf.inflate(R.layout.layout1,null); View view2 = lf.inflate(R.layout.layout2,null); View view3 = lf.inflate(R.layout.layout3,null); List myList = new ArrayList<>(); myList.add(view1); myList.add(view2); myList.add(view3); ViewPager viewPager = findViewById(R.id.myViewPage); MyAdapter myAdapter = new MyAdapter(myList); viewPager.setAdapter(myAdapter); } }

有关函数解析:

ViewPager里面对每个页面的管理是key-value形式的,也就是说每个page都有个对应的id(id是object类型),需要对page操作的时候都是通过id来完成的

首先看这个函数
public Object instantiateItem(ViewGroup container, int position);
这是pageAdapter里的函数,功能就是往PageView里添加自己需要的page。同时注意它还有个返回值object,这就是那个id。

最后
public abstract boolean isViewFromObject (View view, Object object)
这个函数就是用来告诉框架,这个view的id是不是这个object。
谷歌官方推荐把view当id用,所以常规的instantiateItem()函数的返回值是你自己定义的view,而isViewFromObject()的返回值是view == object。

ps:感觉这个机制应该是历史遗留问题,属于改bug改出来的机制。否则官方不会推荐这种把view当id的做法。

posted @ 2021-09-06 15:39  涂妖教  阅读(36)  评论(0)    收藏  举报