【问题标题】:How to scroll to a particular position in recyclerview inside scrollview?如何滚动到scrollview内recyclerview中的特定位置?
【发布时间】:2017-04-08 01:16:55
【问题描述】:

我搜索了很多关于我的查询的内容,但没有一个有用。我在根父布局内的滚动视图中有一个 recyclerview 和一些静态数据,如下所示。

我已经设置了 -

scrollview.scrollto(0,0);

因为每次我打开活动时,它都会跳转到 recyclerview firstitem,它会跳过 recyclerview 上方的静态数据。

recyclerview.setNestedScrollingEnabled(false); 
recyclerview.setfocusable(false);

用于平滑滚动。

问题出在——

layoutmanager.scrollToPositionWithOffset(pos,0);

它不工作。在将适配器设置为 recyclerview 后,我已经设置了上述行。也尝试使用 NestedScrollView 但徒劳无功。

虽然我用过

layoutmanager.scrollToPosition(pos);

对于那些跳过代码的人,我已将 match_parent 设置为 ScrollView 和 fillviewport 为 true。

这是我的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">

    <RelativeLayout
        android:id="@+id/inclusionviewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <static data/>

        <ScrollView
            android:id="@+id/scrollviewmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/one"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_above="@+id/donelayout">


            <staticdata/>

                <TextView
                    android:id="@+id/dealstext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/card1"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:textSize="16sp"
                    android:text="Deals &amp; Coupons"
                    android:textColor="#444444" />

                <RelativeLayout
                    android:id="@+id/recyclerlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/dealstext"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@color/colorbackground">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/coupon_rec_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorbackground"
                        android:visibility="visible"/>


                </RelativeLayout>

                <statisdata>


            </RelativeLayout>

        </ScrollView>

        include layout="@layout/activity_coupons"
            android:visibility="gone"
            />

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

  • 你能发布一些你的android代码吗?你确定你引用的是正确的layoutManager吗?不是RecyclerView 而是ScrollView 对吗?
  • 您想在旋转时保留滚动位置,或者只是以编程方式想要滚动到某个位置?
  • 粘贴您的适配器和相关的活动/片段代码以及@akshay_shahane。

标签: android android-recyclerview scrollview android-nestedscrollview linearlayoutmanager


【解决方案1】:

需要通过获取RecyclerView 的孩子的顶部位置来滚动ScrollView

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
scrollView.smoothScrollTo(0, (int) y);

【讨论】:

  • X是零速吗?
  • (不看)我希望它与 jquery 类似,在 scrollto 方法上有速度和 y。
  • 在我的例子中,scrollView 正在移动,但它没有到达指定的适配器位置。有什么想法吗?
  • @msrowley 如果您的 RecyclerView 不是 XML 文件中的第一个元素,则将 RecyclerView 的 Y 位置添加到孩子的 Y 位置。
【解决方案2】:

您可以使用滚动视图方法到达您想要的位置:

scrollView.smoothScrollTo(int x, int y);

【讨论】:

    【解决方案3】:

    我知道我回答得很晚,但如果您遇到自动滚动问题,您需要在您的 RelativeLayout 中添加 android:descendantFocusability="blocksDescendants"。因此,您的 RealtiveLyout 标记将如下所示:

    <RelativeLayout
        android:id="@+id/inclusionviewgroup"
        android:layout_width="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:layout_height="match_parent">
    

    【讨论】:

      【解决方案4】:

      第 1 步:添加此行以在回收站视图中选择该位置

        adaptercat.setSelectedItem(Integer.parseInt(dealtypeid));
      

      第 2 步:通过获取当前位置检查下面的构造函数

        public CatgerotyAdapter(Context context, ArrayList<HashMap<String, String>> arraylist,String selectedPosition) {
              this.context = context;
              this.data = arraylist;
              resultp = new HashMap<>();
              currentItemPos=Integer.parseInt(selectedPosition)-1;
          }
      

      第 3 步:这个功能也在适配器中

        public void setCurrentItem(int item)
          {
              this.currentItemPos = item;
          }
      

      最后一步 4. 在你的类的适配器之外添加这个函数

       private void onPagerItemClick2(Integer tag)
      {
          adaptercat.setCurrentItem(tag);
          catsp.setAdapter(adaptercat);
      }
      

      将此行放入您的列表视图项单击列表中

        ((ClassName) context).onPagerItemClick2(position);
      

      你什么时候打开listview会选择那个位置..

      【讨论】:

        【解决方案5】:

        使用此方法scrollToPosition (int position) 这对我有用。希望这会对你有所帮助。

                mMessagesRecyclerView = (RecyclerView) findViewById(R.id.messagesRecyclerView);
                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                layoutManager.setStackFromEnd(true);
        
                mMessagesRecyclerView.setLayoutManager(layoutManager);
                mMessagesAdapter = new MessagesAdapter();
                mMessagesRecyclerView.setAdapter(mMessagesAdapter);
        
                mMessagesRecyclerView.scrollToPosition(your position);
        

        这是我的布局结构器

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-17
          • 2015-07-29
          • 1970-01-01
          • 2018-10-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多