2016-03-03 27 views
2

我在NestedScrollView中使用Header实现了RecyclerView。但我得到滚动问题的位置。当活动暂停然后恢复时,位置始终是自动滚动焦点到RecyclerView。在NestedScrollView中执行RecyclerView时发出位置滚动

enter image description here

我的XML:

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

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="4dp"> 

       <LinearLayout 
        android:id="@+id/header" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Header" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textStyle="bold" /> 

       </LinearLayout> 


       <android.support.v7.widget.RecyclerView 
        android:id="@+id/grid_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/header" 
        android:layout_gravity="center_horizontal|top" /> 
      </RelativeLayout> 
     </android.support.v4.widget.NestedScrollView> 

那么如何解决这个问题?

+0

嗨,我一直停留在同样的问题了一个多星期了。你有没有找到解决办法呢? :) – edwinj

+0

@edwinj http://stackoverflow.com/a/36645305/842607 –

回答

-1

如果您希望标头保持原位,则不应使用滚动视图。

<LinearLayout 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Header" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/grid_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/header" 
     android:layout_gravity="center_horizontal|top" /> 
    </RelativeLayout> 
</LinearLayout> 

如果您想报头滚动到屏幕的顶部,然后在您的适配器,你必须声明你有2种不同的视图类型,与头是一个与细胞在休息Recycler查看其他。

您需要返回不同的视图,然后根据位置(您将返回位置0的标题视图,以及任何其他位置的“正常”单元格)。

+0

我想滚动标题太先生,.. –

+0

然后,你需要声明你的适配器有2种视图类型,并分配一个头。在滚动视图中包装recyclerview是一个糟糕的主意,因为您会遇到冲突的触摸事件。 – Francesc

2

这应该可以解决你的问题:

  <RelativeLayout 
       android:descendantFocusability="blocksDescendants" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="4dp"> 
相关问题