OnItemClickListener consumes the onClickListener event
I’ve read about onClickListener consuming onItemClickListener events, such as here或 here My question is exactly the opposite:
I
have a CustomAdapter extends ArrayAdapter where I place model objects to generate a row. In my activity, I registered an onItemClickListener like this
//items can focus false to try to get the onItemClick-event
mListView.setItemsCanFocus(false);
mListView.setOnItemClickListener(this);
mListView.setAdapter(this.favoritePointAdapter);
Then inflating my layout in my Adapter.getView() method, getting my ImageView and registering an OnClickListener on it like this
@Override
public View getView(int position, View convertView, ViewGroup parent){
//.... inflate other Views.....
LinearLayout clickArea = (LinearLayout)convertView.findViewById(R.id.list_row_favorite_point_click_area);
clickArea.setOnClickListener(this);
//... other logic follows ...
}
At least I tried to get this event in my adapter like this
@Override
public void onClick(View v) {
Log.d("ListAdapter", "onClick triggered");
never triggered
switch (v.getId()) {
case R.id.list_row_favorite_point_click_area:
Log.d("ListAdapter", "onClick id->list_row_favorite_point_click_area");
never triggered
break;
default:
break;
}
}
If you find RowView’s xml interesting, here it is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_row_relative_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="false">
<LinearLayout
android:id="@+id/list_row_icon"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:layout_margin="3dip"
/>
<LinearLayout
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<TextView
android:id="@+id/list_row_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="defaultText"
android:textStyle="bold" />
<TextView
android:id="@+id/list_row_region"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#999999"
android:textStyle="italic"
android:text="Region" />
</LinearLayout>
<LinearLayout
android:id="@+id/list_row_favorite_point_click_area"
android:layout_width="50dip"
android:layout_height="50dip">
<ImageView
android:id="@+id/list_row_favorite_icon"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_gravity="center_vertical"
android:layout_weight="0.1">
</ImageView>
</LinearLayout>
</LinearLayout>
As I said, the
onItemClick
of my activity’s ListView was triggered, and I never reached the onClick inside my adapter. I think the onItemClick “feel” is responsible for the entire row and consumes TouchEvents. What can I do to fix this?
Note: As mentioned in other issues, I also tried setting setItemsCanFocus(false) and focusable in the root directory to false.
Solution
Instead of using onClickListener, I recommend using onTouchListener.
Check out this tutorial, which I previously used to get the “sliding screen” in the app:
http://www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/
Also, see the android development documentation on onTouchListener:
http://developer.android.com/reference/android/view/View.OnTouchListener.html