ListView with clickable/editable widget

2019-01-01 15:34发布

问题:

Is it possible to use a OnItemClickListener on a ListView when the Items layout has a clickable/editable widget (RadioButton,EditText, or CheckBox)?

回答1:

You might want to take a look at this issue. Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked. However, that does not mean you cannot have focusable/clickable items in a row, there are some workarounds like this one.

Also, you can take a look at the Call Logs screen. It has a ListView with clickable item(the call icon on the right). See Source code here



回答2:

Quoting comment #31 in the link mentioned by Samuh (which solved the problem for me):

In fact you can add it to the layout XML (if inflated by one): android:descendantFocusability=\"blocksDescendants\".

Adding here JIC that webpage is down in the future.



回答3:

If any row item of list contains focusable or clickable view then OnItemClickListener won\'t work.

row item must be having param like 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>


回答4:

Tried many complex solutions, but this was the simplest one that worked:

Just use android:focusable=\"false\" as in:

<CheckBox
    android:id=\"@+id/fav_check_box\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:focusable=\"false\" />


回答5:

Two best solution

  1. Add android:descendantFocusability=\"beforeDescendants\" to listView in xml OR
  2. Set given two attributes to false

like

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

Then it will handle the listView row item child(Button,EditText etc) events instead of listView.setOnItemClick .



回答6:

I fixed my problem different , in my item I have more than one LinearLayout so if you give id to your linearayout and setOnclickListener in adapter class it will work, only original effect of touching will dissapear. but this link Making a LinearLayout act like an Button is usefull to make linearlaout act like button on click

item

<?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>

inside getView method

 @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;
    }