欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android RecyclerView使用方法解析

程序员文章站 2024-03-01 21:59:22
1.简介   recyclerview是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为listview和gridview控件的继承者,在最新的s...

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);
 
//layoutmanager
recyclerview.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,linearlayoutmanager.vertical,false));
 
//设置grid类型效果
recyclerview.setlayoutmanager(new gridlayoutmanager(recyclerviewactivity.this, 2, gridlayoutmanager.vertical, false));
//recyclerview.scrolltoposition(99);
 
//设置瀑布流类型效果
recyclerview.setlayoutmanager(new staggeredgridlayoutmanager(3,staggeredgridlayoutmanager.vertical));

4.分割线

参照网址:

//设置分割线-分割线需要自定义&还可以自定义分割线的样式
//没有提供默认的分割线
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, top, right, bottom);
  mdivider.draw(c);
 }
 }

 @override
 public void getitemoffsets(rect outrect, int itemposition, recyclerview parent) {
 if (morientation == vertical_list) {
  outrect.set(0, 0, 0, mdivider.getintrinsicheight());
 } else {
  outrect.set(0, 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, int position, 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);
 }
});

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