アイテムレイアウトにクリック可能/編集可能なウィジェット(RadioButton、EditText、またはCheckBox)がある場合、ListViewでOnItemClickListenerを使用することは可能ですか?
6 に答える
この問題を確認することをお勧めします。aの行にフォーカス可能なアイテムがあるListView
と、OnItemClickListener
NOTが呼び出されます。ただし、これは、フォーカス可能/クリック可能なアイテムを連続して配置できないことを意味するものではありません。このような回避策がいくつかあります。
また、通話履歴画面もご覧いただけます。クリック可能なListView
アイテム(右側の通話アイコン)があります。
ここでソースコードを参照してください
Samuhが言及したリンクのコメント#31を引用します(これは私にとって問題を解決しました):
実際、これをレイアウトXMLに追加できます(1つ膨らんだ場合):android:descendantFocusability="blocksDescendants"。
ここにJICを追加すると、そのWebページは将来ダウンします。
リストのいずれかの行アイテムにフォーカス可能またはクリック可能なビューが含まれている場合、機能し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>
多くの複雑なソリューションを試しましたが、これは最も簡単なソリューションでした。
次のように使用android:focusable="false"
します。
<CheckBox
android:id="@+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
2つの最良の解決策
android:descendantFocusability="beforeDescendants"
xmlでlistViewに追加または- 指定された2つの属性をに設定します
false
お気に入り
android:focusable="false"
android:focusableInTouchMode="false"
次に、listView.setOnItemClickの代わりにlistView行アイテムの子(Button、EditTextなど)イベントを処理します。
私は別の問題を修正しました。私のアイテムには複数の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;
}