回收视图滚动到嵌套滚动视图中不起作用的位置



我在嵌套滚动视图中实现了回收视图。但是回收视图滚动到位置方法不起作用。

下面是我的示例代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

以下是滚动的方法

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
smoothScroller.setTargetPosition(pos);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);

这就是我解决此问题的方式

首先使用以下方法获取需要滚动的回收视图位置

final float y = recyclerView.getChildAt(selectedItem.getPos()).getY();

然后将嵌套的滚动视图滚动到该位置

nestedScrollingView.post(new Runnable() {
@Override
public void run() {
nestedScrollingView.fling(0);
nestedScrollingView.smoothScrollTo(0, (int) y);
}
});

不要忘记在嵌套滚动视图上添加android:fillViewport="true"

问题是你需要滚动NestedScrollView,而不是RecyclerView。例如:

final float y = recyclerView.getChildAt(selectedItem.getPos()).getY(); 
scrollView.smoothScrollTo(0, y)
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:title="Scroll">
<ImageView
android:id="@+id/toolbarImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="@mipmap/ic_launcher"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

删除嵌套滚动视图并添加

app:layout_behavior="@string/appbar_scrolling_view_behavior"

然后 ScrollToPosition 工作正常。

您可以使用此扩展函数。

fun RecyclerView.nestedScrollTo(position:Int,nestedScrollView: NestedScrollView) {
val y = getChildAt(position).y
nestedScrollView.fling(0)
nestedScrollView.smoothScrollTo(0,y.toInt())
}

试试这个,

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.scrollToPosition(position);
}
}, 200);

使用这个:

yourRecyclerView.setNestedScrollingEnabled(false);

它可能会解决您的问题。

就我而言,它是这样工作的。

Handler(Looper.getMainLooper()).postDelayed({
binding.nestedScrollView.smoothScrollTo(
0,
binding.recyclerview.measuredHeight,
500
)
// binding.nestedScrollView.scrollTo(0, binding.recyclerview.measuredHeight)
// binding.nestedScrollView.smoothScrollTo(0, binding.recyclerview.measuredHeight)
}, 50L)

最新更新