46

アイテムレイアウトにクリック可能/編集可能なウィジェット(RadioButton、EditText、またはCheckBox)がある場合、ListViewでOnItemClickListenerを使用することは可能ですか?

4

6 に答える 6

69

この問題を確認することをお勧めします。aの行にフォーカス可能なアイテムがあるListViewと、OnItemClickListenerNOTが呼び出されます。ただし、これは、フォーカス可能/クリック可能なアイテムを連続して配置できないことを意味するものではありません。このような回避策がいくつかあります。

また、通話履歴画面もご覧いただけます。クリック可能なListViewアイテム(右側の通話アイコン)があります。 ここでソースコードを参照してください

于 2010-01-20T03:31:04.620 に答える
21

Samuhが言及したリンクのコメント#31を引用します(これは私にとって問題を解決しました):

実際、これをレイアウトXMLに追加できます(1つ膨らんだ場合):android:descendantFocusability="blocksDescendants"。

ここにJICを追加すると、そのWebページは将来ダウンします。

于 2012-09-03T03:27:23.883 に答える
12

リストのいずれかの行アイテムにフォーカス可能またはクリック可能なビューが含まれている場合、機能しOnItemClickListenerません。

行アイテムは次のようなパラメータを持っている必要がありますandroid:descendantFocusability="blocksDescendants"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>
于 2013-02-22T06:20:10.950 に答える
3

多くの複雑なソリューションを試しましたが、これは最も簡単なソリューションでした。

次のように使用android:focusable="false"します。

<CheckBox
    android:id="@+id/fav_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false" />
于 2013-10-11T10:04:12.853 に答える
0

2つの最良の解決策

  1. android:descendantFocusability="beforeDescendants"xmlでlistViewに追加または
  2. 指定された2つの属性をに設定しますfalse

お気に入り

   android:focusable="false"
   android:focusableInTouchMode="false"

次に、listView.setOnItemClickの代わりにlistView行アイテムの子(Button、EditTextなど)イベントを処理します。

于 2015-01-12T09:02:35.973 に答える
0

私は別の問題を修正しました。私のアイテムには複数のLinearLayoutがあるので、アダプタークラスのlinearayoutとsetOnclickListenerにidを指定すると機能し、タッチの元の効果のみが消えます。しかし、このリンクは、LinearLayoutをボタンの ように動作させることは、linearlaoutをクリック時にボタンのように動作させるのに役立ちます

アイテム

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp">

    <TextView
        android:id="@+id/txt_item_followers_name"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center|start"
        android:paddingLeft="15dp"
        android:text="Ali"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/txt_item_followers_name"
        android:layout_marginLeft="10dp"
        android:src="@drawable/puan_icon" />

    <TextView
        android:id="@+id/txt_item_followers_mark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"
        android:background="@color/red_400"
        android:paddingLeft="10dp"
        android:text="25.5"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:id="@+id/linear_one"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/txt_item_followers_name"
        android:background="@color/red_400"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/btn_item_followers_2b_follow"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/follow_buton" />
    </LinearLayout>


</RelativeLayout>

getViewメソッド内

 @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        View view = convertView;
        if (convertView == null)
            view = inflater.inflate(R.layout.deneme, null);

        final Followers2 myObj = myList.get(position);
        LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNİCATE IT
        linear_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
            }
        });

        TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
        TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
        final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
        name.setText(myObj.getName());
        mark.setText(myObj.getScore());
       /* if (myObj.isFollow() == true) {
            btn_follow.setImageResource(R.drawable.following_buton);
        } else {
            btn_follow.setImageResource(R.drawable.follow_buton);
        }

        btn_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Followers2 myObj = myList.get(position);
                if (myObj.isFollow() == true) {
                    btn_follow.setImageResource(R.drawable.following_buton);
                } else {
                    btn_follow.setImageResource(R.drawable.follow_buton);
                }
            }
        });*/

        return view;
    }
于 2016-07-21T12:53:41.723 に答える