HeMightBeTodd
HeMightBeTodd

Reputation: 117

Android - how to disable a ScrollView and all children

I feel like this question already has an answer, but I can't find one.

I have a ScrollView in my layout, and it contains a variety of clickable views.

Under a specific condition I would like to disable clicks and events for the ScrollView and ALL of its children.

The following have not been helpful:

ScrollView.setEnabled(false)
ScrollView.setClickable(false)
ScrollView.setOnTouchListener(null)

As well as:

(parent view of the ScrollView).requestDisallowInterceptTouchEvent()

I have created a custom ScrollView with the following code:

public class StoppableScrollView extends ScrollView
{
    private static boolean stopped = false;

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if(stopped)
        {
            return true;
        }
        else
        {
            super.onInterceptTouchEvent(ev);
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        if(stopped)
        {
            return true;
        }
        else
        {
            super.onTouchEvent(ev);
            return false;
        }
    }

    public static void setStopped(boolean inBool)
    {
        stopped = inBool;
    }

    public static boolean getStopped()
    {
        return stopped;
    }
}

Using only onTouchEvent() will stop the scrolling, but not the clicking of child views.

Using only onInterceptTouchEvent() makes it such that when clicks work scrolling does not, and vice versa.

Using both onTouchEvent() and onInterceptTouchEvent() successfully stops unwanted clicks on child views when stopped is 'true' but it also disables scrolling regardless of the state of stopped.

Is there an easier way to get this behaviour, or is there a way to modify the StoppableScrollView class so that it will handle these touch events properly?

Upvotes: 2

Views: 3497

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

As scrollview allows immeditate one child say in my case i have linear layout.and in this linear layout i have other conreolls.

now our first task is to get this linear layout so what we can write is

LinearLayout l = (LinearLayout) scrollview.getChildAt(0);

now after getting this linear layour we can easily access other controlls placed inside it via this code and disable it.

for(int i =0; i<l.getChildCount(); i++)
{
    Log.i(TAG,"child "+ l.getChildAt(i));
    l.getChildAt(i).setEnabled(false);
}

Upvotes: 0

androidavid
androidavid

Reputation: 1259

What probably should help is the following (because I had similar problems):

In the ScrollView you should do a RelativeLayout as Main Child (ScrollView does accept only 1 main child anyway). This RelativeLayout should of course of fill_parent in both directions.

At the really end of the RelativeLayout (after all other children), you could put now a LinearLayout with transparent background (#00FFFFFF) which has also fill_parent in both directions. This LinearLayout should have Visibility = View.GONE (by default) Also you have to attach an empty OnClickListener to it. Now, because of zOrder if you make this LinearLayout Visibility = View.Visible it will catch all the events and avoid clicking the children above!

Upvotes: 5

Related Questions