欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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)

相关标签: Material android