Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cannot resolve method 'findFirstVisibleItemPosition()'?

I'm trying to write a code for endless scroll on a recycler view. This is the snippet that gives me a compiler error:

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

                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if ( (visibleItemCount+pastVisiblesItems) >= totalItemCount) {
                    Log.v("...", "Last Item Wow !");
                }

And the declaration I've written before is:

mLayoutManager = new LinearLayoutManager(this);

And mLayoutManager is an object of class RecyclerView.LayoutManager

like image 577
ScreenSeer Avatar asked Mar 29 '15 08:03

ScreenSeer


1 Answers

mLayoutManager is an object of class RecyclerView.LayoutManager is wrong, you should use android.support.v7.widget.LinearLayoutManager for mLayoutManager, so:

mLayoutManager = new LinearLayoutManager(this); 
//above 'LinearLayoutManager' is from
//'android.support.v7.widget.LinearLayoutManager'

mRecyclerView.setLayoutManager(mLayoutManager);

then mLayoutManager.findFirstVisibleItemPosition(); call should be ok in onScrolled(...);.

Hope this help!

like image 82
Xcihnegn Avatar answered Oct 10 '22 01:10

Xcihnegn