Multiple choice list with custom view?

后端 未结 9 754
失恋的感觉
失恋的感觉 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:12

    Got the solution ... You can get the clicks on the views (like checkboxes in custom layouts of row) by adding listener to each of them in the adapter itself while you return the converted view in getView(). You may possibly have to pass a reference of list object if you intent to get any list specific info. like row id.

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

    It is possible by some trick

    in your ListActivtyClass in method

    protected void onListItemClick(ListView l, View v, int position, long id) {
    //just set
    <your_model>.setSelected(true);
    }
    

    now in you custom Adapter

    public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(textViewResourceId, parent, false);
            }       
            if (<your_model>.isSelected()) {
                convertView.setBackgroundColor(Color.BLUE);
            } else {
                convertView.setBackgroundColor(Color.BLACK);
            }
            return convertView;
        }
    

    this way you can customize the view in adapter when the item is selected in the list.

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

    I found it very useful this little code: http://alvinalexander.com/java/jwarehouse/apps-for-android/RingsExtended/src/com/example/android/rings_extended/CheckableRelativeLayout.java.shtml

    It is a great addition to @ferdy182 's http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/ content.

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

    The solution is to create a custom View that implements the Clickable interface.

    public class OneLineCheckableListItem extends LinearLayout implements Checkable {
    
        public OneLineCheckableListItem(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        private boolean checked;
    
    
        @Override
        public boolean isChecked() {
            return checked;
        }
    
        @Override
        public void setChecked(boolean checked) {
            this.checked = checked; 
    
            ImageView iv = (ImageView) findViewById(R.id.SelectImageView);
            iv.setImageResource(checked ? R.drawable.button_up : R.drawable.button_down);
        }
    
        @Override
        public void toggle() {
            this.checked = !this.checked;
        }
    }
    

    And create a custom layout for the list items using the new widget.

    <?xml version="1.0" encoding="utf-8"?>
    <ax.wordster.OneLineCheckableListItem xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:background="@drawable/selector_listitem"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/SelectImageView"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/button_friends_down" />
    
        <TextView
            android:id="@+id/ItemTextView"
            android:layout_width="fill_parent"
            android:layout_height="60dp"
            android:gravity="center"
            android:text="@string/___"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/text_item" />
    
    </ax.wordster.OneLineCheckableListItem>
    

    Then create a new custom Adapter using the layout above.

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

    The answer of Rahul Garg is good for the first time the list is loaded, if you want some rows to be checked depending on the model data, but after that you have to handle the check/uncheck events by yourself.

    You can override the onListItemCLick() of the ListActivity to check/uncheck the rows

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        ViewGroup row = (ViewGroup)v;
     CheckBox check = (CheckBox) row.findViewById(R.id.checkbox);            
        check.toggle();
    }
    

    If you do so, do not set the ListView to CHOICE_MODE_MULTIPLE, because it makes strange things when calling the function.

    To retrieve the list of checked rows, you have to implement a method yourself, calling getCheckItemIds() on the ListView does not work:

    ListView l = getListView();
    int count = l.getCount();
    for(int i=0; i<count; ++i) {
       ViewGroup row = (ViewGroup)l.getChildAt(i);
       CheckBox check = (Checked) row.findViewById(R.id.ck1);
       if( check.isChecked() ) {
          // do something
       }
    }
    
    0 讨论(0)
  • 2020-12-02 09:24

    You have to make your own RelativeLayout that implements the Checkable interface and have a reference to the CheckBox or to the CheckedTextView (or a list if it's multiple choice mode).

    Look at this post: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

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