Android RecyclerView选择多个item的实现代码

Android RecyclerView选择多个item的实现代码,第1张

概述模仿网易新闻客户端阅读偏好的频道选择,先看实现的页面:直接上代码:importandroid.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选择多个item的实现代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存