2

My android project includes a recyclerView that contains a list of cardViews, and also there is a swipeRefreshLayout on the top of this recyclerView. When I scroll down the list and pull up those cardViews, I just want to disable swipeRefreshLayout. In other word, when RecyclerView is not on the first item, and if the user wants to scrolling back to first item, it must not show swipeRefreshLayout.

I googled a lot about this issue and there are some solutions for this problem that overrides onScrollStateChanged method, but they not behave very smooth and still swipeRefreshLayout remains enabled in some situations.

EDIT 1: Following links are include some of these solutions I mentioned above:

https://stackoverflow.com/a/27042911/4257703

https://gist.github.com/NikolaDespotoski/1a6bb83dbae133f67812

Here is my xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:choiceMode="none"
            android:focusable="false"
            android:listSelector="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center" />
    </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>

EDIT 2: Today I realized that my broblem is occured because of implementing Tabs and swipeRefreshLayout together. For refreshing the data of Fragment which contains RecyclerView, user must drag the page to bottom, and in other hand for switching between tabs, user must drag the screen to right or left. Due to this touch gestures, some bugs and lags occur in scrolling my list. Please help me to address this problem. Thanks a lot.

Community
  • 1
  • 1
mohammad
  • 90
  • 2
  • 12

2 Answers2

10

Maybe I am late, but have a try to this solution:

mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(getActivity());    // a LinearLayoutManager
mRecyclerView.setLayoutManager(mLayoutManager);             // setting layoutManager on our RecyclerView

// Adding ScrollListener to getting whether we're on First Item position or not
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        mSwipeRefreshLayout.setEnabled(mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0); // 0 is for first item position
    }
});

mSwipeRefreshLayout is your SwipeRefreshLayout

After putting above code, you'll be able to swipe only when your First item is visible.

Hope this helps!

Meet Vora
  • 2,783
  • 1
  • 16
  • 33
0

Here is the fix:

public class SwipeRefreshLayoutToggleScrollListener extends RecyclerView.OnScrollListener {
private List<RecyclerView.OnScrollListener> mScrollListeners = new ArrayList<RecyclerView.OnScrollListener>();
private int mExpectedVisiblePosition = 0;
private SwipeRefreshLayout mSwipeLayout;

public SwipeRefreshLayoutToggleScrollListener(SwipeRefreshLayout swipeLayout) {
    mSwipeLayout = swipeLayout;
}
public void addScrollListener(RecyclerView.OnScrollListener listener){
    mScrollListeners.add(listener);
}
public boolean removeScrollListener(RecyclerView.OnScrollListener listener){
    return mScrollListeners.remove(listener);
}
public void setExpectedFirstVisiblePosition(int position){
    mExpectedVisiblePosition = position;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    notifyScrollStateChanged(recyclerView,newState);
    LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
    int firstVisible = llm.findFirstCompletelyVisibleItemPosition();
    if(firstVisible != RecyclerView.NO_POSITION)
        mSwipeLayout.setEnabled(firstVisible == mExpectedVisiblePosition);

}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    notifyOnScrolled(recyclerView, dx, dy);
}
private void notifyOnScrolled(RecyclerView recyclerView, int dx, int dy){
    for(RecyclerView.OnScrollListener listener : mScrollListeners){
        listener.onScrolled(recyclerView, dx, dy);
    }
}
private void notifyScrollStateChanged(RecyclerView recyclerView, int newState){
    for(RecyclerView.OnScrollListener listener : mScrollListeners){
        listener.onScrollStateChanged(recyclerView, newState);
    }
}
}

for more info check this

URL:https://gist.github.com/NikolaDespotoski/1a6bb83dbae133f67812

YLS
  • 1,475
  • 2
  • 15
  • 35
  • I visited this link too before, but I don't have any idea about how use this class in my project. Please guide me what will be the usage of the object of this class? – mohammad Oct 13 '15 at 14:18
  • if you post your class file, it would help me to fix it. because you get this problem in some situation not always. – YLS Oct 13 '15 at 14:26
  • I tried this solution, still the problems remained. when recyclerview is not on the first item and user try to scrollback to top, swipeRefreshLayout is seen on the screen and prevent scrolling. – mohammad Oct 13 '15 at 14:43