Get Visible Items in Recyclerview

Get visible items in RecyclerView

First / last visible child depends on the LayoutManager.
If you are using LinearLayoutManager or GridLayoutManager, you can use

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

For example:

GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();

For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may prefer to layout more items than visible for caching.

how to get current visible item in Recycler View

 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);

Getting current visible item in RecyclerView

I can only think of the solution as follows.
Trigger onClickListener every time the user scrolls.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentPosition = llm.findFirstVisibleItemPosition();
downloadFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
}
}
}

How to check if first item of RecyclerView is visible on the screen

You can find some helper methods in RecyclerView.LayoutManager, for example, if you use a LinearLayoutManager, check these methods:

int findFirstCompletelyVisibleItemPosition() // Returns the adapter position of the first fully visible view.
int findFirstVisibleItemPosition() // Returns the adapter position of the first visible view.
int findLastCompletelyVisibleItemPosition() // Returns the adapter position of the last fully visible view.
int findLastVisibleItemPosition() // Returns the adapter position of the last visible view.

See the full docs here.

In your code:

recyclerView.setAdapter(adapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (layoutManager.findFirstVisibleItemPosition() > 0) {
mAccountLayout.setVisibility(View.GONE);
mDateLayout.setVisibility(View.GONE);
Log.d("SCROLLINGDOWN","SCROLL");
} else {
mAccountLayout.setVisibility(View.VISIBLE);
mDateLayout.setVisibility(View.VISIBLE);
Log.d("SCROLLINGUP","SCROLL");
}
}
});

RecyclerView in ConstraintLayout. Show only complety visible items on a screen

class HideLastDecorator() : RecyclerView.ItemDecoration() {

override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
val count = parent.childCount
for (i in 0 until count) {
parent.getChildAt(i).visibility = if (count == i - 1) View.INVISIBLE else View.VISIBLE
}
}
}

and add it to your recyclerView Decorations

appsRecyclerView.addItemDecoration(HideLastDecorator())

Sorry for Kotlin :)

View.INVISIBLE is important, because if the View becomes GONE, it will be removed from the measuring of the RecyclerView's content and the new ViewHolder would be added.

I prefer to work careful with OnClickListener if any is set for the ViewHolder's content.



Related Topics



Leave a reply



Submit