Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable / Enable Scroll for ListView in android

I have a ListView inside ScrollView. I can enable scroll of ListView by

listView.getParent().requestDisallowInterCeptTouchEvent(true);

But the problem is when i scroll up in listView and it reaches top it should scroll to parent view i.e. parent scroll has to work . How can i do this ? any suggestion please.

like image 401
prabhu Avatar asked Jun 10 '26 15:06

prabhu


2 Answers

listView.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            return true; // Indicates that this has been handled by you and will not be forwarded further.
        }
        return false;
    }
});

OR

To make the View unselectable just get the view and .setClickable(false)

OR

listView.setScrollContainer(false);
like image 170
Kanaiya Katarmal Avatar answered Jun 13 '26 23:06

Kanaiya Katarmal


You can override ScrollView class and insert these methods inside:

private boolean isScrollEnabled = true;

public void enableScroll(boolean isScrollEnabled ) {
    this.isScrollEnabled = isScrollEnabled ;
}

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

This is the cleanest solution to achieve this. You only call scrollView.enableScroll(true) to enable scrolling or scrollView.enableScroll(false) to disable it.

like image 42
Gaskoin Avatar answered Jun 13 '26 21:06

Gaskoin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!