0

I have a "cell" in my list with picture, text and a checkbox. Here's my xml fragment_people_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
    // ... more elements
    <CheckBox />
</RelativeLayout>

I'd like to distinguish if user taps on a checkbox or on the rest of the view. A relevant part of my RecyclerView.OnItemTouchListener:

@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent e) {
    View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

}

It says that findChildViewUnder finds the topmost view under the given point which is ALWAYS my RelativeLayout!

How can I know on which view exactly user tapped (clicked)?

Dmitry
  • 1,484
  • 2
  • 15
  • 23
  • You should use custom adapter to acomplish such thing. Try this answer http://stackoverflow.com/a/13220810/783612. – fastholf Nov 24 '15 at 17:17
  • Well I I can easily attach an `OnClickListener` or `OnCheckedChangeListener`. The problem is that clicking on a checkbox do 2 things: 1. ticks a checkbox itself; 2: is propogated further to my `onInterceptTouchEvent` listener – Dmitry Nov 25 '15 at 02:32
  • And in my `onInterceptTouchEvent` listener I don't want to call `setChecked` on my checkbox if the click was on it and vise versa. Does it make sense? – Dmitry Nov 25 '15 at 02:34

1 Answers1

0

To distinguish clicks on checkBox from clicks on the rest of the list item you should use custom adapter for your list and override getView. In getView method you can set OnClickListener separately for your checkBox and for the item.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, parent, false);
    }
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("@@@", "checkbox clicked");
        }
    });
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("@@@", "item clicked");
        }
    });
    return convertView;
}
fastholf
  • 11
  • 3
  • I certainly use my own adapter which extends from `RecyclerView.Adapter` and it doesn't have a method `getView` to override. – Dmitry Nov 26 '15 at 05:30
  • Sorry, `getView` method is used in adapter for `ListView`. In adapter for `RecyclerView` the same thing may be applied in `onBindViewHolder` method. – fastholf Nov 26 '15 at 09:56