0

I want to user swipe refresh , so I need to make sure that I'm at the top of the recycleview .This is my code :

    LinearLayoutManager mLayoutManager;
recycle = (RecyclerView) findViewById(R.id.recycle);
        mLayoutManager = new LinearLayoutManager(this);
        recycle.setLayoutManager(mLayoutManager);

recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            boolean enable = false;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if(recycle != null && mLayoutManager.getChildCount() > 0){
                    boolean firstItemVisible = mLayoutManager.findFirstVisibleItemPosition() == 0;
                    boolean topOfFirstItemVisible = mLayoutManager.getChildAt(0).getTop() == 0;
                    Log.v("this",firstItemVisible + "  "+topOfFirstItemVisible);
                    //boolean topOfFirstItemVisible = recycle.getChildAt(0).getTop() == 0;
                    enable = firstItemVisible && topOfFirstItemVisible;
                }
                mSwipeRefreshLayout.setEnabled(enable);

            }
        });
    }

as you can see, I'm using a boolean and I want to check if recycleView was at the top , then it enable the swifeLayout .

How can I do so ? this code is not working and when I'm at the top of the recycleView , it doesn't enable the swipeLayout.

navidjons
  • 501
  • 4
  • 9
  • 15

1 Answers1

0

You could look into SwipeRefreshLayout, where all this is handled for you, and provides a conventional way of doing swipe refresh. See https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

If this is what you wan't to do. This is how it works:

https://www.bignerdranch.com/img/blog/2014/12/catnames.gif

Kenneth
  • 3,957
  • 8
  • 39
  • 62