getCount()
获得viewpager中有多少个view
instantiateltem()
1.将给定位置的view添加到viewGroup(容器)中,创建并显示出来
2.返回一个代表新增页面的Object(key)通常都是直接返回view本身就可以了,当然你也可以自定义自己的key,但是key和每个view要一一对应的关系
isViewFromObject()
判断instantiateltem(ViewGroup,int)函数所返回来的Key与一个页面视图是否是代表的同一个视图(即它俩是否是对应的,对应的表示同一个View)通常我们直接写 return view == object
destroyltem()
移除一个给定位置的页面。适配器有责任从容器中删除这个视图。这是为了确保在
finishUpdate(viewGroup)返回时视图能够被移除。
而另外两个方法则是涉及到一个key的东东:
页面切换效果
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/Vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
layout1,2,3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="这是第一页"//二,三
android:background="#FF00"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
main java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater lf= getLayoutInflater().from(this);
View v1=lf.inflate(R.layout.layout1,null);
View v2=lf.inflate(R.layout.layout2,null);
View v3=lf.inflate(R.layout.layout3,null);
List<View> mylistview=new ArrayList<>();//创建view集合,将v1,v2,v3放进去
mylistview.add(v1);
mylistview.add(v2);
mylistview.add(v3);
ViewPager vp= findViewById(R.id.Vp);
MyAdapter myAdapter=new MyAdapter(mylistview);
vp.setAdapter(myAdapter);
}
}
MyAdapter.java
package com.example.myapplication;
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 {//继承自PagerAdapter
private List<View> mylistview;//用于存放数据
public MyAdapter(List<View> mylistview) {
this.mylistview = mylistview;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(mylistview.get(position),0);
return mylistview.get(position);
}
@Override
public int getCount() {//得到View的数量
return mylistview.size();//0改为mylistview.size()
}
@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(mylistview.get(position));
}
}