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

android RecyclerView

程序员文章站 2022-07-05 15:25:34
引用 在项目的build.gradle添加依赖 RecyclierView使用的基本方法 recyclerView.setAdapter(); 添加适配器(必须) recyclerView.setLayoutManager(); 选择一种布局(必须) recyclerAdapter.setHeade ......

引用

在项目的build.gradle添加依赖

compile 'com.android.support:recyclerview-v7:23.4.0'

recyclierview使用的基本方法

recyclerview.setadapter(); 添加适配器(必须)

recyclerview.setlayoutmanager(); 选择一种布局(必须)

recycleradapter.setheaderview(); 添加头布局

recycleradapter.setfooterview(); 添加底部布局

recyclerview.setitemanimator(new defaultitemanimator()); 添加默认动画

recyclerview.additemdecoration(); 添加分割线

layout manager布局管理器

1.三种布局管理器:

new linearlayoutmanager(this, linearlayoutmanager.vertical, false) 以垂直或者水平列表方式展示item,第三个参数是否颠倒数据显示

new gridlayoutmanager(this,4)  以网格方式展示item,第二个参数代表列数

new staggeredgridlayoutmanager(4, staggeredgridlayoutmanager.horizontal)  以瀑布流方式展示item,第一个参数代表列数

2.常用方法

canscrollhorizontally();//能否横向滚动
canscrollvertically();//能否纵向滚动
scrolltoposition(int position);//滚动到指定位置
setorientation(int orientation);//设置滚动的方向
getorientation();//获取滚动方向
findviewbyposition(int position);//获取指定位置的item view
findfirstcompletelyvisibleitemposition();//获取第一个完全可见的item位置
findfirstvisibleitemposition();//获取第一个可见item的位置
findlastcompletelyvisibleitemposition();//获取最后一个完全可见的item位置
findlastvisibleitemposition();//获取最后一个可见item的位置

 adapter适配器模板

recyclerview必须自定义适配器,并且自己创建事件监听,布局和数据的绑定顺序,每个item都会执行一遍:getiitemviewtype(绑定类型) > oncreateviewholder(选择布局类型) > onbindviewholder(绑定数据)

notifyiteminserted(position);插入一个item

notifyitemremoved(position); 移除一个item

notifyitemchanged(position) 刷新item

public class datarecycleradapter extends recyclerview.adapter<datarecycleradapter.myviewholder>{
    private list<string> list;
    private context context;
    private list<integer> mheights;
    private static int type_title=0;
    private static int type_footer=1;
    private static int type_content =2;
    private view mheaderview;
    private view mfooterview;

    public datarecycleradapter(context context, list<string> list) {
        this.list=list;
        mheights = new arraylist<>();
        this.context=context;
    }

    //根据不同的类型适应不用的布局
    @override
    public myviewholder oncreateviewholder(viewgroup parent, int viewtype) {
        if(mheaderview != null && viewtype == type_title) {
            return new myviewholder(mheaderview);
        }
        if(mfooterview != null && viewtype == type_footer) {
            return new myviewholder(mfooterview);
        }
        return new myviewholder(layoutinflater.from(context).inflate(r.layout.options_item3,parent,false));
    }

    //给item分布不同的类型
    @override
    public int getitemviewtype(int position) {
        if (position == 0){
            return type_title;
         }else if(position==list.size()){
            return type_footer;
        }else{
            return type_content;
        }
    }

    public view getheaderview() {
        return mheaderview;
    }
    //添加自定义的头部
    public void setheaderview(view headerview) {
        mheaderview = headerview;
        notifyiteminserted(0);
    }

    public view getfooterview() {
        return mfooterview;
    }
    //添加自定义底部
    public void setfooterview(view footerview) {
        mfooterview = footerview;
        notifyiteminserted(list.size()+1);
    }
    //主体布局
    @override
    public void onbindviewholder(final myviewholder holder, int position) {
        if(getitemviewtype(position) != type_content) {
            return;
        }
            holder.textview.settext(list.get(position-1));

        if (monitemclicklitener != null){
            holder.itemview.setonclicklistener(new view.onclicklistener(){
                @override
                public void onclick(view v){
                    int pos = holder.getlayoutposition();
                    monitemclicklitener.onitemclick(holder.itemview, pos);
                }
            });

            holder.itemview.setonlongclicklistener(new view.onlongclicklistener(){
                @override
                public boolean onlongclick(view v){
                    int pos = holder.getlayoutposition();
                    monitemclicklitener.onitemlongclick(holder.itemview, pos);
                    return true;
                }
            });
        }
    }

    //获取item总数
    @override
    public int getitemcount() {
         if(mfooterview !=null){
            return list.size()+1;
        }else {
            return list.size();
        }
    }
    //监听器接口
    public interface onitemclicklitener{
        void onitemclick(view view, int position);
        void onitemlongclick(view view , int position);
    }

    private onitemclicklitener monitemclicklitener;

    public void setonmyitemclicklitener(onitemclicklitener monitemclicklitener) {
        this.monitemclicklitener = monitemclicklitener;
    }

   //头和底部的布局
    class myviewholder extends recyclerview.viewholder {
        private textview textview;
        myviewholder(view itemview) {
            super(itemview);
            if (itemview == mheaderview||itemview == mfooterview){
                return;
            }
            textview= (textview) itemview.findviewbyid(r.id.options_tv);
            textview.settext("uuu");
        }
    }
    //模拟新增的数据
    public void adddata(int position) {
        list.add(position, "insert 数据");
        notifyiteminserted(position+1);
        toast.maketext(context,""+list.size(),toast.length_short).show();
    }
    //移除一个布局
    public void removedata(int position) {
        list.remove(position);
        notifyitemremoved(position);
    }

实例

private void initdatarecycleradapter(){
    recycleradapter=new datarecycleradapter(this,mdata);
    //布局管理器
    recyclerview.setlayoutmanager( new linearlayoutmanager(this, linearlayoutmanager.vertical, false));
    //分割线
    recyclerview.additemdecoration(new divideritemdecoration(this,
            divideritemdecoration.horizontal));
    //使用默认动画
    recyclerview.setitemanimator(new defaultitemanimator());
    //不使用默认的布局类型,添加自定义头部布局
    recycleradapter.setheaderview(layoutinflater.from(this).inflate(r.layout.options_title, recyclerview, false));
    //不使用默认的布局类型,添加自定义底部布局
    layoutinflater inflater= layoutinflater.from(this);
    view footerview = inflater.inflate(r.layout.options_footer, null);
    recycleradapter.setfooterview(footerview);
    //底部布局事件,增加一个item
    footerview.setonclicklistener(new view.onclicklistener() {
        @override
        public void onclick(view view) {
            toast.maketext(mainactivity.this,"展开",toast.length_short).show();
            recycleradapter.adddata(mdata.size());
        }
    });
    recyclerview.setadapter(recycleradapter);
    recycleradapter.setonmyitemclicklitener(new datarecycleradapter.onitemclicklitener() {
        //item的点击事件,移除一个item
        @override
        public void onitemclick(view view, int position) {
            recycleradapter.removedata(position);
            toast.maketext(mainactivity.this,""+mdata.size(),toast.length_short).show();
        }
        //item的长按事件,显示item下标
        @override
        public void onitemlongclick(view view, int position) {
            toast.maketext(mainactivity.this,"第"+position+"个",toast.length_short).show();
        }
    });
}
//模拟数据源
private void adddata(){
    mdata= new arraylist<>();
    for (int i=1;i<20;i++) {
        mdata.add("数据"+i+"个");
    }
}