Recyclerview和CheckBox的完美
来源:互联网 发布:nginx 多个二级域名 编辑:程序博客网 时间:2024/06/05 12:14
Recyclerview的
多选 单选 全选 去不选
mainactivity
public class MainActivity extends AppCompatActivity { private boolean isType = false; private List list = new ArrayList(); private RecyclerviewAdapter recyclerviewAdapter; private Button selectAll; private Button cancal; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); selectAll = (Button) findViewById(R.id.select_all); cancal = (Button) findViewById(R.id.cancal); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); list.add(R.mipmap.ic_launcher); setRecyclerview(); initData(); } private void initData() { selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isType = true; recyclerviewAdapter.getAllSelect(); } }); cancal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isType = false; recyclerviewAdapter.setAllSelect(); } }); } private void setRecyclerview() { LinearLayoutManager layout = new LinearLayoutManager(this); layout.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layout); recyclerviewAdapter = new RecyclerviewAdapter(list, this); //rvGroupPicture.addItemDecoration(recyclerViewItemDecoration); recyclerView.setAdapter(recyclerviewAdapter); } public void setResponseState() { runOnUiThread(new Runnable() { @Override public void run() { try { int len = 0; int lenght = list.size(); if (lenght > 1) { for (int i = 0; i < lenght; i++) { if (recyclerviewAdapter.ischeck.get(i, false)) { len = len + 1; } } if (len == 0) { isType = false; } else if (len > 0 & len < lenght) { isType = false; } else if (len == lenght) { isType = true; } } } catch (Exception e) { e.printStackTrace(); } } }); }}mainactivity的布局
<?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" android:orientation="vertical"> <Button android:id="@+id/select_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" /> <Button android:id="@+id/cancal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>adapter
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerViewHodler> { private List list; private Context context; public SparseArray<Boolean> ischeck; public SparseArray<CheckBox> listbox; private boolean selectedState = false; public RecyclerviewAdapter(List<String> list, Context context) { this.context = context; this.list = list; ischeck = new SparseArray<Boolean>(); listbox = new SparseArray<CheckBox>(); } @Override public int getItemCount() { return list != null ? list.size() : 0; } @Override public RecyclerViewHodler onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_item, parent, false); return new RecyclerViewHodler(view); } @Override public void onBindViewHolder(RecyclerViewHodler holder, int position) { holder.iv.setBackgroundResource((Integer) list.get(position)); holder.relativeLayour_item.setId(position); SetSelectedState(holder.checkBoxGroupPicture, ischeck.get(position, false)); listbox.put(position, holder.checkBoxGroupPicture); holder.relativeLayour_item.setOnClickListener(onclick); } public View.OnClickListener onclick = new View.OnClickListener() { @Override public void onClick(View v) { int position = v.getId(); setIsSelectedState(true); if (ischeck == null) { ischeck.put(position, true); } else { if (!ischeck.get(position, false)) { ischeck.put(position, true); } else { ischeck.put(position, false); } } SetSelectedState(listbox.get(position), ischeck.get(position)); ((MainActivity) context).setResponseState(); } }; public void setIsSelectedState(boolean state) { selectedState = state; } public boolean getIsSelectedState() { return selectedState; } public void SetSelectedState(CheckBox checkBox, boolean type) { if (getIsSelectedState()) { checkBox.setChecked(type); if (type) { checkBox.setBackgroundResource(R.mipmap.selected); } else { checkBox.setBackgroundResource(R.mipmap.unselected); } } } public void getAllSelect() { try { setIsSelectedState(true); if (getIsSelectedState()) { int length = list.size(); ischeck.clear(); //groupPath.clear(); for (int i = 0; i < length; i++) { ischeck.put(i, true); // ((GroupPictureEditActivity) context).setResponseState(); if (i < listbox.size()) { SetSelectedState(listbox.get(i), ischeck.get(i)); } } } } catch (Exception e) { e.printStackTrace(); } } public void setAllSelect() { try { setIsSelectedState(true); if (getIsSelectedState()) { ischeck.clear(); int length = list.size(); for (int i = 0; i < length; i++) { ischeck.put(i, false); if (i < listbox.size()) { SetSelectedState(listbox.get(i), ischeck.get(i)); } } ((MainActivity)context).setResponseState(); } } catch (Exception e) { e.printStackTrace(); /** 为避免index下标越界 */ } }}item布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="60dp" android:layout_height="60dp" android:orientation="vertical"> <RelativeLayout android:id="@+id/relativeLayour_item" android:layout_width="60dp" android:layout_height="60dp"> <ImageView android:id="@+id/iv" android:layout_width="60dp" android:layout_height="60dp" /> <CheckBox android:id="@+id/checkbox" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@mipmap/unselected" android:button="@null" android:clickable="false" android:duplicateParentState="true" android:focusable="false" android:focusableInTouchMode="false" /> </RelativeLayout></LinearLayout>viewholder
public class RecyclerViewHodler extends RecyclerView.ViewHolder { public ImageView iv; public CheckBox checkBoxGroupPicture; public RelativeLayout relativeLayour_item; public RecyclerViewHodler(View view){ super(view); iv = (ImageView)view.findViewById(R.id.iv); iv.setScaleType(ImageView.ScaleType.CENTER_CROP); checkBoxGroupPicture = (CheckBox)view.findViewById(R.id.checkbox); relativeLayour_item = (RelativeLayout)view.findViewById(R.id.relativeLayour_item); }}
阅读全文
1 0
- Recyclerview和CheckBox的完美
- Recyclerview+checkbox
- RecyclerView中使用checkbox遇到的问题
- RecyclerView中CheckBox显示混乱的解决办法
- Recyclerview中checkbox的复用问题
- CheckBox以及RecyclerView的初步使用
- RecyclerView+CheckBox 实现横向的图片选择
- Recyclerview+checkbox 简单的购物车效果
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- RecyclerView与SwipeRefreshLayout的完美搭配
- RecyclerView与SwipeRefreshLayout的完美搭配
- android studio checkbox隐藏和显示在recyclerview中
- RecyclerView实现checkBox
- RecyclerView中CheckBox无效.
- Kotlin语法基础,函数与闭包
- WUST 1904 饥饿的牛(dp)
- Mac问题积累
- POJ 1012--Joseph
- 数据库导入数据
- Recyclerview和CheckBox的完美
- 错误解决:Unable to add module to the current project as it is not of package
- 一步一步理解Java NIO(上)
- 理解EJB,EJB到底是什么?
- 初探es7中async和await方法
- Angular2 ElementRef 实现低耦合高内聚 视图应用分离
- angulaejs监听路由跳转
- mysql启动服务
- PAT--1047. Student List for Course