文章目录
1. 效果
类似上一篇文章 Android - ViewPager2
只是改用Fragment实现每个页面
2. 添加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>
3. 添加Fragment页面
layout右键new,选择Fragment -> Fragment(Blank)
添加后会出现 fragment_blank.xml
和 BlankFragment.java
两个文件。
fragment_blank.xml
中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:id="@+id/fragment_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="30sp" android:text="@string/hello_blank_fragment" />
</FrameLayout>
BlankFragment.java
代码修改如下:
public class BlankFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
// TODO: Rename and change types of parameters
private String mParam1;
private TextView mFragmentTv;
private View mFragmentView;
public BlankFragment() {
// Required empty public constructor
}
/** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @return A new instance of fragment BlankFragment. */
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (mFragmentView == null){
mFragmentView = inflater.inflate(R.layout.fragment_blank, container, false);
mFragmentTv = mFragmentView.findViewById(R.id.fragment_tv);
mFragmentTv.setText(mParam1);
}
return mFragmentView;
}
}
4. 添加FragmentStateAdapter类的继承类
public class MyFragmentStateAdapter extends FragmentStateAdapter {
private List<Fragment> mFragmentList;
public MyFragmentStateAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
super(fragmentManager, lifecycle);
mFragmentList = fragments;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemCount() {
return mFragmentList.size();
}
}
5. 添加ViewPager2的逻辑
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager2 viewPager2 = findViewById(R.id.viewpager);
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(BlankFragment.newInstance("第一个页面"));
fragmentList.add(BlankFragment.newInstance("第二个页面"));
fragmentList.add(BlankFragment.newInstance("第三个页面"));
fragmentList.add(BlankFragment.newInstance("第四个页面"));
fragmentList.add(BlankFragment.newInstance("第五个页面"));
viewPager2.setAdapter(new MyFragmentStateAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList));
}
}
6. 示例代码
7. 参考
更多详细ViewPager2的例子,可以参考Android官方示例:Android ViewPager2 官方示例
文章评论