Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HorizontalScrollView disable scrolling

Tags:

android

I have HorizontalScrollView with long view as a child, so HorizontalScrollView is scrollable and can scroll its child horizontally. Is there any possibility to block that? I don't want user to be able to scroll the view.

like image 393
Jim Avatar asked May 05 '12 13:05

Jim


People also ask

How do I stop my android from scrolling?

You cannot disable the scrolling of a ScrollView. You would need to extend to ScrollView and override the onTouchEvent method to return false when some condition is matched.

How do I stop Nestedscrollview scrolling?

setnestedscrollingenabled set it to false.

How do I turn off horizontal scrolling?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.


2 Answers

My suggestion is to use an OnTouchListener, for example:

In onCreate Method


HorziontalScrollView scrollView= (HorizontalScrollView)findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new OnTouch());

And has a class:


private class OnTouch implements OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    return true;
    }
}
like image 53
Trine Avatar answered Sep 20 '22 22:09

Trine


Ok, I found the way how to implement that.

Just need to create my own HorizontalScrollView and override onTouchEvent method

public class MyHSV extends HorizontalScrollView {

    public MyHSV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

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

    public MyHSV(Context context) {
        super(context);
        init(context);
    }

    void init(Context context) {
        // remove the fading as the HSV looks better without it
        setHorizontalFadingEdgeEnabled(false);
        setVerticalFadingEdgeEnabled(false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Do not allow touch events.
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Do not allow touch events.
        return false;
    }

}

And then in the xml file

<pathToClass.MyHSV xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:id="@+id/myHSV>

</pathToClass.MyHSV>
like image 35
Jim Avatar answered Sep 20 '22 22:09

Jim