OnItemClickListener 消耗 onClickListener 事件
我读过有关 onClickListener 将消耗 onItemClickListener 事件的问题,例如 此处 或 此处。我的问题恰恰是另一种方式:
我有一个 CustomAdapter 扩展了 ArrayAdapter,我在其中放置模型对象以生成一行。在我的 Activity 中,我注册了一个 onItemClickListener 像这样
//items can focus false to try to get the onItemClick-event
mListView.setItemsCanFocus(false);
mListView.setOnItemClickListener(this);
mListView.setAdapter(this.favoritePointAdapter);
然后在我的 Adapter.getView() 方法中我膨胀我的布局获取我的 ImageView 并在其上注册一个 OnClickListener
@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 ...
}
至少像这样我尝试像这样在我的适配器中获取此事件
@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;
}
}
如果你找到 xml RowView 的有趣之处在于:
<?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>
正如我所说,我的 Activity 的 ListView 的 onItemClick 被触发,而我从未在适配器内到达 onClick。我认为 onItemClick “感觉”对整行负责并消耗 TouchEvents。我可以做些什么来解决这个问题?
注意:我也尝试了 setItemsCanFocus(false) 并将 root 中的 focusable 设置为 false,正如其他问题中提到的。
I have read about problems that onClickListener will consume onItemClickListener Events like here or here. My Problem is exactly the other way:
I have a CustomAdapter extends ArrayAdapter where I place model Objects to generate a row. In My Activity i registered an onItemClickListener like so
//items can focus false to try to get the onItemClick-event
mListView.setItemsCanFocus(false);
mListView.setOnItemClickListener(this);
mListView.setAdapter(this.favoritePointAdapter);
then in my Adapter.getView() Method I inflate my layout get my ImageView and register 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 Try to fetch this Events 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 xml of RowView 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 ListView's onItemClick
of My Activity gets triggered while I never reach onClick inside my Adapter. I think that onItemClick 'feels' responsible for the whole row and consumes TouchEvents. What can I do to work around it?
Note: I tried setItemsCanFocus(false) and focusable in my root to false, too, as mentioned in the other questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 onTouchListener,而不是使用 onClickListener。
查看本教程,我之前曾在应用程序中使用过该教程来获取“滑动屏幕”:
http: //www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/
另外,请参阅onTouch监听器:
http://developer.android.com/reference/android/view/View .OnTouchListener.html
Rather than using an onClickListener, I'd suggest using the onTouchListener.
Check out this tutorial, which I have used before to get the "swipe screens" in an application:
http://www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/
Also, see the android dev docs on the onTouchListener:
http://developer.android.com/reference/android/view/View.OnTouchListener.html