列表多选效果

效果

代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_spinner_up"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorWhite">

    <TextView
        android:id="@+id/tv_spinner_multi"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text=""
        android:textAllCaps="false"
        android:textColor="@color/colorTv" />

    <CheckBox
        android:id="@+id/cb_spinner_multi"
        android:layout_width="13dp"
        android:layout_height="9dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@id/tv_spinner_multi"
        android:background="@drawable/selector_item_type"
        android:button="@null"
        android:checked="true"
        android:clickable="false"
        android:focusable="false"
        android:gravity="center" />

</RelativeLayout>
public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList;
    //用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    public MyAdapter(Context context, List<String> list, HashMap<Integer, Boolean> isSelected) {
        this.mList = list;
        this.mContext = context;
        this.isSelected = isSelected;
        initData();
    }

    //初始化isSelected的数据
    private void initData() {
        for (int i = 0; i < mList.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        String bean = mList.get(position);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_layout, parent, false);
            holder = new ViewHolder();
            holder.tv_spinner_multi = convertView.findViewById(R.id.tv_spinner_multi);
            holder.cb_spinner_multi = convertView.findViewById(R.id.cb_spinner_multi);
            holder.rl_spinner_up = convertView.findViewById(R.id.rl_spinner_up);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_spinner_multi.setText(bean);
        //监听checkBox并根据原来的状态来设置新的状态
        holder.rl_spinner_up.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (isSelected.get(position)) {
                    isSelected.put(position, false);
                    setIsSelected(isSelected);
                } else {
                    isSelected.put(position, true);
                    setIsSelected(isSelected);
                }
                notifyDataSetChanged();
            }
        });

        //根据选中情况设置字体颜色
        if (isSelected.get(position)) {
            holder.tv_spinner_multi.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
        } else {
            holder.tv_spinner_multi.setTextColor(mContext.getResources().getColor(R.color.colorTv));
        }

        // 根据isSelected来设置checkbox的选中状况
        holder.cb_spinner_multi.setChecked(getIsSelected().get(position));
        return convertView;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    private static class ViewHolder {
        private TextView tv_spinner_multi;
        private CheckBox cb_spinner_multi;
        private RelativeLayout rl_spinner_up;
    }
}
private String area[] = {"全部", "北京", "上海", "广州", "深圳", "郑州", "南京"};
private HashMap<Integer, Boolean> isSelectedArea = new HashMap<Integer, Boolean>();

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	ListView listview = (ListView) findViewById(R.id.listview);
	MyAdapter myAdapter = new MyAdapter(this, Arrays.asList(area), isSelectedArea);
	listview.setAdapter(myAdapter);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值