Có thể sử dụng OnItemClickListener trên ListView khi bố cục Mặt hàng có tiện ích con có thể nhấp/chỉnh sửa được (RadioButton, EditText hoặc CheckBox) không?ListView với tiện ích có thể nhấp/chỉnh sửa
Trả lời
Bạn có thể muốn xem this issue. Có một mục có thể đặt tiêu điểm trong một hàng của một ListView
làm cho số OnItemClickListener
KHÔNG được gọi. Tuy nhiên, điều đó không có nghĩa là bạn không thể có các mục có thể có tiêu điểm/có thể nhấp trong một hàng, có một số cách giải quyết như this one.
Ngoài ra, bạn có thể xem qua màn hình Nhật ký cuộc gọi. Nó có một ListView
với mục có thể nhấp (biểu tượng cuộc gọi ở bên phải). See Source code here
Trợ giúp nhanh: "Một cách dễ dàng để giải quyết vấn đề này là gọi " setDescendantFocusability (FOCUS_BLOCK_DESCENDANTS); " trên danh sáchXem chế độ xem khi chúng được thêm. Bạn sẽ có thể chọn hàng, nhấp vào hàng và nhấp vào hộp kiểm và nút con. " - Đây là câu trích dẫn từ nhận xét số 27 trong trang được đề cập bởi hai trong số các liên kết trong câu trả lời ở trên. Cám ơn rất nhiều!! –
Liên kết đến giải pháp 1 bị hỏng. Ai đó có thể chỉnh sửa điều này? – Martze
chỉ liên kết đầu tiên đang hoạt động với những người khác bị hỏng, vui lòng sửa lại – BBdev
Trích dẫn comment # 31 trong liên kết được đề cập bởi Samuh (mà giải quyết được vấn đề đối với tôi):
Trong thực tế, bạn có thể thêm nó vào XML layout (nếu thổi phồng bởi một): android: descendantFocusability = "blocksDescendants".
Thêm vào đây JIC trang web đó sẽ ngừng hoạt động trong tương lai.
Nếu bất kỳ mục hàng nào trong danh sách chứa chế độ xem có thể lấy tiêu điểm hoặc có thể nhấp thì OnItemClickListener
sẽ không hoạt động.
hàng mục phải có param như 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>
thử nhiều giải pháp phức tạp, nhưng nó là một trong những đơn giản nhất mà làm việc:
android: đặt tiêu điểm = "false"
<CheckBox
android:id="@+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
Hai giải pháp tốt nhất
- Thêm
android:descendantFocusability="beforeDescendants"
để ListView trong xml HOẶC - Set cho hai thuộc tính để
false
như
android:focusable="false"
android:focusableInTouchMode="false"
Sau đó, nó sẽ xử lý các con mục ListView hàng (Button, EditText vv) sự kiện thay vì listView.setOnItemClick.
Tôi đã khắc phục sự cố của mình khác nhau, trong mục của tôi có nhiều hơn một LinearLayout vì vậy nếu bạn đặt id cho linearayout và setOnclickListener trong lớp bộ điều hợp thì nó sẽ hoạt động, chỉ có hiệu ứng chạm gốc sẽ biến mất. nhưng liên kết này Making a LinearLayout act like an Button rất hữu ích để làm hành động linearlaout nút như trên nhấp chuột
mục
<?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>
bên trong phương pháp 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;
}
Mở rộng ListActivity thay vì hoạt động. tham khảo http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ –