2012-10-26 48 views
-1

我在上一个ListView与OnItemClickListener一个问题,不会触发OnItemClick重写的方法......这是我的代码:OnItemClickListener不触发自定义的ListView

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。它显示得很好,除了然后我点击一行,没有任何反应,它不会触发Log或任何东西。 onItemClick的Overridden方法根本不被触发。

我已经试图在ListView上设置ClickClickable(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> 

反正,我不知道该怎么办了,谁能帮帮我吗?

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

+0

你试过这个:listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); –

+0

我刚刚尝试过,仍然没有... – jpmastermind

+0

你在代码中动态地完成了textView.setFocusable(flase)吗?尝试它,因为前几天它不适合我(只是在XML文件中)。它只有当我通过动态编码来完成时才起作用。 –

回答

0

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

你只需要设置这些用于TextView的:

android:focusable="false"

android:focusableInTouchMode="false"

+1

我已经尝试过,它仍然无法正常工作。 onItemClick仍然没有触发:( – jpmastermind

+0

我已经添加了我在BaseAdapter中用于自定义ListView的xml ... – jpmastermind