Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linearLayoutmaneger.findLastCompletelyVisibleItemPosition returns wrong value in coordinatorLayout

I have a recyclerView in coordinatorLayout and I want to get last visible Item from it:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#f00"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lastVisibleItem"
    android:text="last position"
    />


 </android.support.design.widget.CoordinatorLayout>

and

findViewById(R.id.lastVisibleItem).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position=((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();

            Log.d(TAG, "onClick: position="+position);
            Toast.makeText(RecyclerViewActivity.this, "position="+position, Toast.LENGTH_SHORT).show();
        }
    });

the problem is in start findLastCompletelyVisibleItemPosition() method return, for example, 14 which is wrong and 12 is right and after I scroll down to item 14 findLastCompletelyVisibleItemPosition() return 14 again.
I know that's because of AppBarLayout and CoordinatorLayout but I cannot find proper way to find out what's right last complete visible item position is.

like image 934
max Avatar asked Apr 30 '26 12:04

max


2 Answers

I've managed to find a workaround for this issue

You should use custom LayoutManager for your RecyclerView and override methods for finding first and last positions

public final class FixedForAppBarLayoutManager extends LinearLayoutManager {

public FixedForAppBarLayoutManager(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public FixedForAppBarLayoutManager(final Context context, final int spanCount) {
    super(context, spanCount);
}

public FixedForAppBarLayoutManager(final Context context, final int spanCount, final int orientation, final boolean reverseLayout) {
    super(context, spanCount, orientation, reverseLayout);
}

@Override
public int findFirstVisibleItemPosition() {
    final View item = findVisibleItem(0, getChildCount(), false);
    return item == null ? -1 : getPosition(item);
}

@Override
public int findFirstCompletelyVisibleItemPosition() {
    final View item = findVisibleItem(0, getChildCount(), true);
    return item == null ? -1 : getPosition(item);
}

@Override
public int findLastVisibleItemPosition() {
    final View item = findVisibleItem(getChildCount() - 1, -1, false);
    return item == null ? -1 : getPosition(item);
}

@Override
public int findLastCompletelyVisibleItemPosition() {
    final View item = findVisibleItem(getChildCount() - 1, -1, true);
    return item == null ? -1 : getPosition(item);
}

private View findVisibleItem(final int fromIndex, final int toIndex, final boolean isCompletely) {
    final int next = toIndex > fromIndex ? 1 : -1;
    for (int i = fromIndex; i != toIndex; i += next) {
        final View child = getChildAt(i);
        if (checkIsVisible(child, isCompletely)) {
            return child;
        }
    }

    return null;
}

private boolean checkIsVisible(final View child, final boolean isCompletely) {
    final int[] location = new int[2];
    child.getLocationOnScreen(location);

    final View parent = (View) child.getParent();
    final Rect parentRect = new Rect();
    parent.getGlobalVisibleRect(parentRect);

    if (getOrientation() == HORIZONTAL) {
        final int childLeft = location[0];
        final int childRight = location[0] + child.getWidth();
        return isCompletely
            ? childLeft >= parentRect.left && childRight <= parentRect.right
            : childLeft <= parentRect.right && childRight >= parentRect.left;
    } else {
        final int childTop = location[1];
        final int childBottom = location[1] + child.getHeight();
        return isCompletely
            ? childTop >= parentRect.top && childBottom <= parentRect.bottom
            : childTop <= parentRect.bottom && childBottom >= parentRect.top;
    }
}

}

like image 80
Dm Dungeon Avatar answered May 04 '26 12:05

Dm Dungeon


Answer below works like a charm! Maybe for someone kotlin variant can be useful:

import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager

class FixedForAppBarLayoutManager(context: Context?) : LinearLayoutManager(context) {

    override fun findFirstVisibleItemPosition(): Int {
        val item: View? = findVisibleItem(0, childCount, false)
        return if (item == null) -1 else getPosition(item)
    }

    override fun findFirstCompletelyVisibleItemPosition(): Int {
        val item: View? = findVisibleItem(0, childCount, true)
        return if (item == null) -1 else getPosition(item)
    }

    override fun findLastVisibleItemPosition(): Int {
        val item: View? = findVisibleItem(childCount - 1, -1, false)
        return if (item == null) -1 else getPosition(item)
    }

    override fun findLastCompletelyVisibleItemPosition(): Int {
        val item: View? = findVisibleItem(childCount - 1, -1, true)
        return if (item == null) -1 else getPosition(item)
    }

    private fun findVisibleItem(
            fromIndex: Int, 
            toIndex: Int, 
            isCompletely: Boolean
    ): View? {
        val next = if (toIndex > fromIndex) 1 else -1
        var i = fromIndex
        while (i != toIndex) {
            val child: View? = getChildAt(i)
            child?.let {
                if (checkIsVisible(child, isCompletely)) {
                    return child
                }
                i += next
            }
        }
        return null
    }

    private fun checkIsVisible(child: View, isCompletely: Boolean): Boolean {
        val location = IntArray(2)
        child.getLocationOnScreen(location)
        val parent: View = child.parent as View
        val parentRect = Rect()
        parent.getGlobalVisibleRect(parentRect)
        return if (orientation == HORIZONTAL) {
            val childLeft = location[0]
            val childRight: Int = location[0] + child.width
            if (isCompletely) {
                childLeft >= parentRect.left && childRight <= parentRect.right
            } else {
                childLeft <= parentRect.right && childRight >= parentRect.left
            }
        } else {
            val childTop = location[1]
            val childBottom: Int = location[1] + child.height
            if (isCompletely) {
                childTop >= parentRect.top && childBottom <= parentRect.bottom
            } else {
                childTop <= parentRect.bottom && childBottom >= parentRect.top
            }
        }
    }
}
like image 23
Orest Hredil Avatar answered May 04 '26 12:05

Orest Hredil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!