Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting onClickListener on ScrollView items disables the scrolling effect

I have a custom scrollview and I'm applying a touchListener because I want to drag it up and down on the screen. Also inside this scrollview I have a few child items that have a clickListener attached.

My problem is that when trying to click on the items it doesn't happen anything. I guess the problem is that the touch listener somehow interferes with the click listener but I don't know what should I do.

Here's my custom scrollview:

public class ObservableScrollView extends ScrollView {
private Callbacks mCallbacks;
private boolean enableScrolling = true;

public boolean isEnableScrolling() {
    return enableScrolling;
}

public void setEnableScrolling(boolean enableScrolling) {
    this.enableScrolling = enableScrolling;
}

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

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (mCallbacks != null) {
        mCallbacks.onScrollChanged(t);
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(isEnableScrolling() && mCallbacks!=null){
        return super.onTouchEvent(ev);
    }
    else{
        return false;
    }
}


@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
{
    //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
    super.onOverScrolled( scrollX,  scrollY,  clampedX,  clampedY);
}

@Override
public int computeVerticalScrollRange() {
    return super.computeVerticalScrollRange();
}

public void setCallbacks(Callbacks listener) {
    mCallbacks = listener;
}

public static interface Callbacks {
    public void onScrollChanged(int scrollY);

    public void onDownMotionEvent();

    public void onUpOrCancelMotionEvent();
}

}

Also in my activity I've attached a touch listener to this scrollview because as I've said I need to move the scrollview up/down.

Can you please tell me how I can have the click listener triggered and also keep the touch effect? Thanks!

like image 507
Alin Avatar asked Oct 21 '22 08:10

Alin


1 Answers

As I recall, it works as a chain of events.

First the onTouch is triggered, going through the onTouch listeners.

If any of the listeners returns true, it means the touch has been handled and no onClick will be sent.

If all of the listeners return false, then the systems considers the onTouch to not have been processed and when the click is completed, onClick will be triggered.

You may want to check how much the user has scrolled since the touch started and return false if the amount of scroll is small.

like image 96
shalafi Avatar answered Nov 03 '22 21:11

shalafi