23. ViewPager
页面滑动效果
23.1 页面布局
layout1.xml
<?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:background="#ff00ffff"
android:orientation="vertical"
>
<TextView
android:textSize="30sp"
android:text="layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
layout2.xml
<?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:background="#ff00ff00"
android:orientation="vertical"
>
<TextView
android:textSize="30sp"
android:text="layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
layout3.xml
<?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:background="#ffffff00"
android:orientation="vertical"
>
<TextView
android:textSize="30sp"
android:text="layout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
23.2 创建ViewPager
activity_main.xml
<?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"
>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"
/>
</LinearLayout>
23.3 创建适配器Adapter
package com.dingjiaxiong.myviewpager;
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<View> mListView;
public MyAdapter(List<View> mListView) {
this.mListView = mListView;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) { //将给定位置的view添加到viewgroup容器中,并显示出来;返回一个key值,与view一一对应
container.addView(mListView.get(position),0);
return mListView.get(position);
}
@Override
public int getCount() { //获得ViewPager中有多少个View
return mListView.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { //判断key与view是否相等对应
return view == object;
}
//销毁
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(mListView.get(position));
}
}
23.4 设置视图数据
MainActivity.java
package com.dingjiaxiong.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 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<View> viewList = new ArrayList<>();
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
ViewPager viewPager = findViewById(R.id.vp);
MyAdapter myAdapter = new MyAdapter(viewList);
viewPager.setAdapter(myAdapter);
}
}
运行