与Viewpager不同,ViewPager2是对RecycleView的封装
由于采用ViewPager2实现,所以自带懒加载
添加依赖
ViewPager2需要先添加依赖
1
| implementation 'androidx.viewpager2:viewpager2:1.0.0'
|
修改布局
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.viewpager2.widget.ViewPager2 android:layout_width="match_parent" android:id="@+id/viewPager2" android:layout_height="match_parent"/> </LinearLayout>
|
创建item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:gravity="center" android:id="@+id/item_linearLayout" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="文本" android:textSize="28sp" android:textColor="@color/black" android:textStyle="bold" android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
|
创建适配器
ViewPager2的适配器与RecyclerView相同,继承自RecyclerView.Adapter,并重写Adapter中的三个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MyAdapter extends RecyclerView.Adapter{
@NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return null; }
@Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override public int getItemCount() { return 0; } }
|
Adapter是泛型,需要给它一个继承自ViewHolder的类,然后把其他方法中的RecyclerView.ViewHolder修改为MyViewHolder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class MyAdapter extends RecyclerView.Adapter<MyTestAdapter.MyViewHolder>{
@NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return null; }
@Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
}
@Override public int getItemCount() { return 0; }
class MyViewHolder extends RecyclerView.ViewHolder { public MyViewHolder(@NonNull View itemView) { super(itemView); } } }
|
定义变量、创建构造器、绑定事件(参考RecyclerView)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private ArrayList<Integer> integerArrayList; private ArrayList<String> stringArrayList; public MyAdapter(ArrayList<String> stringArrayList,ArrayList<Integer> integerArrayList){ this.stringArrayList = stringArrayList; this.integerArrayList = integerArrayList; }
@NonNull @Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; }
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.item_text.setText(stringArrayList.get(position)); holder.item_linearLayout.setBackgroundResource(integerArrayList.get(position)); }
@Override
public int getItemCount() { return stringArrayList.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder { private LinearLayout item_linearLayout; private TextView item_text; public MyViewHolder(@NonNull View itemView) { super(itemView); item_text = itemView.findViewById(R.id.item_text); item_linearLayout = itemView.findViewById(R.id.item_linearLayout); } } }
|
调用适配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import androidx.appcompat.app.AppCompatActivity; import androidx.viewpager2.widget.ViewPager2; import android.os.Bundle; import java.util.ArrayList;
public class MainActivity extends AppCompatActivity{ private ViewPager2 viewPager2; private ArrayList<Integer> integerArrayList; private ArrayList<String> stringArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
stringArrayList = new ArrayList<>(); integerArrayList = new ArrayList<>(); stringArrayList.add("页面1"); stringArrayList.add("页面2"); stringArrayList.add("页面3"); integerArrayList.add(R.color.antiquewhite); integerArrayList.add(R.color.darkorange); integerArrayList.add(R.color.lime);
viewPager2 = findViewById(R.id.viewPager2); MyAdapter myAdapter = new MyAdapter(stringArrayList,integerArrayList); viewPager2.setAdapter(myAdapter); } }
|
效果