Git Product home page Git Product logo

android-pulltorefreshrecyclerview's Introduction

Android-PullToRefreshRecyclerView

This is a project based on RecyclerView with pull-to-refresh feature.

It support addding Header, auto-loading more data when scrolling to bottom.

It can help you to implement ListView effect in RecyclerView, and not affect exsiting RecyclerView and Adapter logic.

Screenshot

Latest version:v1.3.1

How to make custom LoadMoreView, see 'DemoLoadMoreView.class' in demo project.

Feature

  • Encapsulation based on native RecyclerView
  • Pull-to-refresh
  • Auto load when scrolling to the bottom
  • Most API of like ListView's
  • Add Header easily (Not supported by native RecyclerView)
  • Support EmptyView settings
  • Current LayoutManager supported:
    • LinearLayoutManager
    • GridLayoutManager

Project site: https://github.com/HomHomLin/Android-PullToRefreshRecyclerView.

Sample

There has a Sample in project:Sample

##Using library in your application

Gradle dependency:

compile 'homhomlin.lib:ptrrv-library:1.3.1'

or

Maven dependency:

<dependency>
	<groupId>homhomlin.lib</groupId>
	<artifactId>ptrrv-library</artifactId>
	<version>1.3.1</version>
</dependency>

If you want your application work on Android 2.x, you should add this in your gradle :

compile 'com.nineoldandroids:library:2.4.0'

##Usage

PullToRefreshRecyclerView is easy to use just like ListView and RecyclerView. It is derived from native SwipeRefreshLayout, so all property of SwipeRefreshLayout can be used here.

See Sample for detail.

First: Config in xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.lhh.ptrrv.library.PullToRefreshRecyclerView
        android:id="@+id/ptrrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

Second: Find it in your Activity

PullToRefreshRecyclerView mPtrrv = (PullToRefreshRecyclerView) this.findViewById(R.id.ptrrv);

Third: Config it in java code

// custom own load-more-view and add it into ptrrv
DemoLoadMoreView loadMoreView = new DemoLoadMoreView(this, mPtrrv.getRecyclerView());
loadMoreView.setLoadmoreString(getString(R.string.demo_loadmore));
loadMoreView.setLoadMorePadding(100);

mPtrrv.setLoadMoreFooter(loadMoreView);

//remove header
mPtrrv.removeHeader();

// set true to open swipe(pull to refresh, default is true)
mPtrrv.setSwipeEnable(true);

// set the layoutManager which to use
mPtrrv.setLayoutManager(new LinearLayoutManager(this));

// set PagingableListener
mPtrrv.setPagingableListener(new PullToRefreshRecyclerView.PagingableListener() {
    @Override
    public void onLoadMoreItems() {
        //do loadmore here
    }
});

// set OnRefreshListener
mPtrrv.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // do refresh here
    }
});

// add item divider to recyclerView
mPtrrv.getRecyclerView().addItemDecoration(new DividerItemDecoration(this,
        DividerItemDecoration.VERTICAL_LIST));

// add headerView
mPtrrv.addHeaderView(View.inflate(this, R.layout.header, null));

//set EmptyVIEW
mPtrrv.setEmptyView(View.inflat(this,R.layout.empty_view, null));

// set loadmore String
mPtrrv.setLoadmoreString("loading");

// set loadmore enable, onFinishLoading(can load more? , select before item)
mPtrrv.onFinishLoading(true, false);

Finally: Set the adapter which extends RecyclerView.Adpater

PtrrvAdapter mAdapter = new PtrrvAdapter(this);
mPtrrv.setAdapter(mAdapter);

##License Copyright 2015 LinHongHong

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.

android-pulltorefreshrecyclerview's People

Contributors

archerding avatar homhomlin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-pulltorefreshrecyclerview's Issues

当我自定义加载更多View的时候,传入和DefaultLoadMoreView一样代码的对象。

当我自定义加载更多View的时候,传入和DefaultLoadMoreView一样代码的对象。结果如果没有网络的情况下,如果不调用setLoadMoreFooter(loadMoreView)而用默认的没有热河问题,可是如果我调用setLoadMoreFooter(loadMoreView)传入和DefaultLoadMoreView一样代码的对象,当我向上滑动多试验几次,下面就会流出空白,不知道怎么回事

Cannot call this method in a scroll callback. Scroll callbacks might be run during a measure & layout pass where you cannot change the RecyclerView data. Any method call that might change the structure of the RecyclerView or the adapter contents should be postponed to the next frame

I got this error when scroll recyclerview. It drop frame so much.
How to fix this ,guy?
Thanks!

Cannot call this method in a scroll callback. Scroll callbacks might be run during a measure & layout pass where you cannot change the RecyclerView data. Any method call that might change the structure of the RecyclerView or the adapter contents should be postponed to the next frame. java.lang.IllegalStateException: at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2458) at android.support.v7.widget.RecyclerView$LayoutManager.assertNotInLayoutOrScroll(RecyclerView.java:6760) at android.support.v7.widget.LinearLayoutManager.assertNotInLayoutOrScroll(LinearLayoutManager.java:1244) at android.support.v7.widget.RecyclerView.removeItemDecoration(RecyclerView.java:1395) at com.lhh.ptrrv.library.PullToRefreshRecyclerView.setHasMoreItems(PullToRefreshRecyclerView.java:334) at com.lhh.ptrrv.library.PullToRefreshRecyclerView.access$800(PullToRefreshRecyclerView.java:22) at com.lhh.ptrrv.library.PullToRefreshRecyclerView$InterOnScrollListener.onScrolled(PullToRefreshRecyclerView.java:389) at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4384) at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3462) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3194) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579) at android.widget.LinearLayout.onLayout(LinearLayout.java:1488) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579) at android.widget.LinearLayout.onLayout(LinearLayout.java:1488) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579) at android.widget.LinearLayout.onLayout(LinearLayout.java:1488) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579) at android.widget.LinearLayout.onLayout(LinearLayout.java:1488) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2934) at android.view.View.layout(View.java:16639) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.

loadmore加载不出来

如果写死适配器中的item数量,loadmore可以加载出来。动态去适配就出不来了。

ScrollListerner

我找到这个方法没有作用的原因了:
qq 20160616145824
这是因为该addOnScrollListener()方法执行的RecycleView的方法,并不是自定义的addOnScrollListener();
addonscroll
所以导致自定义的addOnScrollListener()里面给mOnScrollLinstener()赋值失败!
addonscroll

最后自定义的InterOnScrollListener里面的判断进不去,所以方法失效!
default

Crash

java.lang.IllegalStateException: Cannot remove item decoration during a scroll  or layout

    at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2771)
    at android.support.v7.widget.RecyclerView$LayoutManager.assertNotInLayoutOrScroll(RecyclerView.java:7523)
    at android.support.v7.widget.LinearLayoutManager.assertNotInLayoutOrScroll(LinearLayoutManager.java:1352)
    at android.support.v7.widget.RecyclerView.removeItemDecoration(RecyclerView.java:1566)
    at com.lhh.ptrrv.library.PullToRefreshRecyclerView.setHasMoreItems(PullToRefreshRecyclerView.java:334)
    at com.lhh.ptrrv.library.PullToRefreshRecyclerView.access$800(PullToRefreshRecyclerView.java:22)
    at com.lhh.ptrrv.library.PullToRefreshRecyclerView$InterOnScrollListener.onScrolled(PullToRefreshRecyclerView.java:389)
    at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4844)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3909)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3540)
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1756)
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795)
    at android.view.Choreographer.doCallbacks(Choreographer.java:598)
    at android.view.Choreographer.doFrame(Choreographer.java:566)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781)
    at android.os.Handler.handleCallback(Handler.java:810)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:189)
    at android.app.ActivityThread.main(ActivityThread.java:5532)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

下拉刷新视图闪烁 错位

复现:

  1. RecyclerView 界面初始化后
  2. 手指按住先上拉,不松手,再下拉 触发下拉刷新。
    此时,下拉刷新视图 闪烁 错位。

头部扩展局限性很大

如果头部控件为listview或者GridView,或者ScroolView等,只要头部内容展示的高度超过屏幕高度,则会出现RecyclerView内部部分无法展示,下拉刷新圈圈也只会在第一次生效

无法展现EmptyView

setEmptyView后,在我的adapter数据为空时,没有展现我之前设置的view。还有能不能提供RecyclerView中的scrollToPosition方法?方便可以会滚到列表顶部。谢谢!

Wrong state class, expecting View State but received class android.support.v7.widget.RecyclerView$SavedState instead

Process: com.haimai.baletu, PID: 27332
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.haimai.baletu/com.haimai.main.activity.MainActivity}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.support.v7.widget.RecyclerView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/main_house_list_recyclerview. Make sure other views do not use the same id.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2330)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.support.v7.widget.RecyclerView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/main_house_list_recyclerview. Make sure other views do not use the same id.
at android.view.View.onRestoreInstanceState(View.java:13807)
at com.handmark.pulltorefresh.library.PullToRefreshBase.onRestoreInstanceState(PullToRefreshBase.java:833)
at android.view.View.dispatchRestoreInstanceState(View.java:13783)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2888)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2894)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2894)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2894)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2894)
at android.view.View.restoreHierarchyState(View.java:13761)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:471)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1105)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1241)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2053)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1979)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1241)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2053)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
at android.app.ActivityThread.access$800(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5279) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705) 

似乎和一些别的库冲突

看了下好像setItemAnimator,addItemDecoration这个方法都没有提供,内部有用到,如果是这样,好像会和stickyheadersrecyclerview冲突?

mPtrrv.setOnRefreshComplete() not working

Hi HomHomLin,
Thanks for your wonderful porject.
When I use your sample project, I find that
"mPtrrv.setOnRefreshComplete() " seems not working.
Please help check it.
Thanks.

11楼说的问题!

@OverRide
public void onFinishLoading(boolean hasMoreItems, boolean needSetSelection) {

    if (getLayoutManager() == null) {
        return;
    }

    if (!hasMoreItems && mLoadMoreFooter != null) {
        //if it's last line, minus the extra height of loadmore

// mCurScroll = mCurScroll - mLoadMoreFooter.getLoadMorePadding();
// @2016/6/23
// 上拉再下拉再上拉,始终没有出现加载更多view的话,会多减一个rootview的高度,暂时先注释,或者是该方法onFinishLoading(true,false)第一个参数始终传值true
}

    // if items is too short, don't show loadingview
    if (getLayoutManager().getItemCount() < mLoadMoreCount) {
        hasMoreItems = false;
    }

    setHasMoreItems(hasMoreItems);

    isLoading = false;

    if (needSetSelection) {
        int first = findFirstVisibleItemPosition();
        mRecyclerView.scrollToPosition(--first);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.