我在listView上遇到了onItemClickListener的问题,没有触发OnItemClick覆盖方法...这是我的代码:
public void popupCatCategories(){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupCatCategoriesView = layoutInflater.inflate(R.layout.popup_cats_categories, null);
final PopupWindow popupWindow = new PopupWindow(popupCatCategoriesView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button) popupCatCategoriesView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
// listview to display image and text
ListView listCatCategories = (ListView) popupCatCategoriesView.findViewById(R.id.listCatCategories);
List<HashMap<String, String>> listCategories = new ArrayList<HashMap<String, String>>();
db.open();
Cursor catCategories = db.getAllCatCategoryList();
if (catCategories.moveToFirst())
{
do {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("catCategoryThumbnail", Integer.toString(catCategories.getInt(1)));
hm.put("catName", catCategories.getString(0));
listCategories.add(hm);
} while (catCategories.moveToNext());
}
db.close();
listCatCategories.setAdapter(new ItemListBaseAdapter(getApplicationContext(), listCategories));
Log.i("got it?", "wait for it...");
listCatCategories.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View view, int position, long id) {
Log.i("got it?", "yea, got it!");
Toast.makeText(getBaseContext(), "got it", Toast.LENGTH_SHORT).show();
}
});
popupWindow.showAsDropDown(btnCats, 50, -330);
}
我只是在hashmap的阵列列表中添加了一些值,每个hashmap包含2个字符串变量,这些变量显示在listView的每一行中。ListView的自定义XML布局中只有一个imageView和一个relativelayout中的文本视图。它显示得很好,除了我单击一行,什么也不会发生,它不会触发日志或任何东西。根本没有触发覆盖方法OnItemClick。
我已经尝试在listView上使用setClickable(true)或setItemScanFocus(false),或者在imageView和textView上(即使它适用于复选框和按钮,compts and set focusable(false)(false)和setFocusableIntouchmode(false)。
这是ListView的自定义XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/catCategoryThumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<TextView
android:id="@+id/txtCatCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
无论如何,我不知道该怎么办,有人可以帮我吗?
如果您需要更多信息,请告诉我。谢谢
您的视图不接受事件的原因是因为TextView(可集中)正在覆盖它,并为其自身采取所有触摸/单击操作。
您只需要为文本视图设置这些:
android:focusable="false"
android:focusableInTouchMode="false"
找到了一个替代解决方案:https://stackoverflow.com/a/6579192/1759005。它的工作正常。