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
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!
Related
This question already has answers here:
Get visible items in RecyclerView
(11 answers)
Closed 4 years ago.
How i can get the "RecyclerView" current visible item. already try different methods of recyclerview but i can't get the solution so please help and guide me
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
final int lastItem = firstVisibleItemPosition + visibleItemCount;
}
};
Declare LinearLayoutManger globally,
private LinearLayoutManager linearLayoutManager;
Initialize RecyclerView like this,
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
notificationListRecyclerView.setLayoutManager(linearLayoutManager);
notificationListRecyclerView.addOnScrollListener(recyclerViewOnScrollListener);
Please i am working on an android contact app which i need to load first 20 phone book contact to recycler view and use Onscroll listener to get the remaining contact to the recyclyer view when scrolling.
First add 20 item in Arraylist of Contact.on Scroll end add more item's and call adapter.notifyDataSetChanged() . This is the code to detect scroll end.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mRecyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
Log.i("Yaeye!", "end called");
// Add more items into contact Arraylist and call notifyDataSetChanged()
loading = true;
}
}
});
Note : Make sure you are using LinearLayoutManager as layout manager for RecyclerView.
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
How to implement pagination in recycler listview. I have to show 10 items per page. Can any one tell me how to deal with this?
You can add a scroll listener on recycler view and call the next paginated api on following condition:
recyclerList.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisibleItems = mLayoutManager
.findFirstVisibleItemPosition();
int currentPos = pastVisibleItems + visibleItemCount;
if (currentPos >= totalItemCount) {
callNextApi();
}
}
}
});
I think my code is ok, but for some reason I have this error. This is my code:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if(dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading)
{
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
loading = false;
Log.v("...", "Last Item Wow !");
//Do pagination.. i.e. fetch new data
}
}
}
}
});
I have already imported android.support.v7.widget.LinearLayoutManager and decleared
private RecyclerView recyclerView;
private RecyclerView.LayoutManager mLayoutManager;
Because there is no method like findFirstVisibleItemPosition in RecyclerView.LayoutManager (See documentation). You need to use LinearLayoutManager to use findFirstVisibleItemPosition
Change your declaration from this
private RecyclerView.LayoutManager mLayoutManager
to this
private LinearLayoutManager mLayoutManager
I try implement staggered RecyclerView. I create Abstract onScroll RecyclerView Like this :
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int grid_column_count = context.getResources().getInteger(R.integer.grid_column_count);
StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(grid_column_count, StaggeredGridLayoutManager.VERTICAL);
int visibleItemCount = recyclerView.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItemsGrid[] = new int[grid_column_count];
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0];
if ((visibleItemCount + firstVisibleItem ) >= totalItemCount
&& totalItemCount != 0) {
onLoadingMore();
}
}
protected abstract void onLoadingMore();
and I implenting by MainActity
//===============
recyclerview= (RecyclerView)rootView. findViewById(R.id.grid_view);
recyclerview.setHasFixedSize(true);
grid_column_count = getResources().getInteger(R.integer.grid_column_count);
mLayoutManager = new StaggeredGridLayoutManager(grid_column_count, StaggeredGridLayoutManager.VERTICAL);
recyclerview.setLayoutManager(mLayoutManager);
//=====================
recyclerview.addOnScrollListener(new HidingScrollListener(getActivity()) {
#Override
protected void onLoadingMore() {
if ( isFinishLoadingAwal
&& !isFinishMoreNews
&& adapter.getItemCount()> 0) {
getMoreNewsFromServer();
}
}
});
But I getting error in line :
firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0];
log :
Process: com.ad.kamardagang, PID: 23350
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.widget.OrientationHelper.getStartAfterPadding()' on a null object reference
at android.support.v7.widget.StaggeredGridLayoutManager$Span.findOneVisibleChild(StaggeredGridLayoutManager.java:2337)
at android.support.v7.widget.StaggeredGridLayoutManager$Span.findFirstVisibleItemPosition(StaggeredGridLayoutManager.java:2313)
at android.support.v7.widget.StaggeredGridLayoutManager.findFirstVisibleItemPositions(StaggeredGridLayoutManager.java:826)
at com.ad.kamardagang.utils.HidingScrollListener.onScrolled(HidingScrollListener.java:90)
at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3674)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2824)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
at android.view.View.layout(View.java:15671)
so how to fix it ? or any solution ?
sorry for my english.
I use this Mugen for endless scrolling for StaggeredGrid with RecyclerView.
Replace firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0]; with
firstVisibleItem = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); and try.