How disable swiperefreshlayout when Recyclerview is not on the first item?

前端 未结 2 1163
眼角桃花
眼角桃花 2021-01-21 21:44

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

相关标签:
2条回答
  • 2021-01-21 21:55

    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

    0 讨论(0)
  • 2021-01-21 22:04

    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!

    0 讨论(0)
提交回复
热议问题