码迷,mamicode.com
首页 > 其他好文 > 详细

ListView用CHOICE_MODE_MULTIPLE属性实现多选

时间:2015-07-30 17:06:45      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:listview   多选   

效果图:

技术分享

适配器:

public class CrazyAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;

    public CrazyAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @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, null);
            holder = new ViewHolder();
            holder.view = (TextView) convertView.findViewById(R.id.tv_city);
            holder.box = (CheckBox) convertView.findViewById(R.id.rb_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.view.setText(mList.get(position));
        holder.box.setChecked(((ListView) parent).isItemChecked(position));

        return convertView;
    }


    public final class ViewHolder {
        private TextView view;
        private CheckBox box;
    }

}

页面代码:

public class ListViewActivity extends Activity {
    private ListView mListView;
    private List<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        mListView = (ListView)findViewById(R.id.ls_view);

        String[] mStrs = {"芥川龙之介", "三岛由纪夫", "川端康成", "村上春树", "东野圭吾", "张爱玲", "金庸", "钱钟书", "老舍", "梁实秋", "亨利米勒", "海明威", "菲兹杰拉德", "凯鲁亚克", "杰克伦敦", "小仲马", "杜拉斯", "福楼拜", "雨果", "巴尔扎克", "莎士比亚", "劳伦斯", "毛姆", "柯南道尔", "笛福"};
        mList = Arrays.asList(mStrs);

        final CrazyAdapter mAdapter = new CrazyAdapter(this, mList);
        mListView.setAdapter(mAdapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mAdapter.notifyDataSetChanged();
            }
        });
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

ListView用CHOICE_MODE_MULTIPLE属性实现多选

标签:listview   多选   

原文地址:http://blog.csdn.net/pengkv/article/details/47151023

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!