ViewPager2 简单使用 禁止滑动和设置滑动方向
程序员文章站
2022-06-08 09:29:30
...
ViewPager2 简单使用
<androidx.viewpager2.widget.ViewPager2
android:layout_below="@+id/top"
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Adapter
public class MonitorPagerAdapter extends FragmentStateAdapter {
Context context;
List<Fragment> fragments = new ArrayList<>();
public MonitorPagerAdapter(FragmentActivity context, List<Fragment> fragments) {
super(context);
this.context = context;
this.fragments = fragments;
}
@NonNull
@Override
public Fragment createFragment(int position) {
Fragment fragment = fragments.get(position);
return fragment;
}
public Fragment getFragment(int position) {
return fragments.get(position);
}
@Override
public int getItemCount() {
return fragments.size();
}
填充
var listFragment=ArrayList<Fragment>()
listFragment.add(fragment()))
listFragment.add(fragment()))
mAdapter = MonitorPagerAdapter(mActivity, listFragment)
mActivity.binding.vpContent.adapter=mAdapter
//禁止滑动
mActivity.binding.vpContent.setUserInputEnabled(false)
//设置滑动方向
//mActivity.binding.vpContent.setOrientation(ViewPager2.ORIENTATION_VERTICAL)