目录
学习ViewPager2之前,建议先学习使用RecycleView
ViewPager2属于新增的配件,且底层是用RecycleViews实现的,需要在项目的中添加相应的依赖.
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
最简单的水平翻页
1.主布局
<?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"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager"/>
</LinearLayout>
2.对ViewPager进行定制,创建一个类和布局
package com.example.view_pager2;
public class ViewPager {
private String name;
private int image;
public ViewPager(String name, int image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}
}
<?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:id="@+id/vp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:textSize="40dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image"
/>
</LinearLayout>
3.适配器
package com.example.view_pager2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class HorizontalVpAdapter extends RecyclerView.Adapter<HorizontalVpAdapter.HorizontalVpViewHolder> {
private List<ViewPager> viewpagerList;
public HorizontalVpAdapter(List<ViewPager> viewpagerList) {
this.viewpagerList = viewpagerList;
}
@NonNull
@Override
public HorizontalVpAdapter.HorizontalVpViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewpager_item,parent,false);
return new HorizontalVpViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HorizontalVpAdapter.HorizontalVpViewHolder holder, int position) {
ViewPager viewpager = viewpagerList.get(position);
holder.mImage.setImageResource(viewpager.getImage());
holder.mText.setText(viewpager.getName());
}
@Override
public int getItemCount() {
return viewpagerList.size();
}
class HorizontalVpViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
TextView mText;
HorizontalVpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.image);
mText = itemView.findViewById(R.id.text);
}
}
}
在这里,顺便我们把RecycleView复习一下
a.
首先我们创建了一个HorizontalVpViewHolder内部类,继承RecycleView.ViewHolder.然后我们在构造函数中传入了一个View,这个就是每个页面的最外层布局了
b.
接着我们创建一个HorizontalVpAdapter的构造函数,这个用来把要使用的数据都传进来,我们后续的操作都是围绕这个数据进行的
c.
onCreateViewHolder()这个方法是用来创建一个ViewHolder实例的,我们在这个方法中将布局加载进来,传入到构造函数中,最后在返回这个实例.
d.
onBindViewHolder()这个方式就是对每个子项的数据进行赋值,在每个子项被滚入到屏幕的时候进行执行,这里我们可以通过position参数达到当前的实例.然后再将数据设置就可以了
e.
getItemCount()就是告诉ViewPager有多少个子项就可以了,返回链表的长度
4.主函数
package com.example.view_pager2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<ViewPager> viewPagerList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
ViewPager2 viewPager2 = findViewById(R.id.view_pager);
HorizontalVpAdapter horizontalVpAdapter = new HorizontalVpAdapter(viewPagerList);
viewPager2.setAdapter(horizontalVpAdapter);
}
private void initData(){
for (int i = 0; i < 2; i++){
ViewPager viewPager1 = new ViewPager("123", R.drawable.cloudy);
viewPagerList.add(viewPager1);
ViewPager viewPager2 = new ViewPager("456", R.drawable.moon);
viewPagerList.add(viewPager2);
ViewPager viewPager3 = new ViewPager("789", R.drawable.sunny);
viewPagerList.add(viewPager3);
}
}
}
最简单的垂直翻页
变成垂直的翻页只需要两步
a.
给主布局中的ViewPager中加入,使他的布局变成垂直的即可
android:orientation="vertical"
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager"
android:orientation="vertical"/>
b.
给活动中添加
viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
ViewPager2 viewPager2 = findViewById(R.id.view_pager);
HorizontalVpAdapter horizontalVpAdapter = new HorizontalVpAdapter(viewPagerList);
viewPager2.setAdapter(horizontalVpAdapter);
viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
}