Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not scroll ListView when cutsom view is touched

I've created a custom view on which you can draw a path with your finger. It extends View class.

When I use it inside a ListView as one of its items if a user touches custom view the ListView is scrolled. How can I prevent this from happening?

I suppose I need to get focus somehow on my custom view. But I don't know how.

Update:

I found possible solution. In my custom view's onTouchEvent(Motion event) method I've placed getParent().requestDisallowInterceptTouchEvent(true);.

Without this the event queue when user touches custom view looked like this:

  1. MotionEvent.ACTION_DOWN
  2. MotionEvent.ACTION_MOVE
  3. MotionEvent.ACTION_MOVE
  4. MotionEvent.ACTION_CANCEL

When I receive MotionEvent with code MotionEvent.ACTION_CANCEL the ListView starts to scroll.

like image 474
Slava Avatar asked Sep 12 '25 13:09

Slava


1 Answers

Setting requestDisallowInterceptTouchEvent(true) within child's onTouch(View, MotionEvent) method works:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewGroup item = (ViewGroup) inflater.inflate(R.layout.list_item, null);
        Button button = (Button)item.findViewById(R.id.list_item_btn);
        button.setText("button " + position);

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                listView.requestDisallowInterceptTouchEvent(true);
                return false;
            }

        });

        return item;
    }
like image 124
Michal Vician Avatar answered Sep 14 '25 03:09

Michal Vician