克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Lrecyclerview

介绍

原项目地址:https://github.com/jdsjlzx/LRecyclerView
按照其使用方式:
Step 1. 在你的根build.gradle文件中增加JitPack仓库依赖。

    allprojects {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }

Step 2. 在你的module的build.gradle文件中增加LRecyclerView依赖。
implementation 'com.github.jdsjlzx:LRecyclerView:1.5.4.3'
LRecyclerView requires at minimum Java 7 or Android 4.0.

  引入之后发现地址和引入的库的包的位置不对,下载源码看到里面的support库,所以手动下载下来导入了androidX的库,除了更新了androidX其余代码逻辑均未修改!自己做个很神经病的操作·····

这个问题是android studio的bug,重新编译一下就可以了

LRecyclerView 此处是原项目的使用介绍

LRecyclerView是支持addHeaderView、 addFooterView、下拉刷新、分页加载数据的RecyclerView。

它对 RecyclerView 控件进行了拓展,给RecyclerView增加HeaderView、FooterView,并且不需要对你的Adapter做任何修改。

推荐

RxJava经典视频教程已经上线,戳我就可以看啦......

效果图

实战项目

为了方便大家更好的在项目中使用LRecyclerView,这里提供一个项目demo,有需要可以参考下!

github地址: https://github.com/jdsjlzx/Community

Gradle

Step 1. 在你的根build.gradle文件中增加JitPack仓库依赖。

    allprojects {
             repositories {
                 jcenter()
                 maven { url "https://jitpack.io" }
             }
         }

Step 2. 在你的module的build.gradle文件中增加LRecyclerView依赖。

implementation 'com.github.jdsjlzx:LRecyclerView:1.5.4.3'

LRecyclerView requires at minimum Java 7 or Android 4.0.

JavaDoc

https://jitpack.io/com/github/jdsjlzx/LRecyclerView/1.5.4.3/javadoc/

##项目简述

  1. 下拉刷新、滑动到底部自动加载下页数据;
  2. 可以方便添加Header和Footer;
  3. 头部下拉样式可以自定义;
  4. 具备item点击和长按事件;
  5. 网络错误加载失败点击Footer重新请求数据;
  6. 可以动态为FooterView赋予不同状态(加载中、加载失败、滑到最底等);
  7. 可以根据不同的viewtype自定义item视图;
  8. 具备类似IOS侧滑删除菜单功能;
  9. 完善的局部刷新效果;

注意:

  1. EndlessLinearLayoutActivity.java类里面有标准完整的使用方法,请尽量在这个界面看效果;
  2. 本着解耦的原则,能在demo中实现的就尽量不在libray中实现。
  3. libray中的sdk版本都是最新版本,如果你不想处理申请权限的问题,可以在你本地的app的build.gradle中如下设置:
    compileSdkVersion 25
    buildToolsVersion '25.0.2'
        
    defaultConfig {
        applicationId "com.github.jdsjlzx"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 4
        versionName "0.5.3"
    }

targetSdkVersion设置为22即可。

Demo下载

点我下载

功能介绍

填充数据

    mDataAdapter = new DataAdapter(this);
    mDataAdapter.setData(dataList);

    mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
    mRecyclerView.setAdapter(mLRecyclerViewAdapter);
  1. DataAdapter是用户自己真正的adapter,用户自己定义;
  2. LRecyclerViewAdapter提供了一些实用的功能,使用者不用关心它的实现,只需构造的时候把自己的mDataAdapter以参数形式传进去即可。

添加HeaderView、FooterView

//add a HeaderView
mLRecyclerViewAdapter.addHeaderView(new SampleHeader(this));

//add a FooterView
mLRecyclerViewAdapter.addFooterView(new SampleFooter(this));

添加HeaderView还可以使用下面两种方式:

View header = LayoutInflater.from(this).inflate(R.layout.sample_header,(ViewGroup)findViewById(android.R.id.content), false);
mLRecyclerViewAdapter.addHeaderView(header);


CommonHeader headerView = new CommonHeader(getActivity(), R.layout.layout_home_header);
mLRecyclerViewAdapter.addHeaderView(headerView);

上面的方式同样适用于FooterView。

移除HeaderView、FooterView

//remove a HeaderView
mLRecyclerViewAdapter.removeHeaderView();

//remove a FooterView
mLRecyclerViewAdapter.removeFooterView();

注意:

1.如果有两个以上的HeaderView,连续调用mLRecyclerViewAdapter.removeHeaderView()即可。

LScrollListener-滑动监听事件接口

LScrollListener实现了onScrollUp()、onScrollDown()、onScrolled、onScrollStateChanged四个事件,如下所示:

void onScrollUp();//scroll down to up

void onScrollDown();//scroll from up to down

void onScrolled(int distanceX, int distanceY);// moving state,you can get the move distance

void onScrollStateChanged(int state);

  • onScrollUp()——RecyclerView向上滑动的监听事件;
  • onScrollDown()——RecyclerView向下滑动的监听事件;
  • onScrolled()——RecyclerView正在滚动的监听事件;
  • onScrollStateChanged(int state)——RecyclerView正在滚动的监听事件;

使用:

mRecyclerView.setLScrollListener(new LRecyclerView.LScrollListener() {
            @Override
            public void onScrollUp() {
            }

            @Override
            public void onScrollDown() {
            }

            @Override
            public void onScrolled(int distanceX, int distanceY) {
            }
            @Override
            public void onScrollStateChanged(int state) {

            }

        });
 

下拉刷新

mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                
            }
        });

加载更多

mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                
            }
        });

设置下拉刷新样式

mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头

AVLoadingIndicatorView库有多少效果,LRecyclerView就支持多少下拉刷新效果,当然你也可以自定义下拉刷新效果。

效果图:

这里写图片描述

自定义下拉刷新View

  1. 自定义view实现IRefreshHeader接口;
  2. 调用LRecyclerView提供的setRefreshHeader(IRefreshHeader refreshHeader)即可。
/**
 * 设置自定义的RefreshHeader
 */
public void setRefreshHeader(IRefreshHeader refreshHeader) {
    this.mRefreshHeader = refreshHeader;
}

设置下拉刷新Header和Footer文字内容和颜色

//设置头部加载颜色
mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white);
//设置底部加载颜色
mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white);
//设置底部加载文字提示
mRecyclerView.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊,点击再试一次吧");

记得设置ProgressStyle:

mRecyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

开启和禁止下拉刷新功能

mRecyclerView.setPullRefreshEnabled(true);

or

mRecyclerView.setPullRefreshEnabled(false);

默认是开启。

强制刷新

根据大家的反馈,增加了一个强制刷新的方法,使用如下:

mRecyclerView.forceToRefresh();

无论是下拉刷新还是强制刷新,刷新完成后调用下面代码:

mRecyclerView.refreshComplete(pageSize);
mLRecyclerViewAdapter.notifyDataSetChanged();

下拉刷新清空数据

有的时候,需要下拉的时候清空数据并更新UI,可以这么做:

@Override
public void onRefresh() {
    mDataAdapter.clear();
    mLRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
    mCurrentCounter = 0;
    requestData();
}

如果不需要下拉的时候清空数据并更新UI,如下即可:

@Override
public void onRefresh() {
    requestData();
}

开启和禁止自动加载更多功能

mRecyclerView.setLoadMoreEnabled(true);

or

mRecyclerView.setLoadMoreEnabled(false);;

默认是开启。如果不需要自动加载更多功能(也就是不需要分页)手动设置为false即可。

加载数据完成处理

mDataAdapter.addAll(list);
mRecyclerView.refreshComplete(REQUEST_COUNT);// REQUEST_COUNT为每页加载数量

如果没有更多数据(也就是全部加载完成),判断逻辑如下:

mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

                if (mCurrentPage < totalPage) {
                    // loading data
                    requestData();
                } else {
                    mRecyclerView.setNoMore(true);
                }
            }
        });

去除底部的加载更多的view

如果你的逻辑是一次性加载所有的数据,那么可以如下设置:

mRecyclerView.setLoadMoreEnabled(false);;

mRecyclerView.setOnLoadMoreListener就不需要设置了.

加载数据网络异常处理

加载数据时如果网络异常或者断网,LRecyclerView为你提供了重新加载的机制。

效果图:

这里写图片描述

网络异常出错代码处理如下:

mRecyclerView.setOnNetWorkErrorListener(new OnNetWorkErrorListener() {
                @Override
                public void reload() {
                    requestData();
                }
            });

上面的mFooterClick就是我们点击底部的Footer时的逻辑处理事件,很显然我们还是在这里做重新请求数据操作。

点击事件和长按事件处理

先看下怎么使用:

mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                
            }

        });

mLRecyclerViewAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                
            }
        });

原理就是实现viewHolder.itemView的点击和长按事件。由于代码过多就不贴出来了。

viewHolder源码如下:

public static abstract class ViewHolder {
        public final View itemView;
        int mPosition = NO_POSITION;
        int mOldPosition = NO_POSITION;
        long mItemId = NO_ID;
        int mItemViewType = INVALID_TYPE;
        int mPreLayoutPosition = NO_POSITION;
}

设置空白View(setEmptyView)

mRecyclerView.setEmptyView(view);

需要注意的是布局文件,如下所示:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <com.github.jdsjlzx.recyclerview.LRecyclerView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <include
        android:id="@+id/empty_view"
        layout="@layout/layout_empty"
        android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>

关于添加分割线

经过不断优化,LRecyclerView支持了ItemDecoration,使用如下所示:

LinearLayoutManager布局设置如下:

DividerDecoration divider = new DividerDecoration.Builder(this,mLRecyclerViewAdapter)
                .setHeight(R.dimen.default_divider_height)
                .setPadding(R.dimen.default_divider_padding)
                .setColorResource(R.color.split)
                .build();
mRecyclerView.addItemDecoration(divider);

GridLayoutManager布局设置如下:

int spacing = getResources().getDimensionPixelSize(R.dimen.dp_4);
mRecyclerView.addItemDecoration(SpacesItemDecoration.newInstance(spacing, spacing, manager.getSpanCount(), Color.GRAY));

//根据需要选择使用GridItemDecoration还是SpacesItemDecoration
GridItemDecoration divider = new GridItemDecoration.Builder(this)
        .setHorizontal(R.dimen.default_divider_padding)
        .setVertical(R.dimen.default_divider_padding)
        .setColorResource(R.color.split)
        .build();
//mRecyclerView.addItemDecoration(divider);

根据需要选择使用GridItemDecoration还是SpacesItemDecoration,SpacesItemDecoration(支持多类型布局)

同样的,LuRecyclerView也支持了ItemDecoration,只是命名稍微不同(类名以Lu开头,如LuGridItemDecoration、LuSpacesItemDecoration)

滑动删除

效果图:

分组

效果图:

这里写图片描述

功能还在完善中....

PullScrollView、PullWebView也有实现,为了解耦,这两个类都放在了demo中,有需要的可以自己修改使用!

代码混淆

#LRecyclerview
-dontwarn com.github.jdsjlzx.**
-keep class com.github.jdsjlzx.progressindicator.indicators.** { *; }

如果你想了解更多混淆配置,参考:http://blog.csdn.net/jdsjlzx/article/details/51861460

注意事项

1.如果添加了footerview,不要再使用setLScrollListener方法,如有需要,自定义实现即可。如下面代码不要同时使用:

mRecyclerView.setLScrollListener(LScrollListener); 
mLRecyclerViewAdapter.addFooterView(new SampleFooter(this));

2.不要SwipeRefreshLayout与LRecyclerView一起使用,会有冲突,为了更好的满足广大用户,新增了LuRecyclerView类,可以与SwipeRefreshLayout搭配使用,详细请参考SwipeRefreshLayoutActivity类的实现。

3.关于RecyclerView自动滑动的问题

这个自动滑动归根结底是焦点问题,子item有焦点,导致RecyclerView自动滑动到了子item,在根布局上加了android:descendantFocusability="blocksDescendants",根view来处理焦点,不传给子view就能解决问题。

4.关于LRecyclerView嵌套RecyclerView滑动卡顿的问题

可以参考:https://github.com/jdsjlzx/LRecyclerView/issues/165

LRecyclerView的应用

效果图:

代码详见:https://github.com/jdsjlzx/Community

Thanks

1.SwipeDelMenuViewGroup

问题反馈

QQ交流群1:183899857
QQ交流群2:250468947

打赏

觉得本框架对你有帮助,不妨打赏赞助我一下,让我有动力走的更远。

支付宝

86906d726b0cd6a0c59066ae6ea87e7ca064deea

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

原项目地址:https://github.com/jdsjlzx/LRecyclerView 按照其使用方式: Step 1. 在你的根build.gradle文件中增加JitPack仓库依赖。 allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } } Step 2. 在你的module的build.gradle文件中增加LRecyclerView依赖。 implementation 'com.github.jdsjlzx:LRecyclerView:1.5.4.3' LRecyclerView requires a... 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化