How to disable the scrollView interaction on the user's screen?

2

On the screen, the user should not be able to tweak Scroll , his move I'm doing all by code like this:

Drive:

public void move(View v){scroll.scrollTo(160, 0);}

I'm looking for a way to block moving the screen to make it just by code as shown above.

The content of ScrollView is LinearLayout (vertical).

    
asked by anonymous 29.08.2016 / 13:58

1 answer

3

You can create a custom ScrollView.

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    // true se pode mover (nao rolavel)
    // false se não pode mover (rolavel)
    private boolean mScrollable = true;

    public CustomScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }


    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:

                // se podemos rolar passar o evento para a superclasse
                if (mScrollable) 
                    return super.onTouchEvent(ev);

                return mScrollable; 
            default:
                return super.onTouchEvent(ev);
        }
    }

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

}

In your xml do this:

<com.mypackagename.CustomScrollView
    android:id="@+id/scroll" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

    <ListView android:id="@+id/listview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    </ListView >

</com.mypackagename.CustomScrollView>

So you set your setScrollingEnabled() method by setting a false value, as in the example below:

CustomScrollView customScrollView;
customScrollView = (CustomScrollView) findViewById(R.id.scroll);
customScrollView.setScrollingEnabled(false);

Good Luck!

    
29.08.2016 / 14:14