android 如何禁用/锁定viewpager中的一个页面?

0pizxfdo  于 2023-04-28  发布在  Android
关注(0)|答案(5)|浏览(161)

我想锁定/禁用一个特定的页面(片段)在viewpager。根据一些人只能访问该页面的情况。如何限制滑动到该页面。

我使用以下代码禁用了“事件”页的选项卡单击:

LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
if(!isAccess){
            tabStrip.getChildAt(3).setClickable(false);
            tabStrip.getChildAt(3).setEnabled(false);
}

但是现在我需要限制在视图分页器中对该页面的滑动。如何操作?

n6lpvg4x

n6lpvg4x1#

我写了这个在寻呼机适配器它的作品。

@Override
public int getCount() {
  if(!isAccess) {
    return 3;
  } else {
    return 4;
  }
}
jvlzgdj9

jvlzgdj92#

用这个

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

public void onPageSelected(int position) {
    if (position == 3)
   {
     // show dialog that you are not allowed
      viewPager.setSelected(2);
   }  
}
});

希望这对你有帮助
您还可以使用

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

// optional 
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

// optional 
@Override
public void onPageSelected(int position) { }

// optional 
@Override
public void onPageScrollStateChanged(int state) { }
});
f1tvaqid

f1tvaqid3#

这也是不使用已弃用的方法(setOnPageChangeListener)的解决方案。

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);

            if(position == 3) {
                //To disable specific tab and set the previuos tab
                viewPager.setCurrentItem(2); //We cannot provide the position in setSelected(boolean) now. This was missed in above solution
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    });
lymnna71

lymnna714#

初始化第一个制表符位置

int currentTab = 0;

然后为viewPager设置监听器

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //4 is the position of the tab we want to disable
                if (position == 4) {
                    if (currentTab < 4) {
                        // swipe from left to right
                        viewPager.setCurrentItem(5);
                        currentTab = 5;
                    } else {
                        // swipe from right to left 
                        viewPager.setCurrentItem(3);
                        currentTab = 3;
                    }
                } else {
                    currentTab = position;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
hrirmatl

hrirmatl5#

要阻止特定的选项卡被选中,只需使用自定义的TabLayout和ViewPager,您甚至可以通过这种方式将选项卡用于其他目的。

public class CustomViewPager extends ViewPager {
private float initialXValue;
public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event)
{
    if(event.getAction()==MotionEvent.ACTION_DOWN)
        initialXValue = event.getX();
    return this.isSwipeAllowed(event) && super.onInterceptTouchEvent(event);
}
private boolean isSwipeAllowed(MotionEvent event) {
    //you can prevent any swipe from right to left for an item at a specified index, in your case getCurrentItem() returns 3 for Event tab 
    return getCurrentItem()!=3 || event.getAction() != MotionEvent.ACTION_MOVE || (event.getX() - initialXValue) > 0;
}
}

自定义TabLayout

public class CustomTabLayout extends TabLayout {

public CustomTabLayout(Context context,AttributeSet attrs) {
    super(context, attrs);
}
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void selectTab(Tab tab, boolean updateIndicator) {
    // it doesn't allow the item on index 3 to be selected in anyway
    if (!(tab.getPosition() == 3))
        super.selectTab(tab, updateIndicator);
}

}

相关问题