[英]Custom List Array Adapter is not clickable
當我使用自定義列表數組適配器進行列表視圖時,數據顯示在列表中但不可單擊。 我已經在listview項目上設置了一個監聽器點擊,但它不起作用。
此外,我找到了另一種解決方案,使行可點擊並在線性布局列表視圖項目布局上設置背景可繪制,但這是使用此解決方案的一個壞技巧。
還有其他解決方案嗎?
任何人都可以告訴我為什么會這樣嗎? 在列表視圖中,默認功能不適用於自定義陣列適配器
這是我的代碼:
public class Model {
String name;
String id;
String price;
}
mArrayList = new ArrayList<Model>();
// mArrayList has some data in it
mListiView.setsetAdapter(new myCoustomAdapter(mContext, mArrayList));
mListiView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
}
});
public class myCoustomAdapter extends ArrayAdapter<Model> {
ArrayList<Model> mList;
public myCoustomAdapter(Context context, ArrayList<Model> list) {
super(context, R.layout.item);
this.mList=list;
}
@Override
public int getCount() {
return this.mList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// layout data here
return view;
}
}
單擊項目時沒有任何操作。
首先要注意的是,只要ListView元素中存在Buttons或ImageButtons等可點擊元素,它們就會控制點擊事件。 因此,您的ListView將無法接受click事件。
您只需要為ListView中的Button或ImageButton設置focusable屬性為false。 但是它們仍然可以正常工作,而且ListView的onListItemClick也可以工作。
嘗試這個,
<Button android:id="@+id/textsize_increaser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/back_button"
android:focusable="false"
android:text=" A + "/>
在這里我添加了這個android:focusable =“false”,它工作正常。 試試吧。
這里是鏈接供參考點擊不在Listitem Listview android上
很可能您的列表視圖行的布局包含可點擊的元素,這會“竊取”您的點擊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.