Android RecyclerView选择多个item的实现代码
程序员文章站
2023-12-19 22:50:34
模仿网易新闻客户端阅读偏好的频道选择,先看实现的页面:
直接上代码:
import android.content.res.resources;...
模仿网易新闻客户端阅读偏好的频道选择,先看实现的页面:
直接上代码:
import android.content.res.resources; import android.content.res.typedarray; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.defaultitemanimator; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.helper.itemtouchhelper; import android.util.log; import android.util.sparsebooleanarray; import android.view.view; import java.util.arraylist; import java.util.collections; import java.util.list; public class recyclerviewactivity extends appcompatactivity { private recyclerview recycler; private recycleradapter madapter; private list<prefercustomizablechannel> channels = new arraylist<>(); private list<prefercustomizablechannel> channelsselected; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recycler_view_acitivity); initdata(); initui(); findviewbyid(r.id.resultbtn).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int lens = channelsselected.size(); for (int i =0 ; i < lens; i++) { prefercustomizablechannel customizablechannel = channelsselected.get(i); if (customizablechannel.isselected()) { log.i("recyclerviewactivity", "onclick: "+customizablechannel.tostring()); } } } }); } private void initdata() { resources resources = getresources(); typedarray array = resources.obtaintypedarray(r.array.prefer_channel_icon); int len = array.length(); string[] name = resources.getstringarray(r.array.prefer_channel_name); for (int i = 0; i < len; i++) { prefercustomizablechannel customizablechannel = new prefercustomizablechannel(); customizablechannel.setchannel(name[i]); customizablechannel.setresid(array.getresourceid(i, 0)); customizablechannel.setselected(false); customizablechannel.setid(i * 100); channels.add(customizablechannel); } array.recycle(); channelsselected = channels; } private void initui() { recycler = (recyclerview) findviewbyid(r.id.recycler); final gridlayoutmanager manager = new gridlayoutmanager(this, 3); recycler.setlayoutmanager(manager); recycler.sethasfixedsize(true); recycler.setitemanimator(new defaultitemanimator()); madapter = new recycleradapter(recyclerviewactivity.this, channels); recycler.setadapter(madapter); madapter.setclicklistener(new onrecyclerviewitemclicklistener() { @override public void onitemclick(view view, int position) { sparsebooleanarray selecteds = madapter.getselecteditem(); int len = channels.size(); for (int i = 0; i < len; i++) { if (selecteds.get(i)) { channelsselected.get(position).setselected(true); } } } }); } }
布局文件recyclerview横竖都是“match_parent”,否则在点击的时候gridview会自动上滑一段距离。
适配器的实现:
import android.support.v7.widget.recyclerview; import android.util.sparsebooleanarray; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.relativelayout; import android.widget.textview; import com.entity.prefercustomizablechannel; import com.listener.onrecycleritemclicklistener; import java.util.list; import butterknife.bindview; import butterknife.butterknife; public class preferchanneladapter extends recyclerview.adapter<preferchanneladapter.preferchannelholder>{ private list<prefercustomizablechannel> lists; private onrecycleritemclicklistener listener; private sparsebooleanarray selectlists = new sparsebooleanarray(); public preferchanneladapter() { } public void setdatas(list<prefercustomizablechannel> lists) { this.lists = lists; notifydatasetchanged(); } public void setonitemclicklistener(onrecycleritemclicklistener listener) { this.listener = listener; } @override public preferchannelholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.prefer_channel_item, null); return new preferchannelholder(view); } @override public void onbindviewholder(final preferchannelholder holder, final int position) { prefercustomizablechannel channelitem = lists.get(position); holder.channelitemtv.settext(channelitem.getchannel()); holder.channelitemimg.setimageresource(channelitem.getresid()); if (!selectlists.get(position)) { holder.selectedmarkimg.setvisibility(view.gone); } else { holder.selectedmarkimg.setvisibility(view.visible); } holder.preferchannelitemlayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (holder.selectedmarkimg.getvisibility() == view.gone) { holder.selectedmarkimg.setvisibility(view.visible); selectlists.put(position, true); } else if (holder.selectedmarkimg.getvisibility() == view.visible){ holder.selectedmarkimg.setvisibility(view.gone); selectlists.put(position, false); } listener.onrecyclerclick(position); } }); } @override public int getitemcount() { return lists.size(); } public class preferchannelholder extends recyclerview.viewholder { @bindview(r.id.preferchannelitemlayout) relativelayout preferchannelitemlayout; @bindview(r.id.channelitemtv) textview channelitemtv; @bindview(r.id.channelitemimg) imageview channelitemimg; @bindview(r.id.selectedmarkimg) imageview selectedmarkimg; public preferchannelholder(view itemview) { super(itemview); butterknife.bind(this, itemview); } } public sparsebooleanarray getselecteditem() { return selectlists; } }
顺便把item的布局也贴出来吧:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/preferchannelitemlayout" android:gravity="center" android:layout_gravity="center" android:layout_margintop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--android:gravity="center" android:layout_gravity="center"--> <imageview android:id="@+id/channelitemimg" android:scaletype="centerinside" android:layout_width="68dp" android:layout_height="wrap_content"/> <textview android:id="@+id/channelitemtv" android:gravity="center" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_marginbottom="20dp" android:layout_margintop="8dp" android:layout_below="@id/channelitemimg" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <imageview android:id="@+id/selectedmarkimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:visibility="gone" android:layout_alignright="@id/channelitemimg" android:src="@mipmap/prefer_selected"/> </relativelayout>
其他注意的地方:
sparebooleanarrary.size()返回的是已经设置为true的长度,比如选择了一个则返回1,选择了10则返回10,但在选择了10后去掉一个的时候,size()返回的并不是9,而依然是10,这点注意,所以在遍历的时候并没有使用使用size()取长度。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android RecyclerView选择多个item的实现代码
-
Android RecyclerView 实现快速滚动的示例代码
-
Android中RecyclerView拖拽、侧删功能的实现代码
-
Android中RecyclerView实现Item添加和删除的代码示例
-
Android RecyclerView选择多个item的实现代码
-
Android中RecyclerView拖拽、侧删功能的实现代码
-
Android RecyclerView 实现快速滚动的示例代码
-
Android中RecyclerView实现Item添加和删除的代码示例
-
ImageView 实现Android colorPikcer 选择器的示例代码
-
android相册选择图片的编码实现代码