我无法在Android中单击ListView?

Ama*_*ath 7 android listview android-listview

我使用联系人同步创建了一个应用.我列出了以下联系信息,包括照片,姓名和号码.我成功地在自定义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");
Run Code Online (Sandbox Code Playgroud)

这里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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有超过100个联系人,所以只添加了3个对象.在此联系人列表中,我成功收到联系人图片,姓名,电话号码.

ListView无法单击的问题是什么?我希望你们中的任何一个人能指导我.提前致谢.


谢谢大家.现在通过添加android:focusable="false"我的所有子视图得到结果.谢谢你的指导.

the*_*osh 9

在嵌套视图中,子视图始终首先获取所有触摸事件.如果您想要父视图(在您的情况下是listView行),要获取触摸事件,您必须在子事件上返回false或将它们设置android:clickable="false"为清单中.


Kha*_*han 8

 android:focusable="false"  
Run Code Online (Sandbox Code Playgroud)

 android:clickable="false"
Run Code Online (Sandbox Code Playgroud)

对于每个子视图,如imageview,textview,checkbox等...你的行布局意味着在manage_track_list_custom_view.xml中