临时/动态禁用 Viewpager 中的单个页面

Temporarily/Dynamically disable single page in Viewpager(临时/动态禁用 Viewpager 中的单个页面)

本文介绍了临时/动态禁用 Viewpager 中的单个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展的 FragmentPagerAdapter,它为 ViewPager 提供了 3 个片段,给了我 3 个页面,我可以在它们之间滑动或使用我添加到操作栏的选项卡来手动选择页面.

I have an extended FragmentPagerAdapter that provides 3 fragments to a ViewPager, giving me 3 pages that I can either swipe between or use the tabs I've added to the actionbar to manually choose a page.

我想暂时禁止用户使用任一导航类型访问最终页面(禁用任何特定页面的更通用的解决方案也很有趣,但不是必需的).然后,我将通过其中一个片段的回调重新启用他们的访问权限.

I'd like to temporarily disable the user from accessing the final page (a more generalized solution to disable any specific page would also be interesting, but not necessary) with either navigation type. I will then re-enable their access with a callback from one of the fragments.

我已经阅读了一些类似问题的答案,这些问题是关于不允许通过覆盖 onTouchEvent 和 onInterceptTouchEvent 进行滑动,然后在其中使用一些自定义手势检测,甚至是使用 beginFakeDrag() 的半解决方案.

I've read through answers to some similar questions about not allowing swiping by overriding onTouchEvent and onInterceptTouchEvent, and then using some custom gesture detection within those, or even a half solution using beginFakeDrag().

我当前的解决方案只是在适配器中设置较小的项目计数,确保使用 notifyDataSetChanged(),并在必要时将其更改回来.这些方法是否有任何优雅的替代方案,也许不需要更改数据集?

My current solution simply sets a smaller item count in the adapter, making sure to use notifyDataSetChanged(), and changes it back when necessary. Are there any elegant alternatives to these approaches, maybe one that doesn't require changing the dataset?

我还看到 ActionBar.Tab 没有 setEnable() 方法,大多数建议只删除选项卡.有没有办法让标签保持可见但不可选?我的方法是定义哪个选项卡被禁用,如果用户选择了禁用的选项卡,则重新选择之前选择的选项卡.

I also see that ActionBar.Tab doesn't have a setEnable() method, and most suggest to just remove the tab. Is there a way I can have a tab remain visible but not selectable? My approach has been to define which tab is disabled and reselect the previously selected tab if the user selects the disabled one.

我的目标是禁用标签和滑动到最后一页.

I aim to disable both tabbing and swiping to the last page.

推荐答案

您肯定会想要创建自己的自定义 ViewPager 子类.我创建了一个名为 CustomSwipePager 的简单自定义 ViewPager,它将在需要时处理阻止用户交互.

You're definitely going to want to create your own custom ViewPager subclass. I created a simple custom ViewPager called CustomSwipePager that will handle blocking user interaction when needed.

public class CustomSwipeViewPager extends ViewPager {

    private boolean mLastPageEnabled = false;
    private int mLastPageIndex = 0;

    public NoSwipeViewPager(Context context) {
        super(context);
    }

    public NoSwipeViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setLastPageEnabled(boolean enabled) {
        mLastPageEnabled = enabled;
    }

    public void setLastPageIndex(int index) {
        mLastPageIndex = index;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event){

        if(!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
            // Always return false to disable user swipes
            return false;
        }

        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
            // Always return false to disable user swipes
            return false;
        }

        return true;
    }
}

setLastPageEnabled()setLastPageIndex() 类中有两个关键方法您需要利用.您可以将最后一页索引设置为您需要的任何内容,如果您有三个项目,您可以将其设置为 2.然后还使用 setLastPageEnabled(false) 禁用滑动或重新启用使用setLastPageEnabled(true).

There are two key methods you will want to take advantage of in the class setLastPageEnabled() and setLastPageIndex(). You can set the last page index to whatever you need, in your case if you have three items you would set it to 2. Then also use setLastPageEnabled(false) to disable swiping or to re-enabled use setLastPageEnabled(true).

您可以将此自定义视图包含到您的布局中,如下所示:

You can include this custom view into your layout like this:

<com.mypackage.CustomSwipeViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_swipe_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

最后在你的 Fragment 或 Activity 中适当的地方引用它:

And finally reference it in your Fragment or Activity in the appropriate place:

private CustomSwipeViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    mPager = (CustomSwipeViewPager) findViewById(R.id.custom_swipe_view_pager);
    mPager.setLastPageEnabled(false);
    mPager.setLastPageIndex(2);
}

这篇关于临时/动态禁用 Viewpager 中的单个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:临时/动态禁用 Viewpager 中的单个页面