1. 效果
1. 添加ViewPager2控件
在activity_main.xml中添加 androidx.viewpager2.widget.ViewPager2 控件, 有些版本可能提示无此控件,需要在 build.gradle 中添加依赖:implementation 'androidx.viewpager2:viewpager2:1.0.0'
添加ViewPager2控件后,activity_main.xml 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. MainActivity.java中添加ViewPager2
public class MainActivity extends AppCompatActivity {
private ViewPager2 mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.viewpager);
List<String> dataList = new ArrayList<>();
dataList.add("你");
dataList.add("好");
dataList.add("啊");
dataList.add("你");
dataList.add("是");
dataList.add("谁");
dataList.add("啊");
mViewPager.setAdapter(new ViewPagerAdapter(dataList));
}
}
写mViewPager.setAdapter(new ViewPagerAdapter(dataList)); 这行代码时,先写下来,然后用 alt + enter 快捷键提示添加 ViewPagerAdapter 类。 创建完 ViewPagerAdapter 类后,先放着,创建ViewPager中的页面
3. 创建ViewPager中的页面
在 Layout 右键,new -> Layout Resource File,创建一个xml,名字随意 item_pager.xml 里面的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itme_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30sp" />
</LinearLayout>
4. 添加ViewPagerAdapter类代码
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerHolder> {
private List<String> mDataList;
private List<Integer> mItemColorList;
public ViewPagerAdapter(List<String> data){
mDataList = data;
mItemColorList = new ArrayList<>();
mItemColorList.add(Color.parseColor("#feeeed"));
mItemColorList.add(Color.parseColor("#fedcbd"));
mItemColorList.add(Color.parseColor("#817936"));
mItemColorList.add(Color.parseColor("#b2d235"));
mItemColorList.add(Color.parseColor("#64492b"));
mItemColorList.add(Color.parseColor("#817936"));
mItemColorList.add(Color.parseColor("#fffffb"));
}
@NonNull
@Override
public ViewPagerAdapter.ViewPagerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewPagerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewPagerHolder holder, int position) {
holder.mItemTv.setText(mDataList.get(position));
holder.mItemContainer.setBackgroundColor(mItemColorList.get(position));
}
@Override
public int getItemCount() {
return mDataList.size();
}
class ViewPagerHolder extends RecyclerView.ViewHolder{
LinearLayout mItemContainer;
TextView mItemTv;
public ViewPagerHolder(@NonNull View itemView) {
super(itemView);
mItemContainer = itemView.findViewById(R.id.itme_container);
mItemTv = itemView.findViewById(R.id.item_tv);
}
}
}
说明:
5. 示例代码
ViewPager2示例代码
6. 参考
更多详细ViewPager2的例子,可以参考Android官方示例:Android ViewPager2 官方示例
|