先说布局
布局也就一个listview + 一个button,然后item的布局如下
<?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">
<LinearLayout
android:id="@+id/ll"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray_33"
android:layout_marginRight="10dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_description"
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="14sp"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray_33"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
lv_data.setAdapter(new ServiceAdapter(Main.this,initData()));
ServiceAdapter
public class ServiceAdapter extends BaseAdapter {
private List<ServiceItem.ServicesBean> mDataset;
private Context context;
public LinkedHashMap<Integer, ServiceItem.ServicesBean> cbMap = new LinkedHashMap<>();
// 用来控制CheckBox的选中状况
private LinkedHashMap<Integer, Boolean> isSelected;
private StringBuilder sb = new StringBuilder();
public ServiceAdapter(Context context, List<ServiceItem.ServicesBean> mDataset) {
this.mDataset = mDataset;
this.context = context;
isSelected = new LinkedHashMap<>();
initDate();
}
private void initDate() {
for (int i = 0; i < mDataset.size(); i++) {
isSelected.put(i, false);
}
}
@Override
public int getCount() {
return mDataset.size();
}
@Override
public Object getItem(int position) {
return mDataset.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int index = position;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_service, null);
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
viewHolder.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.cb.setChecked(isSelected.get(position));
viewHolder.tv_name.setText(mDataset.get(position).getName());
viewHolder.tv_description.setText(mDataset.get(position).getIntroduce());
viewHolder.cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isSelected.get(position)) {
isSelected.put(position, false);
cbMap.remove(position);
} else {
isSelected.put(position, true);
cbMap.put(position, mDataset.get(position));
}
if(cbMap.size() > 4){
viewHolder.cb.toggle();
ToastUtils.showToast((Activity)context,"最多选择四个");
cbMap.remove(position);
isSelected.put(position, false);
}
}
});
return convertView;
}
class ViewHolder {
public CheckBox cb;
public TextView tv_name;
public TextView tv_description;
}
//供activity获取选中的数据
public List<ServiceItem.ServicesBean> getSelectedData(){
if(cbMap.size() > 0){
List<ServiceItem.ServicesBean> mSelectedData = new ArrayList<>();
for (Integer key : cbMap.keySet()) {
mSelectedData.add(mDataset.get(key));
}
return mSelectedData;
}else{
return null;
}
}
}