`
aliusa
  • 浏览: 84263 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

selectAll___CheckBox in ListView

阅读更多
1.如果你有一个ListView,listView的 adapter 是自定义的,并且item中包含一个CheckBox 跟其他的一些组件,
  如果你想通过额外一的一个CheckBox来实现select All 跟dis select all 以及 手动 选择的话,可参照下面的实现方式。
merge_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="4dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_alignParentLeft="true">

        <RelativeLayout
            android:id="@+id/avatarLayout_1"
            android:layout_width="60dip"
            android:layout_height="60dip"
            android:background="@drawable/frame">
            <ImageView
                android:id="@+id/avatar_1"
                android:layout_width="60dip"
                android:layout_height="60dip"
                android:layout_alignParentLeft="true"
                android:src="@drawable/default_avatar" />

            <ImageView
                android:id="@+id/sns_type_icon_1"
                android:layout_width="25dip"
                android:layout_height="25dip"
                android:paddingRight="5dip"
                android:paddingLeft="3dip"
                android:layout_alignBottom="@id/avatar_1"
                android:layout_alignRight="@id/avatar_1"
                 />
        </RelativeLayout>


        <TextView
            android:layout_below="@+id/item"
            android:layout_toRightOf="@+id/avatarLayout_1"
            android:layout_centerVertical="true"
            android:paddingLeft="1dip"
            android:id="@+id/name_1"
            android:layout_width="60dip"
            android:layout_height="wrap_content"
            android:textSize="18dip"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="Contact1" />
    </LinearLayout>
    <CheckBox
        android:id="@+id/link"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_alignParentRight="true">
        <TextView
            android:layout_below="@+id/item"
            android:layout_centerVertical="true"
            android:paddingRight="1dip"
            android:id="@+id/name_2"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:textSize="18dip"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="Contact2" />

        <RelativeLayout
            android:id="@+id/avatarLayout_2"
            android:layout_width="60dip"
            android:layout_height="60dip"
            android:background="@drawable/frame">
            <ImageView
                android:id="@+id/avatar_2"
                android:layout_width="60dip"
                android:layout_height="60dip"
                android:layout_alignParentLeft="true"
                android:src="@drawable/default_avatar" />

            <ImageView
                android:id="@+id/sns_type_icon_2"
                android:layout_width="25dip"
                android:layout_height="25dip"
                android:paddingRight="5dip"
                android:paddingLeft="3dip"
                android:layout_alignBottom="@id/avatar_2"
                android:layout_alignRight="@id/avatar_2"
                />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>


MergeListActivity.java
public class MergeListActivity extends Activity{
    public MergeListAdapter mAdapter;
    ListView mergeListView;
    
    

    
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.merge_list);
        initMergeList();
        super.onCreate(savedInstanceState);
    }
  
    
    public void initMergeList(){
        MyLog.println("MergeListActivity.initMergeList");
        
        // get the list view
        final ListView mergeListView = ((ListView) findViewById(R.id.merge_list));
        mAdapter = new MergeListAdapter(this);
        //add header
        if (mergeListView.getHeaderViewsCount() == 0){
            final RelativeLayout header = (RelativeLayout) LayoutInflater.from(MergeListActivity.this) .inflate(R.layout.merge_list_header, null);
            mergeListView.addHeaderView(header);
            final CheckBox selectAll = (CheckBox)header.findViewById(R.id.selectedAll);
            selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    //mergeListView.setSelected(true);
                   mAdapter.setAllChecked(isChecked);
                }
            });
            
            final Button cancel = (Button)findViewById(R.id.btn_cancel);
            cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   finish();
                }
            });
            
            final Button ok = (Button)findViewById(R.id.btn_ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   mAdapter.autoMergeSelectItems();
                   finish();
                }
            });
        }
        
        mergeListView.setAdapter(mAdapter);

    }

   }


MergeListAdapter.java
public class MergeListAdapter extends BaseAdapter {  
    private Context mContext;
    public static  ArrayList<SimpleContact> similar_1;
    public static  ArrayList<SimpleContact> similar_2;
    public static LayoutInflater inflater = null;
    private ArrayList<Boolean> checkBoxStateList = new ArrayList<Boolean>(); 
    private ArrayList<CheckBox> ctv = new ArrayList<CheckBox>(); 
   
    
    public MergeListAdapter(Context context) {
        mContext = context;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
        for(int i = 0; i < similar_1.size(); i++){         
            checkBoxStateList.add(i,false);    
        }     

    } 
    public int getCount() {     
        return similar_1.size(); 
    }  
    public Object getItem(int position) {     
        return position; 
    } 
    public long getItemId(int position) {   
        return position; 
    } 
    
    private void onClickItem(int position){
        MyLog.println("MergeListActivity.onClickItem");
        final int contactID1 = Integer.parseInt(similar_1.get(position).contactID);
        final int contactID2 =  Integer.parseInt(similar_2.get(position).contactID);
        Intent itent = new Intent(mContext,MergeDetailActivity.class);
        itent.putExtra(IntentExtraName.EXTRA_PRIMARY_CONTACT_ID_NAME, contactID1);
        itent.putExtra(IntentExtraName.EXTRA_SECONDARY_CONTACT_ID_NAME, contactID2);
        mContext.startActivity(itent);
    }
    
    public static class ViewHolder{     
         public ImageView avatar_1;  
         public ImageView avatar_2;
         public TextView name_1;     
         public TextView name_2;  
         public ImageView snsTypeIcon_1;  
         public ImageView snsTypeIcon_2; 
         public CheckBox checkBox;
    }  
    public View getView(
         final int position, View convertView, ViewGroup parent) {    
         View row = convertView;   
         ViewHolder holder;     
         if(convertView==null)     {       
             row = inflater.inflate(R.layout.merge_list_item, parent, false);       
             holder = new ViewHolder();   
             holder.avatar_1 = (ImageView)row.findViewById(R.id.avatar_1);   
             holder.name_1 = (TextView)row.findViewById(R.id.name_1);  
             holder.snsTypeIcon_1 = (ImageView)row.findViewById(R.id.sns_type_icon_1);
             holder.checkBox = (CheckBox)row.findViewById(R.id.link);
             holder.avatar_2 = (ImageView)row.findViewById(R.id.avatar_2);   
             holder.name_2 = (TextView)row.findViewById(R.id.name_2); 
             holder.snsTypeIcon_2 = (ImageView)row.findViewById(R.id.sns_type_icon_2);
             holder.checkBox.setTag(position);        
             row.setTag(holder);    
         }else{        
                 holder = (ViewHolder)row.getTag();   
         }      
         
         
         
         final SimpleContact contact1 = similar_1.get(position);
         final SimpleContact contact2 = similar_2.get(position);
         final ContentResolver cr = mContext.getContentResolver();
         final Bitmap avater_1 = DataOperations.getAvatar(Long.parseLong(contact1.contactID), cr);
         final String name_1 = contact1.displayName; 
         
         final Bitmap avater_2 = DataOperations.getAvatar(Long.parseLong(contact2.contactID),cr );   
         final String name_2 = contact2.displayName;
           
         //avatar
         if (avater_1 != null){
             holder.avatar_1.setImageBitmap(avater_1);
         }else{
             holder.avatar_1.setBackgroundResource(R.drawable.default_avatar);
         }
         if (avater_2 != null){
             holder.avatar_2.setImageBitmap(avater_2);
         }else{
             holder.avatar_2.setBackgroundResource(R.drawable.default_avatar);
         }
         
         //name
         holder.name_1.setText(name_1);
         holder.name_2.setText(name_2);
         
         //icon of snsType
         if (RichContactConfig.CONTACT_TYPE_FACEBOOK == Integer.parseInt(contact1.contactType)){
             holder.snsTypeIcon_1.setImageResource(R.drawable.facebook_s);
         }else if (RichContactConfig.CONTACT_TYPE_SINA == Integer.parseInt(contact1.contactType)){
             holder.snsTypeIcon_1.setImageResource(R.drawable.sina_s);
         }
         if (RichContactConfig.CONTACT_TYPE_FACEBOOK == Integer.parseInt(contact2.contactType)){
             holder.snsTypeIcon_2.setImageResource(R.drawable.facebook_s);
         }else if (RichContactConfig.CONTACT_TYPE_SINA == Integer.parseInt(contact2.contactType)){
             holder.snsTypeIcon_2.setImageResource(R.drawable.sina_s);
         }
         
         ctv.add(holder.checkBox);    
         holder.checkBox.setChecked(checkBoxStateList.get(position));    
         holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){              
                    checkBoxStateList.set(position, true);              
                 } else {            
                     checkBoxStateList.set(position,false);        
                 } }
            });         
    
           
         final int index = position;
         row.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               onClickItem(index);
              
           }
         });
         
         row.setId(position);
         return row;
     } 
    
    
    
     public boolean isChecked(int position) {   
         return checkBoxStateList.get(position); 
     }
   
     public void setAllChecked(boolean isChecked) {  
         if(isChecked) {      
             for(int i = 0; i < ctv.size(); i++){    
                 ctv.get(i).setChecked(true);  
              }  
         }else {   
             for(int i = 0; i < ctv.size(); i++) {        
                 ctv.get(i).setChecked(false);  
             }    
         }
     }

    public ArrayList<Boolean> getCheckBoxStateList() {
        return checkBoxStateList;
    }
     
    public void autoMergeSelectItems(){
        MyLog.println("MergeListActivity.autoMergeSelectItems");
        
    }
    
       
     }


实现效果:
全选


全不选


全选后再手动去除几个


  • 大小: 205.6 KB
  • 大小: 138.9 KB
  • 大小: 210.2 KB
分享到:
评论

相关推荐

    Android ListView CheckBox

    Button selectAllBtn = findViewById(R.id.button_select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 全选/全不选的逻辑 for (int i = 0...

    android ListView中的checkBox全选和反选Demo

    void selectAll() { for (int i = 0; i (); i++) { selectedItems[i] = true; } adapter.notifyDataSetChanged(); } // 反选 void reverseSelect() { for (int i = 0; i (); i++) { selectedItems[i] = !...

    listview内嵌checkbox的批处理

    Button selectAll = findViewById(R.id.select_all); selectAll.setOnClickListener(v -&gt; { for (Item item : items) { item.setSelected(true); } adapter.notifyDataSetChanged(); // 通知适配器数据已更新 })...

    Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)

    在适配器中,我们可以添加两个方法:`selectAll()`和`deselectAll()`,分别用于设置所有CheckBox为选中和未选中状态。同时,我们还需要监听CheckBox的状态变化,当用户点击CheckBox时更新数据源的状态。 为了实现...

    ListView全/反选/删除所选,状态切换显示/隐藏CheckBox

    在`selectAll()`方法中,我们可以检查当前项的CheckBox状态,如果已选中则将其设为未选中,反之亦然。 3. **删除所选**:删除所选项需要先获取选中项的列表。在ListView中,我们可以监听CheckBox的...

    listview带checkbox全选、取消功能

    Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { adapter....

    android UI进阶之实现listview中checkbox的多选与记录

    findViewById(R.id.selectAll).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (MyData item : data) { item.setSelected(true); } notifyDataSetChanged(); ...

    列表ListView嵌入CheckBox.zip

    Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { MyListAdapter...

    用ListView+CheckBox+Menu实现全选,删除等效果

    本教程将详细讲解如何利用ListView、CheckBox和Menu来实现全选、反选以及删除选中项的功能。 **1. ListView的基本使用** ListView的使用通常涉及到以下步骤: - 在布局文件中添加ListView控件,设置其ID。 - 创建一...

    listview带可全选全不选删除checkbox

    selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { selectedItems[i] = !selectedItems[i]; } notifyDataSetChanged(); } ...

    ListView中的checkBox全选和反选Demo

    Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i ; i++) { checkedItems[i]...

    ListView+CheckBox UI 完美版。实现 全选 、 全不选 、 删除等功能.zip

    selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { for (int i = 0; i (); i++)...

    Android Checkbox多选全选

    selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { checkBoxStates[i] = true; // 更新ListView或RecyclerView中的Checkbox...

    SelectAll全选

    bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show...

    CheckBox实现多选列表,并实现全选、反选功能

    selectAll.setOnClickListener(v -&gt; adapter.setAllChecked(true)); Button invert = findViewById(R.id.btn_invert_selection); invert.setOnClickListener(v -&gt; adapter.invertSelection()); ``` 这样,我们就...

    listtview绑定checkbox

    Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isAllSelected = !isAllSelected; for (int i...

    listview check 全选

    selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i ; i++) { checkedItems[i] = true; } adapter.notifyDataSetChanged(); // 更新视图...

    CheckBoxList全选反选添加删除

    void selectAll() { isAllChecked = true; for (int i = 0; i (); i++) { getItem(i).setChecked(isAllChecked); } notifyDataSetChanged(); } // 反选 void unselectAll() { isAllChecked = false; for ...

    Android ListView支持批量选择

    public void selectAll() { for (MyItem item : dataList) { item.setSelected(true); } notifyDataSetChanged(); } public void deselectAll() { for (MyItem item : dataList) { item.setSelected(false); ...

    Android中ListView结合CheckBox实现数据批量选择(全选、反选、全不选)

    public void selectAll() { for (int i = 0; i (); i++) { getItem(i).setSelected(true); notifyDataSetInvalidated(); } } // 反选 public void invertSelection() { for (int i = 0; i (); i++) { getItem...

Global site tag (gtag.js) - Google Analytics