【问题标题】:Scroll to Particular Position in (Vertical & Horizontal Scroll Enabled) Grid Layout - RecyclerView - Android滚动到(启用垂直和水平滚动)网格布局中的特定位置 - RecyclerView - Android
【发布时间】:2017-09-15 12:06:17
【问题描述】:

我已经用 GridLayoutManager 实现了一个 RecyclerView,它同时具有 Vertical 和 Horizo​​ntal Scroll ,我需要将布局滚动到特定位置,引用了许多代码,但对我来说并没有成功。

Java 类代码:

void RackGen()
    {
        STRarray = (String[]) mStringList.toArray(new String[mStringList.size()]);
        ROWarray = (String[]) mRowList.toArray(new String[mRowList.size()]);

        numcol = Integer.parseInt(col);
        numdata = Integer.parseInt(num);

        rv = (RecyclerView) findViewById(R.id.rview);
        int numberOfColumns = numcol;

        GridLayoutManager GM2 = new GridLayoutManager(this,numberOfColumns);

        rv.setLayoutManager(GM2);



        rv.setNestedScrollingEnabled(true);
        rv.setHasFixedSize(true);
        adapter = new MyRecyclerViewAdapter(this, STRarray, ROWarray);
        rv.setAdapter(adapter);

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/llzoom"
    android:orientation="vertical"
    >

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_scroll_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    xmlns:android="http://schemas.android.com/apk/res/android">

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:overScrollMode="never">

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rview">

        </android.support.v7.widget.RecyclerView>


        </HorizontalScrollView>
                </ScrollView>
        </android.support.v4.widget.NestedScrollView>
</LinearLayout>

只有在添加嵌套滚动视图以及垂直和水平滚动后,滚动才能在两个方向上顺利进行。 现在我需要滚动到特定位置。

谢谢

【问题讨论】:

    标签: android android-layout scroll android-recyclerview gridlayoutmanager


    【解决方案1】:

    您需要在活动中引用您的 NestedScrollView ,然后在该对象上调用scrollTo(x,y),例如:

    NestedScrollView nestedScrollView  = (NestedScrollView) findViewById(R.id.nested_scroll_view);
    nestedScrollView.scrollTo(x,y);
    

    x 和 y 是水平和垂直滚动。

    【讨论】:

    • 我刚试过,考虑到 (x,y) 手动给 (3,6) 一个 (8,10) 网格,但它没有成功。并且 .. 到目前为止,适配器位置只有一个值,例如 (8) ,根据给定的数据,这些值被分为列数。
    【解决方案2】:

    我知道这是一篇较早的帖子,我还没有在嵌套滚动视图上测试过这段代码,但由于你只有一个位置值,你可以试试这个技术。

    这非常适合滚动到具有动态自动调整功能的 recyclerview GridLayoutManager 的当前选定视图(我使用自定义 recyclerview 进行自动调整,而不是网格本身)。

    设置适配器后:

    recyclerView.setAdapter(adapter);
    

    我把(就在 onCreate 的下面):

    recyclerView.post(new Runnable()
    {
        @Override
        public void run()
        {
            layoutManager.scrollToPositionWithOffset(currentSelected, 0);
        }
    });
    

    它等待视图被绘制,然后滚动到给定的位置。效果很好!

    我的想法在这里:https://stackoverflow.com/a/52895262/2905354

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 2021-11-13
      • 2019-04-13
      • 2019-12-11
      • 1970-01-01
      • 2014-01-09
      相关资源
      最近更新 更多