【问题标题】:Recycleview scroll to a position not working inside NestedscrollviewRecyclerview 滚动到在 Nestedscrollview 内不起作用的位置
【发布时间】:2018-05-22 09:37:29
【问题描述】:

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

下面是我的示例代码

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

【问题讨论】:

  • 但是为什么你在 NestedScrollView 中使用了 RecyclerView?
  • 因为我在recycleview之外还有一些其他的看法
  • @Darshana 测试尝试ViewCompat.setNestedScrollingEnabled(recyclerView,false); 而不是stackoverflow.com/a/45190337/7666442
  • @NileshRathod 不适合我
  • @Darshana 如何解决这个问题?如果您得到解决方案,请发布您的答案谢谢!

标签: android


【解决方案1】:

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

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

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"

【讨论】:

  • 谢谢,它对我有用,还有一个步骤:final float y = recyclerView.getY() + recyclerView.getChildAt(adapterPosition).getY(); scrollView.post(() -&gt; { scrollView.fling(0); scrollView.smoothScrollTo(0, (int) y); });
【解决方案2】:

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

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

    scrollView.smoothScrollTo(0, y)

【讨论】:

  • 我已经尝试了该站点上的所有内容,这是最终对我有用的一种解决方案。很容易忘记 NestedScrollView 控制滚动并感到困惑。小编辑:y 现在需要是“int”而不是 float。
【解决方案3】:
<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"/>

移除 NestedScrollView 并添加

app:layout_behavior="@string/appbar_scrolling_view_behavior"

到您的 Recyclerview。然后 ScrollToPosition 工作正常。

【讨论】:

    【解决方案4】:

    试试这个,

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

    【讨论】:

      【解决方案5】:

      使用这个:

      yourRecyclerView.setNestedScrollingEnabled(false);
      

      它可能会解决您的问题。

      【讨论】:

        【解决方案6】:

        你可以使用这个扩展功能。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-26
          • 1970-01-01
          • 1970-01-01
          • 2020-06-01
          • 1970-01-01
          • 2017-02-05
          • 2019-05-25
          • 1970-01-01
          相关资源
          最近更新 更多