1. 程式人生 > >CheckBox與ListView結合使用

CheckBox與ListView結合使用

實現效果: 長按LitsView的item時, CheckBox顯示,CheckBox可選中,在這個效果當中可能會有CheckBox被選中時出現混亂的情況,在此記錄一下這個解決辦法.

  1. 先做準備工作: 一個listview 的item行佈局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <TextView
    android:layout_margin="10dp"
    android:id="@+id/tv_item_list_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <TextView
    android:layout_margin="10dp"
    android:layout_toRightOf="@id/tv_item_list_name"
    android:id="@+id/tv_item_list_age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <CheckBox
    android:layout_alignParentRight="true"
    android:layout_margin="10dp"
    android:visibility="gone"
    android:id="@+id/ckb_item_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    </RelativeLayout>
    
  2. listview的adapter

                                                                                                                                            package com.limiao.checkbox;     
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.TextView;
    
    import java.util.List;
    import java.util.zip.Inflater;
    
    /**
     * Created by limiao on 16/11/24.
     */
    
    public class MyAdapter extends BaseAdapter {
    private List<Person> mPersons;
    private Context mContext;
    private boolean mVisibility = false;// checkbox是否顯示,預設不顯示
    public MyAdapter(Context context) {
    mContext = context;
    notifyDataSetChanged();
    }
    
    public void setVisibility(boolean visibility) {
    mVisibility = visibility;
    notifyDataSetChanged();
    }
    
    public void setPersons(List<Person> persons) {
    mPersons = persons;
    notifyDataSetChanged();
    }
    
    @Override
    public int getCount() {
    return mPersons == null ? 0 : mPersons.size();
    }
    
    @Override
    public Object getItem(int position) {
    return mPersons.get(position);
    }
    
    @Override
    public long getItemId(int position) {
    return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Person person = mPersons.get(position);
    holder.mNameTv.setText(person.getName());
    holder.mAgeTv.setText(person.getAge());
    if (mVisibility) {
        holder.mCheckBox.setVisibility(View.VISIBLE);
        final ViewHolder finalHolder = holder;
    
        /**
         * 此處要用setOnClickListener,而不是setOnCheckedChangeListener()
         * 否則會出現混亂的情況
         */
    
        holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                finalHolder.mCheckBox.setChecked(checkBox.isChecked());
            }
        });
    }
    return convertView;
    }
    
    private static class ViewHolder {
    private TextView mNameTv;
    private TextView mAgeTv;
    private CheckBox mCheckBox;
    
    public ViewHolder(View view) {
        mNameTv = (TextView) view.findViewById(R.id.tv_item_list_name);
        mAgeTv = (TextView) view.findViewById(R.id.tv_item_list_age);
        mCheckBox = (CheckBox) view.findViewById(R.id.ckb_item_list);
    }
    

    }

在為CheckBox設定選中狀態時,這裡需要用setOnClickListener(),才不會出現混亂,如果用setOnCheckedChangeListener()就會出現以下的情況

CheckBox的選中狀態出現混亂
3. 一個很簡單的實體類

    package com.limiao.checkbox;
    /**
     * Created by limiao on 16/11/23.
     */

    public class Person  {
    String  name ;
    String age;

    public Person(String name, String age) {
    this.name = name;
    this.age = age;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getAge() {
    return age;
    }

    public void setAge(String age) {
    this.age = age;
    }
    }

4. 最後是MainActivity

    package com.limiao.checkbox;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;

    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {
    private ListView mListView;
    private MyAdapter mMyAdapter;
    private List<Person> mPersons;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.lv_aty_main);
    mPersons = new ArrayList<>();
    for (int i = 0; i < 90; i++) {
        Person person = new Person("aaa","34");
        mPersons.add(person);
    }
    mMyAdapter = new MyAdapter(this);
    mMyAdapter.setPersons(mPersons);
    mListView.setAdapter(mMyAdapter);
    mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            mMyAdapter.setVisibility(true);
            return false;
        }
    });
    }
    }

demo的完整的下載地址