Search code examples
androidandroid-layoutandroid-listviewandroid-custom-viewandroid-scrollview

Get the Click event on setOnTouchListener


I am working in an android application and I want to get the click event in setOnTouchListener of a textview inside a custom Listview. When I returns true from the setOnTouchListener I get the click event correctly but the scroll of the ListView will not work at that part of the textview because I have already another click event in that ListView and I have already Overridden ListView setOnTouchListener.

ListView setOnTouchListener

convertView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (IsTablet) {
            if (mDetailList.size() > 3)
                v.getParent().requestDisallowInterceptTouchEvent(true);
        } else {
            if (mDetailList.size() > 2)
                v.getParent().requestDisallowInterceptTouchEvent(true);
        }
        return false;

    }
});

TextView setOnTouchListener

private float mDownX;
private float mDownY;
private final float SCROLL_THRESHOLD = 10;
private boolean isOnClick;

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mDownX = ev.getX();
            mDownY = ev.getY();
            isOnClick = true;
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (isOnClick) {
                Log.i(LOG_TAG, "onClick ");
                //TODO onClick code
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (isOnClick && (Math.abs(mDownX - ev.getX()) > SCROLL_THRESHOLD || Math.abs(mDownY - ev.getY()) > SCROLL_THRESHOLD)) {
                Log.i(LOG_TAG, "movement detected");
                isOnClick = false;
            }
            break;
        default:
            break;
    }
    return true;
}

Is there any way I can get the click event by returning false from TextView setOnTouchListener or please suggest me a another solution to this problem.


Solution

  • I found a solution for my own question.

    Instead of overriding setOnTouchListener of the ListView I have made a custom ListView and Overridden the onInterceptTouchEvent event inside the custom ListView.

    Please see the code below :

    public class CustomListView extends ListView {
    
        public CustomListView(Context context,
                List<DetailSummaryMonth> detailSummaryMonths) {
            super(context);
            init(context);
    
        }
    
        public CustomListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        public CustomListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }
    
        private void init(Context context) {
    
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
    
            if (getAdapter().getCount() > 3)
    
                getParent().requestDisallowInterceptTouchEvent(true);
    
            return super.onInterceptTouchEvent(ev);
        }
    
    }