我在 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 的 ArrayList 中添加一些值,每个 HashMap 包含 2 个 String 变量,它们显示在 ListView 的每一行中。对于ListView的自定义xml布局,RelativeLayout里面只有一个ImageView和一个TextView。它显示得很好,除了然后我点击一行,没有任何反应,它不会触发日志或任何东西。重写方法 onItemClick 根本没有被触发。
我已经尝试在 ListView 上设置 Clickable(true) 或 setItemsCanFocus(false),或者在 ImageView 和 TextView 上设置 setFocusable(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>
无论如何,我不知道该怎么办了,任何人都可以帮助我吗?
如果您需要更多信息,请告诉我。谢谢