Android RecyclerView使用方法解析

Android RecyclerView使用方法解析,第1张

概述1.简介  RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。RecyclerView架构,提供了一种插拔式的体

1.简介

  RecyclerVIEw是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListVIEw和GrIDVIEw控件的继承者,在最新的support-V7版本中提供支持。RecyclerVIEw架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,Itemdecoration,ItemAnimator实现令人瞠目的效果。

总之ListVIEw和GrIDVIEw能做的RecyclerVIEw都能做,并且能实现瀑布流效果

2.使用

在build.gradle配置RecyclerVIEw的库
compile 'com.androID.support:recyclervIEw-v7:23.3.0'

//设置RecyclerVIEw的适配器adapter = new MyRecyclerVIEwAdapter(RecyclerVIEwActivity.this,datas);recyclervIEw.setAdapter(adapter); //LayoutManagerrecyclervIEw.setLayoutManager(new linearlayoutmanager(RecyclerVIEwActivity.this,linearlayoutmanager.VERTICAL,false));// recyclervIEw.scrollToposition(datas.size()-1); //添加RecyclerVIEw的分割线recyclervIEw.addItemdecoration(new divIDerListItemdecoration(RecyclerVIEwActivity.this,divIDerListItemdecoration.VERTICAL_List)); //设置动画recyclervIEw.setItemAnimator(new DefaultItemAnimator());
public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.MyVIEwHolder> {  private final Context context; private final ArrayList<String> datas;  public MyAdapter(Context context,ArrayList<String> datas){ this.context = context; this.datas = datas; } /** * 相当于ListVIEw适配器中的getVIEw的创建holder布局 * * @param parent * @param vIEwType * @return */ @OverrIDe public MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { VIEw vIEw = VIEw.inflate(context,R.layout.item_hello,null); return new MyVIEwHolder(vIEw); }  @OverrIDe public voID onBindVIEwHolder(MyVIEwHolder holder,int position) { holder.tv_text.setText(datas.get(position)); holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher);  }  @OverrIDe public int getItemCount() { return datas.size(); }  class MyVIEwHolder extends RecyclerVIEw.VIEwHolder {  private TextVIEw tv_text; private ImageVIEw iv_icon;  public MyVIEwHolder(VIEw itemVIEw) {  super(itemVIEw);  tv_text = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_text);  iv_icon = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.iv_icon);  } }}

3.设置ListVIEw&GrIDVIEw&瀑布流类型效果

//设置List类型效果recyclervIEw.setLayoutManager(new linearlayoutmanager(RecyclerVIEwActivity.this,false)); //设置GrID类型效果recyclervIEw.setLayoutManager(new GrIDLayoutManager(RecyclerVIEwActivity.this,2,GrIDLayoutManager.VERTICAL,false));//recyclervIEw.scrollToposition(99); //设置瀑布流类型效果recyclervIEw.setLayoutManager(new StaggeredGrIDLayoutManager(3,StaggeredGrIDLayoutManager.VERTICAL));

4.分割线

参照网址:http://blog.csdn.net/lmj623565791/article/details/45059587

//设置分割线-分割线需要自定义&还可以自定义分割线的样式//没有提供默认的分割线recyclervIEw.addItemdecoration(new divIDerListItemdecoration(this,divIDerListItemdecoration.VERTICAL_List));
public class divIDerListItemdecoration extends RecyclerVIEw.Itemdecoration { private static final int[] ATTRS = new int[]{  androID.R.attr.ListdivIDer }; public static final int HORIZONTAL_List = linearlayoutmanager.HORIZONTAL; public static final int VERTICAL_List = linearlayoutmanager.VERTICAL; private Drawable mdivIDer; private int mOrIEntation; public divIDerListItemdecoration(Context context,int orIEntation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mdivIDer = a.getDrawable(0); a.recycle(); setorIEntation(orIEntation); } public voID setorIEntation(int orIEntation) { if (orIEntation != HORIZONTAL_List && orIEntation != VERTICAL_List) {  throw new IllegalArgumentException("invalID orIEntation"); } mOrIEntation = orIEntation; } @OverrIDe public voID onDraw(Canvas c,RecyclerVIEw parent) {// Log.e("recyclervIEw - itemdecoration","onDraw()"); if (mOrIEntation == VERTICAL_List) {  drawVertical(c,parent); } else {  drawHorizontal(c,parent); } } public voID drawVertical(Canvas c,RecyclerVIEw parent) { final int left = parent.getpaddingleft(); final int right = parent.getWIDth() - parent.getpaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) {  final VIEw child = parent.getChildAt(i);  androID.support.v7.Widget.RecyclerVIEw v = new androID.support.v7.Widget.RecyclerVIEw(parent.getContext());  final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child   .getLayoutParams();  final int top = child.getBottom() + params.bottommargin;  final int bottom = top + mdivIDer.getIntrinsicHeight();  mdivIDer.setBounds(left,top,right,bottom);  mdivIDer.draw(c); } } public voID drawHorizontal(Canvas c,RecyclerVIEw parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getHeight() - parent.getpaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) {  final VIEw child = parent.getChildAt(i);  final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child   .getLayoutParams();  final int left = child.getRight() + params.rightmargin;  final int right = left + mdivIDer.getIntrinsicHeight();  mdivIDer.setBounds(left,bottom);  mdivIDer.draw(c); } } @OverrIDe public voID getItemOffsets(Rect outRect,int itemposition,RecyclerVIEw parent) { if (mOrIEntation == VERTICAL_List) {  outRect.set(0,mdivIDer.getIntrinsicHeight()); } else {  outRect.set(0,mdivIDer.getIntrinsicWIDth(),0); } }}divIDerListItemdecoration

设置分割线样式:

①application设置

<!-- Base application theme. --><style name="Apptheme" parent="theme.AppCompat.light.DarkActionbar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="androID:ListdivIDer">@drawable/divIDer_bg</item></style>

②在activity中

<activity androID:name=".recyclervIEw.RecyclerVIEwActivity" androID:theme="@style/ListdivIDertheme"/><style name="ListdivIDertheme" parent="theme.AppCompat.light.DarkActionbar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="androID:ListdivIDer">@drawable/divIDer_bg</item></style>

5.自定义设置item的点击事件

RecyclerVIEw默认是没有点击事件的,需要自定义点击事件
用到知识点:接口,getLayoutposition()

public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.MyVIEwHolder>{  private final Context context; private final ArrayList<String> datas;  //设置点击某个item的监听 public interface OnItemClickListener{  voID onItemClick(VIEw vIEw,int position,String content); }  private OnItemClickListener onItemClickListener; public voID setonItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; }  //设置点击图片 public interface OnImageVIEwClickListener{ voID onImageVIEwClick(VIEw vIEw,int position); }  private OnImageVIEwClickListener onImageVIEwClickListener; public voID setonImageVIEwClickListener(OnImageVIEwClickListener onImageVIEwClickListener) { this.onImageVIEwClickListener = onImageVIEwClickListener; }  ...................................  class MyVIEwHolder extends RecyclerVIEw.VIEwHolder {  private TextVIEw tv_text; private ImageVIEw iv_icon;  public MyVIEwHolder(VIEw itemVIEw) {  super(itemVIEw);  tv_text = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_text);  iv_icon = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.iv_icon);   //设置点击事件  itemVIEw.setonClickListener(new VIEw.OnClickListener() {  @OverrIDe  public voID onClick(VIEw v) {   if(onItemClickListener != null){   onItemClickListener.onItemClick(v,getLayoutposition(),datas.get(getLayoutposition()));   }  }  });   //设置监听  iv_icon.setonClickListener(new VIEw.OnClickListener() {  @OverrIDe  public voID onClick(VIEw v) {   if(onImageVIEwClickListener != null){   onImageVIEwClickListener.onImageVIEwClick(v,getLayoutposition());   }  }  }); } }}

在Activity中使用自定义的点击事件

//设置点击item的点击事件myAdapter.setonItemClickListener(new MyAdapter.OnItemClickListener() { @OverrIDe public voID onItemClick(VIEw vIEw,String content) { Toast.makeText(RecyclerVIEwActivity.this,"content=="+content+",--position=="+position,Toast.LENGTH_SHORT).show(); }}); //设置点击某张图片的点击事件myAdapter.setonImageVIEwClickListener(new MyAdapter.OnImageVIEwClickListener() { @OverrIDe public voID onImageVIEwClick(VIEw vIEw,int position) { Toast.makeText(RecyclerVIEwActivity.this,"position=="+position+",vIEw=="+vIEw.toString(),Toast.LENGTH_SHORT).show(); }});

6.删除和增加数据

1_在适配器中新增加添加和删除两个方法

public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.MyVIEwHolder> {  ........................  public voID addData(int position,String content){ datas.add(position,content); notifyItemInserted(position); }  public voID removeData(int position){ datas.remove(position); notifyItemRemoved(position); }}

2_Activity中

btn_add.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myAdapter.addData(0,"Content NetData"); //定位到第0个位置 recyclervIEw.scrollToposition(0); }}); btn_remove.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myAdapter.removeData(0); }});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android RecyclerView使用方法解析全部内容,希望文章能够帮你解决Android RecyclerView使用方法解析所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存