![](/img/trans.png)
[英]Why can't I click a Button in a ListView using SimpleAdapter in android?
[英]I can't click the ListView in android?
我使用聯系人同步創建了一個應用。 我列出了以下聯系信息,包括照片,姓名和號碼。 我成功地在自定義ListView中列出了所有這些東西,但我無法單擊ListView 。 它看起來像鎖定,無法點擊它。
但我對另一項活動做了同樣的程序。 使用自定義ListView,但我可以單擊此視圖,它工作正常。
問題是什么? 這是我的示例編碼:
ListView settingsList = (ListView) findViewById(R.id.manage_track_listView);
ArrayList<ContactList> MySettingsList = new ArrayList<ContactList>();
ContactList setting1 = new ContactList("contact name 1", "Number 1", null);
ContactList setting2 = new ContactList("contact name 2", "Number 2", null);
ContactList setting3 = new ContactList("contact name 3", "Number 3", null);
MySettingsList.add(setting1);
MySettingsList.add(setting2);
MySettingsList.add(setting3);
ContactList list[] = new ContactList[MySettingsList.size()];
for(int i=0;i<MySettingsList.size();i++) {
ContactList mySettings = MySettingsList.get(i);
list[i] = new ContactList(mySettings.getName(), mySettings.getNumber(), mySettings.getImageIcon());
}
ContactListAdapter adapter = new ContactListAdapter(this, R.layout.manage_track_list_custom_view, list);
settingsList.setAdapter(adapter);
System.out.println("before listener");
settingsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("Clicked " + position);
}
});
System.out.println("after listener");
這里ContactList是一個類,它有imageBlob的聯系人姓名,號碼和byte []。 如果圖像為空,我將默認ic_launcher設置為聯系人圖像。 適配器類是:
public class ContactListAdapter extends ArrayAdapter<ContactList> {
Context context;
int layoutResourceId;
ContactList objects[] = null;
View row;
public ContactListAdapter(Context context, int layoutResourceId, ContactList[] objects) {
super(context, layoutResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.layoutResourceId = layoutResourceId;
this.objects = objects;
System.out.println(objects[1].getName());
System.out.println(objects[1].getNumber());
System.out.println(objects[1].getImageIcon());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
row = convertView;
final ContactListHolder holder;
if ( row == null ) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactListHolder();
holder.image = (ImageView) row.findViewById(R.id.contactImage);
holder.name = (TextView) row.findViewById(R.id.contactName);
holder.number = (TextView) row.findViewById(R.id.contactNumber);
holder.check = (CheckBox) row.findViewById(R.id.selectedContact);
row.setTag(holder);
} else {
holder = (ContactListHolder)row.getTag();
}
ContactList contact = objects[position];
if(contact.imageIcon != null) {
Bitmap imgBitmap = BitmapFactory.decodeByteArray(contact.imageIcon, 0, contact.imageIcon.length);
holder.image.setImageBitmap(imgBitmap);
} else {
holder.image.setImageResource(R.drawable.ic_launcher);
}
holder.name.setText(contact.name);
holder.number.setText(contact.number);
holder.check.setChecked(objects[position].isSelected());
return row;
}
static class ContactListHolder {
ImageView image;
TextView name;
TextView number;
CheckBox check;
}
}
我有超過100個聯系人,所以只添加了3個對象。 在此聯系人列表中,我成功收到聯系人圖片,姓名,電話號碼。
ListView無法單擊的問題是什么? 我希望你們中的任何一個人能指導我。 提前致謝。
謝謝大家。 現在通過在我的所有子視圖中添加android:focusable="false"
來獲得結果。 謝謝你的指導。
在嵌套視圖中,子視圖始終首先獲取所有觸摸事件。 如果你想要父視圖(在你的情況下是listView行),要獲得觸摸事件,你必須在子事件上返回false或者在清單中將它們設置為android:clickable="false"
。
加
android:focusable="false"
和
android:clickable="false"
對於每個子視圖,如imageview,textview,checkbox等...你的行布局意味着在manage_track_list_custom_view.xml中
我認為你必須設置所有可點擊的東西,例如。 復選框,按鈕等不可聚焦(在適配器類中)。
holder.yourButton.setFocusable(false);
holder.yourButton.setFocusableInTouchMode(false);
holder.yourCheckbox.setFocusable(false);
holder.yourCheckbox.setFocusableInTouchMode(false);
您可以將onclick listner設置為ContactListAdapter類中的View行。
holder.check.setChecked(objects[position].isSelected());
row.setOnclickListner(new Onclicklistner(){
// Your code here
});
return row;
當我無法在列表視圖中單擊某個項目的其他部分時,我遇到同樣的問題,只需單擊該項目內的textview或imageview即可。 所以我設置了我的textview:
android:layout_width="fill_parent"
我的項目是這樣的:
<relative>
[image][textview]
relative/>
點擊事件工作得很好,因為我點擊了textview。 希望能幫助任何人有同樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.