问题描述
我已经在嵌套滚动视图中实现了recycleview.但是将视图循环滚动到位置方法无效.
I have implemented a recycleview inside a nested scroll view. But recycle view scroll to position methods are not working.
下面是我的示例代码
<?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);
推荐答案
这是我解决此问题的方式
This is how I resolve this issue
首先获取需要使用以下方法滚动的回收视图位置
first get recycle view position that need to scroll using below method
final float y = recyclerView.getChildAt(selectedItem.getPos()).getY();
然后将嵌套的滚动视图滚动到该位置 nestedScrollingView.post(new Runnable(){@Override公共无效run(){nestedScrollingView.fling(0);nestedScrollingView.smoothScrollTo(0,(int)y);}});
Then scroll nested scroll view to that position
nestedScrollingView.post(new Runnable() { @Override public void run() { nestedScrollingView.fling(0); nestedScrollingView.smoothScrollTo(0, (int) y); } });
别忘了在nestedscrollview上添加 android:fillViewport ="true"
Don't forget to add android:fillViewport="true"
on nestedscrollview
这篇关于Recycleview滚动到Nestedscrollview内部不起作用的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!