当适配器包含带有 onClickListener 的按钮时,OnItemClickListener 无法正常工作
我已经为我的 ListView
实现了一个适配器,它扩展了 BaseAdapter。 我的列表项包含每个按钮都有 OnClickListener。
为每个项目添加 OnclickLister 后,列表的 OnItemClickListener 停止工作。
如何解决?
代码
在我的 Activity 中 -
ListView lv = (ListView) findViewById(R.id.list);
lv.setTextFilterEnabled(true);
lv.setItemsCanFocus(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String debString = "position = " + position + " id = " + id;
Log.d(TAG, debString);
Toast.makeText(getApplicationContext(), debString, Toast.LENGTH_SHORT).show();
Contact selectedContact = dataVector.elementAt(position);
Bundle bundle = new Bundle();
bundle.putInt(Constants.POSITION, position);
bundle.putString(Constants.NAME, selectedContact.getName());
bundle.putString(Constants.MDN, selectedContact.getMdn());
bundle.putString(Constants.STATUS, selectedContact.getStatus());
String filePath = null;
if(contactsImagesProperties != null || !Utils.isNullOrEmpty((String) contactsImagesProperties.get(selectedContact.getMdn()))) {
filePath = (String) contactsImagesProperties.get(selectedContact.getMdn());
}
bundle.putString(Constants.IMAGE, filePath);
Intent intent = new Intent(context, ChildDisplayActivity.class);
intent.putExtras(bundle);
getParent().startActivityForResult(intent, 10);
}
在 myListRaw.xml 中的 getView() 中的 myBaseAdapter 中
bitmap = Bitmap.createScaledBitmap(bitmap, Constants.CHILD_ICON_WIDTH, Constants.CHILD_ICON_HEIGHT, false);
imageView.setImageBitmap(bitmap);
statusView.setText(Constants.StatusCodeHandler.getStatusDesc(dataVector.elementAt(position).getStatus(), context));
ImageButton imageButton = (ImageButton) view.findViewById(viewIds[3]);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt(Constants.ACTION, Constants.CONTACT_LOCATION_CODE);
bundle.putString(Constants.MDN, dataVector.elementAt(position).getMdn());
MainActivity.bundle = bundle;
TabActivity mainActivity = (TabActivity) ((UsersListActivity)context).getParent().getParent();
TabHost tabHost = mainActivity.getTabHost();
tabHost.setCurrentTab(Constants.MAP_TAB_INDEX);
}
});
-
<ImageView android:src="@drawable/icon"
android:id="@+id/childListImageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
<TextView android:id="@+id/childListTextView"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:text="TextView"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_toLeftOf="@+id/childListImageView"
android:layout_centerVertical="true"/>
<TextView android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Child Status"
android:id="@+id/childListStatus"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/childListTextView"
android:layout_marginRight="15dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
<ImageButton android:id="@+id/childListButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loc"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"/>
I have implemented an adapter for my ListView
which extends BaseAdapter.
My list items contain buttons that each of them have OnClickListener.
after adding OnclickLister for each item, the list's OnItemClickListener stoped working.
How can it be fixed?
the code
In my Activity -
ListView lv = (ListView) findViewById(R.id.list);
lv.setTextFilterEnabled(true);
lv.setItemsCanFocus(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String debString = "position = " + position + " id = " + id;
Log.d(TAG, debString);
Toast.makeText(getApplicationContext(), debString, Toast.LENGTH_SHORT).show();
Contact selectedContact = dataVector.elementAt(position);
Bundle bundle = new Bundle();
bundle.putInt(Constants.POSITION, position);
bundle.putString(Constants.NAME, selectedContact.getName());
bundle.putString(Constants.MDN, selectedContact.getMdn());
bundle.putString(Constants.STATUS, selectedContact.getStatus());
String filePath = null;
if(contactsImagesProperties != null || !Utils.isNullOrEmpty((String) contactsImagesProperties.get(selectedContact.getMdn()))) {
filePath = (String) contactsImagesProperties.get(selectedContact.getMdn());
}
bundle.putString(Constants.IMAGE, filePath);
Intent intent = new Intent(context, ChildDisplayActivity.class);
intent.putExtras(bundle);
getParent().startActivityForResult(intent, 10);
}
in myBaseAdapter in getView()
bitmap = Bitmap.createScaledBitmap(bitmap, Constants.CHILD_ICON_WIDTH, Constants.CHILD_ICON_HEIGHT, false);
imageView.setImageBitmap(bitmap);
statusView.setText(Constants.StatusCodeHandler.getStatusDesc(dataVector.elementAt(position).getStatus(), context));
ImageButton imageButton = (ImageButton) view.findViewById(viewIds[3]);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt(Constants.ACTION, Constants.CONTACT_LOCATION_CODE);
bundle.putString(Constants.MDN, dataVector.elementAt(position).getMdn());
MainActivity.bundle = bundle;
TabActivity mainActivity = (TabActivity) ((UsersListActivity)context).getParent().getParent();
TabHost tabHost = mainActivity.getTabHost();
tabHost.setCurrentTab(Constants.MAP_TAB_INDEX);
}
});
in myListRaw.xml -
<ImageView android:src="@drawable/icon"
android:id="@+id/childListImageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
<TextView android:id="@+id/childListTextView"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:text="TextView"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_toLeftOf="@+id/childListImageView"
android:layout_centerVertical="true"/>
<TextView android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Child Status"
android:id="@+id/childListStatus"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/childListTextView"
android:layout_marginRight="15dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
<ImageButton android:id="@+id/childListButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loc"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将行的部分设置为可聚焦 (android:focusable="true"),则 ListView 的 OnItemClickListener 不会响应。一探究竟
If you set your parts of row as focusable (android:focusable="true") than OnItemClickListener for ListView doen not respond. Check it out