Asked  7 Months ago    Answers:  5   Viewed   18 times

I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...) method on ListViews. I tried to work with View.getGlobalVisibleRect(...), but that hack is too ugly and does not always work too.

Someone any ideas?



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.

Tuesday, June 1, 2021
answered 7 Months ago

The RecyclerView.Adapter is a parameterized class that takes in a single ViewHolder type. Your definition probably looks like:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MenuItemViewHolder>

It should be:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>

Don't forget to have the right definition for onBindViewHolder as well. You need to override getItemViewType as well.

Wednesday, August 4, 2021
answered 4 Months ago

You call the setter inside of the setter.. a.k.a. infinite loop:

    set(value) {
        /* execute setter logic */
        chatManger = value

Inside a property getter or setter there is an additional variable available: field. This represents the java backing field of that property.

    get() = field
    set(value) {
        field = value

With a regular var property, these getters and setters are auto-generated. So, this is default behaviour and you don't have to override getter / setter if all you do is set the value to a field.

Tuesday, August 31, 2021
answered 3 Months ago

When you hit back from an activity you are popping it from the stack i.e the activity object will be destroyed. This is explained in further detail here. The crux of the concept can be understood from this picture.

You can look at this to see how you should recreate your activity - and this to see how you should apply that to a recyclerview.

Wednesday, September 1, 2021
answered 3 Months ago

For You need to calculate the width of the screen and set the width dynamically below is the my code

Add below code in your ViewHolder initilisation

llImg = (LinearLayout) itemView.findViewById(;

            llImg.getLayoutParams().width = (int) (Utils.getScreenWidth(itemView.getContext()) / 2);
            llImg.getLayoutParams().height = (int) (Utils.getScreenWidth(itemView.getContext()) / 2);

            imgView = (ImageView) itemView.findViewById(;

The Layout file is here



Make one

public static int getScreenWidth(Context context) {

        if (screenWidth == 0) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            screenWidth = size.x;
        return screenWidth;

Hope this will help you !

Saturday, October 9, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :