ListView带有可点击/可编辑的小部件

时间:2010-01-20 01:51:59

标签: android android-widget

当Items布局有可点击/可编辑的小部件(RadioButton,EditText或CheckBox)时,是否可以在ListView上使用OnItemClickListener?

6 个答案:

答案 0 :(得分:68)

您可能需要查看this issue。在ListView的行中具有可聚焦项目会导致OnItemClickListener NOT不被调用。但是,这并不意味着您不能连续使用可聚焦/可点击的项目,而是有一些解决方法,例如this one

此外,您还可以查看“呼叫记录”屏幕。它有一个ListView可点击的项目(右侧的呼叫图标)。 See Source code here

答案 1 :(得分:20)

在Samuh提到的链接中引用评论#31(为我解决了这个问题):

  

实际上你可以将它添加到布局XML(如果被一个膨胀):android:descendantFocusability =“blocksDescendants”。

在此处添加JIC,以防将来网页停用。

答案 2 :(得分:11)

如果列表中的任何行项目包含可关注或可点击的视图,则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>

答案 3 :(得分:3)

尝试了许多复杂的解决方案,但这是最简单的解决方案:

只需使用android:focusable="false",如下所示:

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

答案 4 :(得分:0)

两个最佳解决方案

  1. android:descendantFocusability="beforeDescendants"添加到listView 在xml中 OR
  2. 将两个属性设置为false
  3.    android:focusable="false"
       android:focusableInTouchMode="false"
    

    然后它将处理listView行项目子项(Button,EditText等)而不是listView.setOnItemClick。

答案 5 :(得分:0)

我修复了我的问题,在我的项目中我有多个LinearLayout 因此,如果你在适配器类中为你的linearayout和setOnclickListener提供id它将起作用,只有触摸的原始效果会消失。 但是这个链接Making a LinearLayout act like an Button对于使linelaout像点击按钮

一样有用

项目

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