-1

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 には、ListView の各行に表示される 2 つの文字列変数が含まれています。ListView のカスタム xml レイアウトの RelativeLayout 内には、ImageView と TextView のみがあります。正常に表示されましたが、行をクリックしても何も起こらず、ログなどもトリガーされません。Overridden メソッド onItemClick はまったくトリガーされません。

ListView で setClickable(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>

とにかく、もうどうしたらいいのかわからないので、誰か助けてくれませんか?

さらに情報が必要な場合は、お知らせください。ありがとう

4

2 に答える 2

0

別の解決策を見つけました: https://stackoverflow.com/a/6579192/1759005。それは同じようにうまく機能しています。

于 2012-10-26T07:38:41.983 に答える
0

ビューがイベントを受け入れない理由は、TextView (フォーカス可能) がそれをオーバーライドし、すべてのタッチ/クリック アクションを自分で実行しているためです。

これらを TextView に設定するだけです。

android:focusable="false"

android:focusableInTouchMode="false"

于 2012-10-26T07:01:18.490 に答える