多选recycleview+ checkbox(原理是用一个集合记录每个条目的状态)
来源:互联网 发布:淘宝怎么看卖家网址 编辑:程序博客网 时间:2024/05/19 20:41
//只写一个adpter吧
public class CouponShowRecycleViewAdapter extends RecyclerView.Adapter<CouponShowRecycleViewAdapter.ViewHolder> implements View.OnClickListener {
public static final int TYPE_HEADER = 0; //说明是带有Header的
public static final int TYPE_NORMAL = 1; //说明是不带有header和footer的
private View mHeaderView;
Context context;
List<CouponShowMode.ResultBean.CouponListBean> list;
private LayoutInflater inflater;
private HashMap<String, Boolean> states = new HashMap<>();//记录所有radiobutton被点击的状态,主要原理是用一个集合记录每个状态
private OnItemClickListener mOnItemClickListener = null;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public CouponShowRecycleViewAdapter(Context context, List<CouponShowMode.ResultBean.CouponListBean> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
//HeaderView的get和set函数
public View getHeaderView() {
return mHeaderView;
}
public void setHeaderView(View headerView) {
mHeaderView = headerView;
notifyItemInserted(0);
}
@Override
public int getItemViewType(int position) {
// return super.getItemViewType(position);
if (mHeaderView == null) {
return TYPE_NORMAL;
}
if (position == 0) {
return TYPE_HEADER;
}
return TYPE_NORMAL;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mHeaderView != null && viewType == TYPE_HEADER) {
return new CouponShowRecycleViewAdapter.ViewHolder(mHeaderView);
}
View view = inflater.inflate(R.layout.item_coupom_gonging_checkbox, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setOnClickListener(this);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
L.e("positon"+position);
if(position == 0){
return;
}
int position1 = position - 1;
holder.itemView.setTag(position1);
holder.cashcount.setText(list.get(position1).getTitle());
holder.lookDetails.setVisibility(View.GONE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
holder.time.setText(sdf.format(Long.valueOf(list.get(position1).getBegin_timestamp()) * 1000L) + "-" + sdf.format(Long.valueOf(list.get(position1).getEnd_timestamp()) * 1000L));
String allow_superposition = list.get(position1).getAllow_superposition();//是否允许叠加使用 1允许
String class_id = list.get(position1).getClass_id();
if (class_id.equals("4")) {
holder.cash.setText("¥" + list.get(position1).getWorth());
holder.or.setImageResource(R.mipmap.daijinquanicon1);
} else if (class_id.equals("2")) {//折扣券
holder.cash.setText(list.get(position1).getWorth() + "折");
holder.or.setImageResource(R.mipmap.zhekouquanicon);
}
Picasso.with(context).load(URLUtil.HOST + list.get(position1).getThumb()).placeholder(R.mipmap.morenicon).into(holder.imageview);
boolean res = false;
if (getStates(position1) == null || !getStates(position1))//checkbox状态
{
res = false;
setStates(position1, false);
} else {
res = true;
}
holder.checkBox.setChecked(res);
}
@Override
public int getItemCount() {
if (mHeaderView == null) {
return list.size();
} else {
return list.size() + 1;
}
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, (int) v.getTag());
}
}
/**
* 传值
*
* @param listener
*/
public void setmOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CheckBox checkBox;
ImageView imageview;
TextView cash;
TextView cashcount;
ImageView or;
TextView time;
Button lookDetails;
public ViewHolder(View itemView) {
super(itemView);
if (itemView == mHeaderView) {
return;
}
checkBox = (CheckBox) itemView.findViewById(R.id.radio_btn);
imageview = (ImageView) itemView.findViewById(R.id.imageview);
cash = (TextView) itemView.findViewById(R.id.cash);
cashcount = (TextView) itemView.findViewById(R.id.cashcount);
or = (ImageView) itemView.findViewById(R.id.or);
time = (TextView) itemView.findViewById(R.id.time);
lookDetails = (Button) itemView.findViewById(R.id.lookDetails);
}
}
/**
* 清楚所有状态
*
* @param position
*/
public void clearState(int position) {
for (String key : states.keySet()) {
states.put(key, false);
}
states.put(String.valueOf(position), true);
}
/**
* 获取states的选中个数
*
* @return
*/
public int getstatescount() {
// HashMap<String,Boolean>
int i = 0;
for (String key : states.keySet()) {
if (states.get(key)) {
i++;
}
}
return i;
}
public HashMap<String, Boolean> getAllStates() {
return states;
}
//用于获取状态值
public Boolean getStates(int position) {
return states.get(String.valueOf(position));
}
//设置状态值
public void setStates(int position, boolean isChecked) {
states.put(String.valueOf(position), isChecked);
}
}
//布局文件
<?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="80dp"
android:layout_margin="@dimen/dp10"
android:background="@color/colorWhite"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp">
<CheckBox
android:id="@+id/radio_btn"
android:layout_width="@dimen/dp15"
android:layout_height="@dimen/dp15"
android:background="@drawable/selector_radiobutton"
android:button="@null"
android:clickable="false" />
</LinearLayout>
<ImageView
android:id="@+id/imageview"
android:layout_width="@dimen/dp45"
android:layout_height="@dimen/dp30"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp40"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp10"
android:layout_toRightOf="@+id/imageview"
android:text="¥88"
android:textColor="@color/color_ff7608"
android:textSize="@dimen/sp18" />
<TextView
android:id="@+id/cashcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp5"
android:layout_toRightOf="@+id/cash"
android:text="代金券X1"
android:textColor="@color/color_text_gray"
android:textSize="@dimen/sp12" />
<ImageView
android:id="@+id/or"
android:layout_width="@dimen/dp40"
android:layout_height="@dimen/dp40"
android:layout_alignParentRight="true"
android:src="@mipmap/daijinquanicon1" />
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/color_retangle" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp40"
android:layout_marginTop="@dimen/dp10"
android:text=""
android:textColor="@color/colorA6A6A6"
android:textSize="10sp" />
<Button
android:id="@+id/lookDetails"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/dp20"
android:background="@color/transparent"
android:gravity="center"
android:text="查看详情"
android:textColor="@color/color_text_gray" />
</RelativeLayout>
</LinearLayout>
- 多选recycleview+ checkbox(原理是用一个集合记录每个条目的状态)
- recycleview中带有checkbox,edittext时的条目复用问题
- android开发 recycleview的item中加入checkbox实现批量选择条目(复用时防止checkbox勾选状态错乱)
- RecycleView的多条目
- recycleview条目的背景选择器
- recycleView的条目长按换位置
- RecycleView 的多条目布局
- recycleview的多条目加载
- recycleview的多条目展示
- android RecycleView复杂多条目的布局
- RecycleView条目点击事件的实现方法
- recycleview定位到具体的条目
- Recycleview 的CheckBox的复用和全选
- 关于RecycleView中保证只有一个条目变化
- RecycleView+checkBox
- ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现
- recycleview滑动过程中checkbox控件状态异常
- 一个recycleview的adapter
- 欢迎使用CSDN-markdown编辑器
- 基于Jquery实现的手风琴
- 顺序表-增(插入)
- 威威猫系列故事——打地鼠
- HDU4585 Shaolin(treap)
- 多选recycleview+ checkbox(原理是用一个集合记录每个条目的状态)
- Python:numpy中shape和reshape的用法
- 史上最全web.xml配置文件元素详解
- 支付宝小程序开发教程
- Iframe使用总结(实战)
- 按点击量排序
- go版本gRPC入门
- python解析json文件回顾(load/loads)
- 堆溢出姿势