暂时/动态禁用Viewpager中的单个页面 [英] Temporarily/Dynamically disable single page in Viewpager

查看:159
本文介绍了暂时/动态禁用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" />

最后在适当位置的片段"或活动"中引用它:

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中的单个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆