2016-03-03 31 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"> 
相關問題