Android RecyclerView封装下拉刷新与上拉加载更多

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zl18603543572/article/details/90744103

1 scanlistlibrary 基础组件说明(基于 RecyclerView的封装)

  • 基本数据列表(支持下拉刷新与上拉加载更多)
  • 九宫格数据显示封装(支持下拉刷新与上拉加载更多)
  • 瀑布流数据显示封装 (支持下拉刷新与上拉加载更多)
  • 类似抖音上下滑动(整页)切换数据显示(支持下拉刷新与上拉加载更多)

详情使用请查看 scanlistlibrary

2 控件引用

<com.base.scanlistlibrary.scanlist.ScanVideoPlayView
       android:id="@+id/scan_video_play"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

代码中

private ScanVideoPlayView mScanVideoPlayView;
mScanVideoPlayView = findViewById(R.id.scan_video_play);

这里使用的基本数据模型 bean

public class TestVideoListBean {
    public String title;
    public String imageUrl= "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg";
    public String videoUrl ="http://pic.studyyoun.com/FhphBfdKTOOkukQgzzysl9nUinR4"+"?time="+System.currentTimeMillis();
    public long length =98000;
}

3 基本数据列表

3.1 创建适配器

适配器需要继承于 ScanBaseRecyclerViewAdapter ,并在相应的构造方法中调用 super(context, data, layoutId)方法;参数 data 为列表list数据源,layoutId 为条目显示layout id

public class TextPageScanAdapter extends ScanBaseRecyclerViewAdapter<TestVideoListBean> {
    public static final String TAG = TextPageScanAdapter.class.getSimpleName();

    public Context mContext;
    public TextPageScanAdapter(Context context, List<TestVideoListBean> data, int layoutId) {
        super(context, data, layoutId);
        mContext = context;
    }

    @Override
    protected void onBindData(ScanRecyclerViewHolder holder, TestVideoListBean bean, int position) {
        //在此方法中设置数据
        TextView lTextView = (TextView) holder.getView(R.id.test_video_list_page);
        lTextView.setText(""+bean.title);
    }
}

3.2 初使用化显示数据

//模拟数据  TestVideoListBean 只是一个普通的数据模型bean
        List<TestVideoListBean> lTestVideoListBeans = new ArrayList<>();
        for (int lI = 0; lI < 20; lI++) {
            TestVideoListBean lTestVideoListBean = new TestVideoListBean();
            lTestVideoListBean.title = System.currentTimeMillis() + " 初始化数据";
            lTestVideoListBeans.add(lTestVideoListBean);
        }
        //数据适配器  item_list_page_data_layout 只是一个 普通的布局显示文件
        TextPageScanAdapter lLittleVideoListAdapter = new TextPageScanAdapter(this, lTestVideoListBeans, R.layout.item_list_page_data_layout);
      
        //  初始化 mScanVideoPlayView 传入数据适配 Adapter Adapter需要继承于BaseRecyclerViewAdapter
        mScanVideoPlayView.initPlayListView(lLittleVideoListAdapter);

item_list_page_data_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/test_video_list_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      />
</LinearLayout>

4 九宫格数据显示封装(支持下拉刷新与上拉加载更多)

九宫格数据显示

4.1 创建数据适配器

与3.1中的适配器基本一样。

public class PicGirdPageScanAdapter extends ScanBaseRecyclerViewAdapter<TestVideoListBean> {
    public static final String TAG = PicGirdPageScanAdapter.class.getSimpleName();

    public Context mContext;
    public PicGirdPageScanAdapter(Context context, List<TestVideoListBean> data, int layoutId) {
        super(context, data, layoutId);
        mContext = context;
    }

    @Override
    protected void onBindData(ScanRecyclerViewHolder holder, TestVideoListBean bean, int position) {
        ImageView lView = (ImageView) holder.getView(R.id.test_video_list_iv_page);
        String url = "http://b-ssl.duitang.com/uploads/blog/201409/11/20140911024410_nX8Gs.jpeg";
        CommonGlideUtils.showImageView(mContext,R.mipmap.ab_toast_error,url,lView);

        TextView lTextView = (TextView) holder.getView(R.id.test_video_list_tv_page);
        lTextView.setText(""+bean.title);
    }
}

4.2 初使用化显示数据

与3.2 中列表显示数据不同的是,在调用videoPlayView.initPlayListView()方法时,参数colum 设置的数据限制显示的列数,如下限制的为3列

 //模拟数据集合
       List<TestVideoListBean> lTestVideoListBeans = new ArrayList<>();
        for (int lI = 0; lI < 10; lI++) {
            TestVideoListBean lTestVideoListBean = new TestVideoListBean();
            lTestVideoListBean.title = System.currentTimeMillis() + " 初始化数据";
            lTestVideoListBeans.add(lTestVideoListBean);
        }
        //创建初始化数据适配器 
        mLittleVideoListAdapter = new PicGirdPageScanAdapter(this, lTestVideoListBeans, R.layout.item_list_page_picglayout);

       /**
         * 初始化 videoPlayView 传入数据适配 Adapter Adapter需要继承于BaseRecyclerViewAdapter
         * 参数二 colum 是九宫格显示时所要显示的列数 这里显示3列
         * 参数三 emptyLayoutId 是无数据时 显示的布局样式,传0 显示默认
         * 参数四 设置为false 只在整页切换数据时使用
         */
        videoPlayView.initPlayListView(mLittleVideoListAdapter,3,0,false);

5 瀑布流数据显示封装 (支持下拉刷新与上拉加载更多)

5.1 创建数据适配器

在这里,与上面所不同的是,自定义adapter继承于ScanBaseRecyclerViewAdapter后,需要在相关构造方法中调用 super(context, data, layoutId, 2); 方能启动高度可变的holder

public class PicStagerPageScanAdapter extends ScanBaseRecyclerViewAdapter<TestVideoListBean> {

    public Context mContext;

    public PicStagerPageScanAdapter(Context context, List<TestVideoListBean> data, int layoutId) {
       //瀑布流使用时 需要设置为 2,默认为1 显示列表数据
       super(context, data, layoutId, 2);
        mContext = context;
    }

 
    @Override
    protected void onBindData(ScanRecyclerViewHolder holder, TestVideoListBean bean, int position) {
        ImageView lView = (ImageView) holder.getView(R.id.test_video_list_iv_page);
        String url = "http://b-ssl.duitang.com/uploads/blog/201409/11/20140911024410_nX8Gs.jpeg";
        CommonGlideUtils.showImageView(mContext, R.mipmap.ab_toast_error, url, lView);

        TextView lTextView = (TextView) holder.getView(R.id.test_video_list_tv_page);
        lTextView.setText("" + bean.title);
    }
}

5.2 初始化数据并设置显示数据

List<TestVideoListBean> lTestVideoListBeans = new ArrayList<>();
        for (int lI = 0; lI < 20; lI++) {
            TestVideoListBean lTestVideoListBean = new TestVideoListBean();
            lTestVideoListBean.title = System.currentTimeMillis() + " 初始化数据";
            lTestVideoListBeans.add(lTestVideoListBean);
        }

        mLittleVideoListAdapter = new PicStagerPageScanAdapter(this, lTestVideoListBeans, R.layout.item_list_page_pic_stag_layout);

     /**
         * 初始化 videoPlayView 传入数据适配 Adapter Adapter需要继承于BaseRecyclerViewAdapter
         * 参数二 colum 是瀑布流显示时所要显示的列数 这里显示2列
         * 参数三 emptyLayoutId 是无数据时 显示的布局样式,传0 显示默认
         * 参数四 emptyMsg 是无数据时 显示的文字提示
         * 参数五 isStager 只有设置为true 时才会使用瀑布流样式
         * 参数六 设置为false 只在整页切换数据时使用
         */
        videoPlayView.initPlayListView(mLittleVideoListAdapter, 2, 0, null, true, false);
        videoPlayView.setRefreshColorSchemeColors(Color.RED);

6 类似拼音上下滑动(整页)切换数据显示(支持下拉刷新与上拉加载更多)

6.1 创建数据适配器

public class PicPageScanAdapter extends ScanBaseRecyclerViewAdapter<TestVideoListBean> {
    public static final String TAG = PicPageScanAdapter.class.getSimpleName();

    public Context mContext;
    public PicPageScanAdapter(Context context, List<TestVideoListBean> data, int layoutId) {
        super(context, data, layoutId);
        mContext = context;
    }

    @Override
    protected void onBindData(ScanRecyclerViewHolder holder, TestVideoListBean bean, int position) {
        ImageView lView = (ImageView) holder.getView(R.id.test_video_list_iv_page);
        String url = "http://b-ssl.duitang.com/uploads/blog/201409/11/20140911024410_nX8Gs.jpeg";
        CommonGlideUtils.showImageView(mContext,R.mipmap.ab_toast_error,url,lView);

        TextView lTextView = (TextView) holder.getView(R.id.test_video_list_tv_page);
        lTextView.setText(""+bean.title);
    }
}

6.2 初始化数据并设置显示

List<TestVideoListBean> lTestVideoListBeans = new ArrayList<>();
        for (int lI = 0; lI < 1; lI++) {
            TestVideoListBean lTestVideoListBean = new TestVideoListBean();
            lTestVideoListBean.title = System.currentTimeMillis() + " 初始化数据";
            lTestVideoListBeans.add(lTestVideoListBean);
        }

        mLittleVideoListAdapter = new PicPageScanAdapter(this, lTestVideoListBeans, R.layout.item_list_page_pic_layout);
     

        /**
         * 初始化 videoPlayView 传入数据适配 Adapter Adapter需要继承于BaseRecyclerViewAdapter
         * 参数二 colum 设置为1 展示数据为列表样式
         * 参数三 emptyLayoutId 是无数据时 显示的布局样式,传0 显示默认
         * 参数四 设置为true 整页切换功能启动
         */
        videoPlayView.initPlayListView(mLittleVideoListAdapter,1,0,true);

7 videoPlayView设置下拉刷新与上拉加载更多数据监听器

//下拉刷新监听
mVideoPlayView.setOnRefreshDataListener(mOnRefreshDataListener);
private ScanContact.OnRefreshDataListener mOnRefreshDataListener = new ScanContact.OnRefreshDataListener() {
        @Override
        public void onRefresh() {
            //下拉刷新
            List<TestVideoListBean> refreshList = new ArrayList<>();
            for (int lI = 0; lI < 40; lI++) {
                TestVideoListBean lTestVideoListBean = new TestVideoListBean();
                lTestVideoListBean.title = System.currentTimeMillis() + " 下拉刷新 ";
                refreshList.add(lTestVideoListBean);
            }
            LogUtils.d("下拉刷新 ");
            //更新所有的数据
            mVideoPlayView.refreshVideoList(refreshList);
            //更新所有的数据 并滑动到第10条数据显示
            mVideoPlayView.refreshVideoList(refreshList,10);
        }

        @Override
        public void onLoadMore() {
            //上拉加载更多
            LogUtils.d("上拉加载更多 ");
            List<TestVideoListBean> moreList = new ArrayList<>();
            for (int lI = 0; lI < 40; lI++) {
                TestVideoListBean lTestVideoListBean = new TestVideoListBean();
                lTestVideoListBean.title = System.currentTimeMillis() + " 上拉加载更多 ";
                moreList.add(lTestVideoListBean);
            }
            mVideoPlayView.addMoreData(moreList);
        }
    };

8 类拟抖音整页切换时,页面选中监听

mVideoPlayView.setOnPageSelectListener(mOnPageSelectListener);
private ScanContact.OnPageSelectListener mOnPageSelectListener = new ScanContact.OnPageSelectListener() {
        @Override
        public void onPageSelected(int position, Object bean, ScanBaseRecyclerViewAdapter adapter, ScanRecyclerViewHolder holder) {
           //当前显示的页面 position 
           //bean 当前页面对应的数据模型
        }

        @Override
        public void onPageRelease(int position, Object o, ScanBaseRecyclerViewAdapter adapter, ScanRecyclerViewHolder holder) {
            //从显示到销毁  已销毁的页面 对应的 position
        }
    };

 

展开阅读全文

没有更多推荐了,返回首页