gpt4 book ai didi

android - linearLayoutmaneger.findLastCompletelyVisibleItemPosition 在 coordinatorLayout 中返回错误值

转载 作者:行者123 更新时间:2023-11-29 19:01:49 25 4
gpt4 key购买 nike

我在 coordinatorLayout 中有一个 recyclerView,我想从中获取最后一个可见的项目:

<?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>

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

问题是在开始 findLastCompletelyVisibleItemPosition() 方法返回时,例如,14 是错误的,12 是正确的,在我向下滚动到第 14 项后,findLastCompletelyVisibleItemPosition()再次返回 14。
我知道这是因为 AppBarLayout 和 CoordinatorLayout 但我找不到正确的方法来找出最后一个完整的可见项目位置是什么。

最佳答案

我已经设法找到解决此问题的方法

您应该为您的 RecyclerView 使用自定义 LayoutManager 并覆盖查找第一个和最后一个位置的方法

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

关于android - linearLayoutmaneger.findLastCompletelyVisibleItemPosition 在 coordinatorLayout 中返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com