最近做带CheckBox的LISTVIEW的实现,在实现过程中遇到一些问题,也参考了一些网上资料,功能已经实现,在此做个记录,方便大家参考。
首先创建一个工程,然后修改main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/select_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text = "SelectAll" /> <!-- android:listSelector="@android:color/transparent"可以去掉点击时默认的黄色背景 android:divider="@android:color/transparent"可以去掉默认的分割线 这两个设置了后就可以换成自己的背景了。 --> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list" android:layout_alignParentTop="true" /> </LinearLayout> 然后需要自己写一个ListView每项的布局文件,我写了一个简单的listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:textSize="12dip" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/listcb" android:layout_width="30dp" android:layout_height="27dp" android:layout_marginRight="5dip" android:layout_alignParentRight="true" android:gravity="center_vertical" android:focusable="false" android:clickable="false" android:focusableInTouchMode="false" android:button="@android:color/transparent" android:background="@drawable/music_listview_checkbox" /> </RelativeLayout> </LinearLayout>
再呢?需要给ListView自定义一个ADAPTER,ListViewAdapter.java,对于处理CheckBox的地方我有加注释。
package com.wm.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; 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.TextView; public class ListViewAdapter extends BaseAdapter { private List<String> mData = new ArrayList<String>(); // private Context mContext; LayoutInflater mInflater; // 用来控制CheckBox的选中状况 private HashMap<Integer,Boolean> isSelected; public ListViewAdapter(LayoutInflater inflater,Context mcontext,List<String> mdata) { mInflater = inflater; // mContext = mcontext; isSelected = new HashMap<Integer,Boolean>(); mData = mdata; initSelectedMap(); } public void initSelectedMap(){ //默认全部未勾选状态。 for(int i = 0; i< mData.size();++i){ isSelected.put(i, false); } } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mData.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v; ViewTag mViewTag = new ViewTag(); // TODO Auto-generated method stub v = mInflater.inflate(R.layout.listview_item, null); mViewTag.mTextView = (TextView) v.findViewById(R.id.name); String mText = mData.get(position); if(mText != null){ mViewTag.mTextView.setText(mData.get(position)); } else{ mViewTag.mTextView.setText("Unknown name"); } mViewTag.mCheckBox = (CheckBox) v.findViewById(R.id.listcb); //According to the saved status and setChecked status mViewTag.mCheckBox.setChecked(isSelected.get(position)); v.setTag(mViewTag); return v; } public void setListViewData(List<String> mList){ mData = mList; } public void setItemCheckBoxStatus(View mView,int index){ ViewTag holder = (ViewTag) mView.getTag(); // 改变CheckBox的状态 holder.mCheckBox.toggle(); // 将CheckBox的选中状况记录下来 isSelected.put(index, holder.mCheckBox.isChecked()); } public void setAllCheckBoxStatus(Boolean mFlag){ for(int i= 0;i < mData.size(); ++i){ isSelected.put(i, mFlag); } } public class ViewTag{ public TextView mTextView; public CheckBox mCheckBox; } }
最后,更该主页面的代码,这里我多做了一个实现全选与不全选的控制功能。
package com.wm.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; public class ListViewCheckboxActivity extends Activity implements OnClickListener,OnItemClickListener{ private static final String TAG="ListViewCheckboxActivity"; private Button mBtn; private ListView mListView; private ListViewAdapter mListViewAdapter = null; private List<String> mData = new ArrayList<String>(); //control select button status(SelectedAll or UnselectAll) private Boolean mAllSelected = false; private HashMap<Integer,Boolean> mCheckedObj = new HashMap<Integer,Boolean>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtn=(Button) findViewById(R.id.select_btn); mBtn.setOnClickListener(this); mListView = (ListView) findViewById(R.id.list); mListView.setOnItemClickListener(this); InitData(); mListViewAdapter = new ListViewAdapter(getLayoutInflater(),this,mData); mListView.setAdapter(mListViewAdapter); } private void InitData(){ mData.add("aaa"); mData.add("bbb"); mData.add("ccc"); mData.add("ddd"); mData.add("eee"); mData.add("fff"); mData.add("ggg"); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub mListViewAdapter.setItemCheckBoxStatus(arg1, arg2); if(mCheckedObj.containsKey(arg2)){ Log.w(TAG,"****onItemClick***NEED REMOVE***"); mCheckedObj.remove(arg2); } else{ Log.w(TAG,"****onItemClick***NEED put***"); mCheckedObj.put(arg2, true); } if(mCheckedObj.size() == 0){ Log.w(TAG,"****onItemClick**NO CHECKED***"); mAllSelected = false; mBtn.setText("SelectAll"); } else if(mCheckedObj.size() == mData.size()){ mAllSelected = true; mBtn.setText("UnselectAll"); } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.select_btn:{ if(mAllSelected){ mAllSelected = false; mBtn.setText("SelectAll"); mListViewAdapter.setAllCheckBoxStatus(false); mCheckedObj.clear(); } else{ mAllSelected = true; mBtn.setText("UnselectAll"); mListViewAdapter.setAllCheckBoxStatus(true); for(int i=0; i<mData.size();++i){ mCheckedObj.put(i, true); } } //Refresh data mListViewAdapter.notifyDataSetChanged(); } break; default:break; } } }
大功告成,因为代码比较简单,所以没有加太多注释。