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

Android RecyclerView下拉刷新和上拉加载及依赖

程序员文章站 2023-01-06 09:38:29
效果图: 代码已经同步到github~ gradle引入依赖: allprojects { repositories { ... maven...

效果图:

Android RecyclerView下拉刷新和上拉加载及依赖

代码已经同步到github~

gradle引入依赖:

  allprojects {
    		repositories {
    			...
    			maven { url 'https://jitpack.io' }
    		}
    	}


    dependencies {
    	        compile 'com.github.louyulin:pullloadrecyclerview:v1.1'
    	}

关键代码使用:

  //绑定组件
    pullrv = (pullloadrecyclerview) findviewbyid(r.id.pullrv);
    //设置下拉颜色
    pullrv.setswiperefreshcolor(android.r.color.holo_blue_bright,android.r.color.holo_blue_dark,android.r.color.holo_blue_bright);
    //设置列数和滚动方向
    pullrv.setlayoutmanager(4, linearlayoutmanager.vertical);
     //设置监听
            pullrv.setonpullloadmorelistener(new pullloadrecyclerview.onpullloadmorelistener() {
                @override
                public void rerresh() {

                }

                @override
                public void loadmore() {

                }
            });

分析:

继承linearlayout并将recyclerview和底布局包含在内

public class pullloadrecyclerview extends linearlayout {

    private context mcontext;
    private swiperefreshlayout mswiperefreshlayout;
    private boolean misrefresh = false; //是否是刷新
    private boolean misloadmore = false; //是否是加载更多
    private recyclerview mrecyclerview;
    private view mfootview;
    private onpullloadmorelistener monpullloadmorelistener;

    public pullloadrecyclerview(context context, attributeset attrs, int defstyleattr) {
        super(context, attrs, defstyleattr);
        initview(context);
    }

    public pullloadrecyclerview(context context, attributeset attrs) {
        super(context, attrs);
        initview(context);
    }

    public pullloadrecyclerview(context context) {
        super(context);
        initview(context);
    }

    private void initview(context context) {
        mcontext = context;
        view view = layoutinflater.from(mcontext).inflate(r.layout.pull_loadmore_layout, null);
        mswiperefreshlayout = (swiperefreshlayout) view.findviewbyid(r.id.swiperefreshlayout);

        mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayoutonrefresh());

        //处理recyclerview
        mrecyclerview = (recyclerview) view.findviewbyid(r.id.recyclerview);
        mrecyclerview.sethasfixedsize(true); //设置固定大小
        mrecyclerview.setitemanimator(new defaultitemanimator());//使用默认动画
        mrecyclerview.setontouchlistener(new ontouchlistener() {
            @override
            public boolean ontouch(view v, motionevent event) {
                return misrefresh || misloadmore;
            }
        });

        mrecyclerview.setverticalscrollbarenabled(false);//隐藏滚动条
        mrecyclerview.addonscrolllistener(new recyclerviewonscroll());
        mfootview = view.findviewbyid(r.id.footer_view);
        imageview imageview = (imageview) mfootview.findviewbyid(r.id.iv_load_img);
   
        textview textview = (textview) mfootview.findviewbyid(r.id.tv_load_text);
        mfootview.setvisibility(view.gone);
        //view 包含swiperefreshlayout, recyclerview, footview
        this.addview(view);//
    }

    //设置刷新时控件颜色渐变
    public void setswiperefreshcolor(integer ...colors) {
        for (int i = 0; i < colors.length ; i++) {
            mswiperefreshlayout.setcolorschemeresources(colors[i]);
        }
    }

    //外部可以设置recyclerview的列数

    public void setlayoutmanager(int spancount,int orientation){
        gridlayoutmanager manager = new gridlayoutmanager(mcontext, spancount);
        manager.setorientation(orientation);
        mrecyclerview.setlayoutmanager(manager);
    }

    public void setadapter(recyclerview.adapter adapter) {
        if (adapter != null) {
            mrecyclerview.setadapter(adapter);
        }
    }

    class swiperefreshlayoutonrefresh implements swiperefreshlayout.onrefreshlistener{

        @override
        public void onrefresh() {
             if (!misrefresh) {
                 misrefresh = true;
                 refreshdata();
             }
        }
    }

    class recyclerviewonscroll extends recyclerview.onscrolllistener {
        @override
        public void onscrolled(recyclerview recyclerview, int dx, int dy) {
            super.onscrolled(recyclerview, dx, dy);
            int firstitem = 0;
            int lastitem = 0;
            recyclerview.layoutmanager manager = recyclerview.getlayoutmanager();
            int totalcount = manager.getitemcount();
            if (manager instanceof gridlayoutmanager) {
                gridlayoutmanager gridlayoutmanager = (gridlayoutmanager) manager;
                //第一个完全可见的item
                firstitem = gridlayoutmanager.findfirstcompletelyvisibleitemposition();
                //最后一个完全可见的item
                lastitem = gridlayoutmanager.findlastcompletelyvisibleitemposition();
                if (firstitem == 0 || firstitem == recyclerview.no_position) {
                    lastitem = gridlayoutmanager.findlastvisibleitemposition();
                }
            }
            //什么时候触发上拉加载更多?
            // 1.加载更多是false
            // 2.totalcount - 1 === lastitem
            // 3.mswiperefreshlayout可以用
            // 4. 不是处于下拉刷新状态
            // 5. 偏移量dx > 0 或dy > 0
            if (!misloadmore
                && totalcount - 1 == lastitem
                && mswiperefreshlayout.isenabled()
                && !misrefresh
                && (dx > 0 || dy > 0)) {
                misloadmore = true;
                //在加载更多时,禁止mswiperefreshlayout使用
                mswiperefreshlayout.setenabled(false);
                loadmoredata();
            } else {
                mswiperefreshlayout.setenabled(true);
            }
        }
    }

    private void refreshdata() {
        if (monpullloadmorelistener != null) {
            monpullloadmorelistener.rerresh();
        }
    }

    private void loadmoredata() {
        if (monpullloadmorelistener != null) {
            mfootview.animate().translationy(0).setinterpolator(new acceleratedecelerateinterpolator())
                    .setduration(300).setlistener(new animatorlisteneradapter() {
                @override
                public void onanimationstart(animator animation) {
                    super.onanimationstart(animation);
                    mfootview.setvisibility(view.visible);
                    //manimationdrawable.start();
                }
            }).start();
            invalidate();
            monpullloadmorelistener.loadmore();
        }
    }

    //设置刷新完毕
    public void setrefreshcompleted() {
        misrefresh = false;
        setrefreshing(false);
    }

    //设置是否正在刷新
    private void setrefreshing(final boolean isrefreshing) {
        mswiperefreshlayout.post(new runnable() {
            @override
            public void run() {
                mswiperefreshlayout.setrefreshing(isrefreshing);
            }
        });
    }

    public void setloadmorecompleted() {
        misloadmore = false;
        misrefresh = false;
        setrefreshing(false);
        mfootview.animate().translationy(mfootview.getheight()).setinterpolator(new acceleratedecelerateinterpolator())
                .setduration(300).start();
    }

    public interface onpullloadmorelistener {
        void rerresh();
        void loadmore();
    }

    public void setonpullloadmorelistener(onpullloadmorelistener listener) {
        monpullloadmorelistener = listener;
    }
}