ListView与可点击/可编辑的小部件

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

你可能想看看这个问题 。 在ListView的一行中有一个可OnItemClickListener项目会导致OnItemClickListener不被调用。 但是,这并不意味着您不能连续获得可聚焦/可点击的项目,有一些像这样的解决方法。

另外,您可以查看“通话logging”屏幕。 它有一个带有可点击项目的ListView (右侧的通话图标)。 在这里看到源代码

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

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

在这里添加JIC网页在未来下降。

如果列表中的任何行项目包含可聚焦或可点击的视图,则OnItemClickListener将不起作用。

行项目必须具有类似于android:descendantFocusability="blocksDescendants"参数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> 

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

机器人:可聚焦=“假”

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

两个最佳解决scheme

  1. 在xml中添加android:descendantFocusability="beforeDescendants"到listView
  2. 将给定的两个属性设置为false

喜欢

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

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

我解决了我的问题不同,在我的项目我有多个LinearLayout,所以如果你给你的linearayout id和setOnclickListener在适配器类的id它将起作用,只有触摸的原始效果将消失。 但这个链接使一个LinearLayout像一个button的行为是有用的,使linearlaout像点击button

项目

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