user2605090 user2605090 - 6 months ago 35
Java Question

Android - How To Disable The Sliding Aspect of SlidingTabLayout

Seeing how the tab layout of the action bar is now deprecated I need to use a sliding tab layout.

However, I don't want the user to be able to swipe because the first tab will contain a google map that the user can move around and navigate through like any normal map.

So I'd like to limit the user to only clicking on each tab, rather than swiping, to change screens.

Also, is there anyway I can keep the first tab loaded even when the user switches tabs? I don't want to have to load/redraw the google map EVERY time.

JLT JLT
Answer

I 'll answer your question regarding disabling the swiping event in a tab layout.

If you are using ViewPager in your layout then you can make a custom ViewPager which extends the ViewPager and set a method that disables and enables the swiping.

Here is a sample:

public class CustomViewPager extends ViewPager 
{
    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        if (this.enabled) 
        {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) 
    {
        if (this.enabled) 
        {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) 
    {
        this.enabled = enabled;
    }
}

As can be seen from the code above, you can disable and enable the swiping anytime by calling the method setPagingEnabled(boolean enabled).

Hope it helps. :)

Comments