前言
ViewPager2是androidx.viewpager2.widget包下面的,用来代替androidx.viewpager.widget.ViewPager,而androidx是用来代替原来的support库的 为了紧跟时代发展,很有必要学习ViewPager2,ViewPager2是由RecyclerView加上LinearLayoutManager并继承自ViewGroup而来
参考文章 https://www.jianshu.com/p/bd70970600aa https://blog.csdn.net/xiangshiweiyu_hd/article/details/104005810 https://blog.csdn.net/diyangxia/article/details/94458274
一、ViewPager2的基本使用
1.1、导入依赖
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0' // ViewPager 2 需要使用 RecycleView 的 adapter
1.2、activity_main.xml
<?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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
android:background="@color/colorAccent">
</androidx.viewpager2.widget.ViewPager2>
</LinearLayout>
1.3、ViewPagerAdapter.java
ViewPager2的适配器 注意:这里继承的是RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder> 因为ViewPager2的底层实现是RecyclerView
package com.example.viewpagerandfragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder> {
private List<String> titles = new ArrayList<>();
private List<Integer> colors = new ArrayList<>();
public ViewPagerAdapter(){
titles.add("zzuli");
titles.add("移动软件");
titles.add("516");
titles.add("study");
titles.add("I");
titles.add("like");
titles.add("you");
titles.add("my");
titles.add("best");
titles.add("wish");
colors.add(R.color.white);
colors.add(R.color.black);
colors.add(R.color.red);
colors.add(R.color.colorAccent);
colors.add(R.color.colorPrimary);
colors.add(R.color.white);
colors.add(R.color.black);
colors.add(R.color.red);
colors.add(R.color.colorPrimary);
colors.add(R.color.white);
}
@NonNull
@Override
public ViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewPagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewPagerViewHolder holder, int position) {
holder.mTv.setText(titles.get(position));
holder.mContainer.setBackgroundResource(colors.get(position));
}
@Override
public int getItemCount() {
return 10;
}
class ViewPagerViewHolder extends RecyclerView.ViewHolder{
TextView mTv;
RelativeLayout mContainer;
public ViewPagerViewHolder(@NonNull View itemView) {
super(itemView);
mContainer = itemView.findViewById(R.id.container);
mTv = itemView.findViewById(R.id.tvTitle);
}
}
}
1.4、单页面布局
item_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTitle"
android:layout_centerInParent="true"
android:textColor="#ff4532"
android:textSize="32dp"
android:text="hello"></TextView>
</RelativeLayout>
1.5、MainActivity.java
package com.example.viewpagerandfragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager2 viewPager = findViewById(R.id.viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
}
}
二、测试
|