How to disable scrollView interaction on the user screen?

Asked

Viewed 193 times

2

On the screen, the user should not be able to touch the Scroll, His drive I’m doing all by code this way for example:

Moving:

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

I’m looking for a way to block off Move by screen to make only by code as shown above.

The content of ScrollView is a LinearLayout (vertical).

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 way:

<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 then you set up your method setScrollingEnabled() setting a value false, as in the example below:

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

Good Luck!

  • I tried to do, it error and asks to implement a constructor, after that, during execution, it gives an error when I enter Activity that implemented the Customscrollview.

  • 1

    @Cristiancotrena calm, I will make a test and I return you! This will solve your case yes.

  • The content of my Scrollview is a Linearlayout (vertical). I forgot to comment on the question..

  • @Cristiancotrena I improved the answer for you by adding the builders ok?! I tested it here now again and it went all right! Hugs.

  • @Cristiancotrena only remembering that the com.mypackagename you have to replace by the package where the class is CustomScrollView. Doing so, it will be all right! As for the LinearLayout vertical, you can insert normally that works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.