Multiple choice list with custom view?

后端 未结 9 755
失恋的感觉
失恋的感觉 2020-12-02 09:07

I\'ve seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_

相关标签:
9条回答
  • 2020-12-02 09:27

    Simple example how to get a custom layout to work as custom checkbox:

    private class FriendsAdapter extends ArrayAdapter<WordsterUser> {
        private Context context;
    
        public FriendsAdapter(Context context) {
            super(context, R.layout.listitem_oneline);
    
            this.context = context;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final int pos = position;
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View rv = inflater.inflate(R.layout.listitem_oneline, parent, false);
            rv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = friendsListView.isItemChecked(pos); 
                    friendsListView.setItemChecked(pos, !checked);
                }
            });
    
            WordsterUser u = getItem(position);
    
            TextView itw = (TextView) rv.findViewById(R.id.ItemTextView);
            itw.setText(u.userName + " (" + u.loginName + ")");
    
            ImageView iv = (ImageView) rv.findViewById(R.id.SelectButton);
    
            if (friendsListView.isItemChecked(position)) {
                iv.setImageResource(R.drawable.downbutton);
            } else {
                iv.setImageResource(R.drawable.upbutton);
            }
    
            return rv;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 09:29

    I want to confirm that the Pritam's answer is correct. You need an onClickListener on each list's item (define it in the adapter's getView()).

    You can create a new onClickListener() for each item, or have the adapter implement onClickListener() - in this case the items must be tagged for the listener to know, which item it is operating on.

    Relying on the list onItemClickListener() - as someone advised in another thread - will not work as the CheckBox will intercept the click event so the list will not get it.

    And finally @Rahul and JVitella:

    The situation is that the CheckBox on a list item must be clickable and checkable independently from the list item itself. Therefore the solution is as I just described above.

    0 讨论(0)
  • 2020-12-02 09:31

    Each such view has a TextView and a CheckBox.

    No, it doesn't. It has a CheckedTextView.

    So what do I need to do to make a multiple choice list with my custom view for each row?

    Try making the CheckBox android:id value be "@android:id/text1" and see if that helps. That is the ID used by Android for the CheckedTextView in simple_list_item_multiple_choice.

    0 讨论(0)
提交回复
热议问题