ria
ria

Reputation: 5

How to disable HorizontalScrollView scrolling on button click and enable again on another button click?

I added a HorizontalScrollView in xml and I want to disable scrolling on button click and enable again on another button click.

Disabling scrolling with button click works but I don't know how to enable scrolling again.

The code below is how disabling scrolling worked.

class OnTouch implements View.OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}

I added the above class and then,

final HorizontalScrollView scrollView = (HorizontalScrollView)findViewById    (R.id.horizontalScrollView);
    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            scrollView.setOnTouchListener(new OnTouch());
        }
    });

I added the above code inside onCreate method. I want to add another button(maybe "scroll") and I want that button to enable scrolling again.

Upvotes: 0

Views: 1700

Answers (2)

Zelig
Zelig

Reputation: 1808

1 - In the onClickListener function associated to your Button :

a) remove scrollView.setOnTouchListener(new OnTouch()),

b) toggle a boolean (scrollEnabled for instance) to indicate if scrolling is enabled or not.

2 - In your ScrollView class, override the onTouchEvent function and put this in it :

    if(scrollEnabled){
        return(false);
    else {
        return(true);
    }

Upvotes: 0

lionscribe
lionscribe

Reputation: 3513

How about this (I didn't test it, so there may be typos);

class OnTouch implements View.OnTouchListener { 
    public boolean intercept = false;
    @Override public boolean onTouch(View v, MotionEvent event) { 
        return intercept; 
} }

final OnTouch listener = new OnTouch()); 
final HorizontalScrollView scrollView = (HorizontalScrollView)findViewById (R.id.horizontalScrollView); 
scrollView.setOnTouchListener(listener);
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() { listener.intercept=true});
Button start = (Button)findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() { listener.intercept=false});

Upvotes: 1

Related Questions