本文主要是介绍listview进行item多选、全选、反选操作。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<pre name="code" class="html"> Context context;List<MineCollectionEntity.BodyBean.ElementsBean> list;boolean isCheck;// 用来控制CheckBox的选中状况private static HashMap<Integer, Boolean> isSelected;public CollectionListAdapter(Context context, List<MineCollectionEntity.BodyBean.ElementsBean> list, boolean isCheck) {this.context = context;this.list = list;this.isCheck = isCheck;isSelected = new HashMap<Integer, Boolean>();// 初始化数据initDate();}// 初始化isSelected的数据public void initDate() {isSelected = new HashMap<Integer, Boolean>();for (int i = 0; i < list.size(); i++) {getIsSelected().put(i, false);}}public static HashMap<Integer, Boolean> getIsSelected() {return isSelected;}public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {CollectionListAdapter.isSelected = isSelected;}public void setIsCheck(boolean isCheck) {this.isCheck = isCheck;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;if (convertView == null) {viewHolder = new ViewHolder();convertView = View.inflate(context, R.layout.item_coll, null);viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);viewHolder.isCheakBox = (CheckBox) convertView.findViewById(R.id.isCheakBox);viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);viewHolder.tv_jiangshi = (TextView) convertView.findViewById(R.id.tv_jiangshi);viewHolder.tv_fenlei = (TextView) convertView.findViewById(R.id.tv_fenlei);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}MineCollectionEntity.BodyBean.ElementsBean bean = list.get(position);if (!TextUtils.isEmpty(bean.getCourseImgPath())) {Picasso.with(context).load(bean.getCourseImgPath()).placeholder(R.drawable.home_top).error(R.drawable.home_top).into(viewHolder.iv_icon);}if (isCheck) {viewHolder.isCheakBox.setVisibility(View.VISIBLE);viewHolder.isCheakBox.setChecked(getIsSelected().get(position));} else {viewHolder.isCheakBox.setVisibility(View.GONE);}final ViewHolder finalViewHolder = viewHolder;convertView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finalViewHolder.isCheakBox.setChecked(!finalViewHolder.isCheakBox.isChecked());getIsSelected().put(position, finalViewHolder.isCheakBox.isChecked());}});viewHolder.isCheakBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {getIsSelected().put(position, isChecked);}});viewHolder.tv_time.setText(bean.getDuration());viewHolder.tv_title.setText(bean.getCourseName());viewHolder.tv_jiangshi.setText(bean.getAuthorName());return convertView;}class ViewHolder {ImageView iv_icon;TextView tv_time, tv_title, tv_jiangshi, tv_fenlei;CheckBox isCheakBox;}
将点击的item的position保存到map中。
<pre name="code" class="html"> @OnClick(R.id.tv_all)public void allClick() {isAll = !isAll;if (isAll) {tv_all.setText("反选");for (int i = 0; i < list.size(); i++) {CollectionListAdapter.getIsSelected().put(i, true);}} else {for (int i = 0; i < list.size(); i++) {tv_all.setText("全选");if (CollectionListAdapter.getIsSelected().get(i)) {CollectionListAdapter.getIsSelected().put(i, false);} else {CollectionListAdapter.getIsSelected().put(i, true);}}}adapter.notifyDataSetChanged();}
<span style="font-size:18px;"><strong>通过点击事件改编listview的显示。</strong></span>
这篇关于listview进行item多选、全选、反选操作。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!