OnItemClickListener不会在自定义ListView上触发

时间:2012-10-26 06:37:58

标签: android listview adapter onitemclicklistener onitemclick

我在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上尝试了setClickable(true)或setItemsCanFocus(false),或者在ImageView和TextView上尝试了setFocusable(false)和setFocusableInTouchMode(false)(即使它适用于Checkboxes和Buttons吧?)。

这是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>

无论如何,我不知道该怎么办,有人可以帮帮我吗?

如果您需要更多信息,请告诉我们。感谢

2 个答案:

答案 0 :(得分:0)

您的观点不接受该事件的原因是因为TextView(可聚焦)覆盖它并为自己采取所有触摸/点击操作。

您只需为TextView设置这些:

android:focusable="false"

android:focusableInTouchMode="false"

答案 1 :(得分:0)

找到了另一种解决方案:https://stackoverflow.com/a/6579192/1759005。它工作得很好。