2017-02-13 103 views
0

我瀏覽過很多帖子,但無法獲得我需要的帖子。我有一個帶有RecyclerView的LinearLayout,我希望兩者都可以滾動。現在,LinearLayout是固定的,RecyclerView是唯一可以滾動的。我嘗試了nestedscrollview,但無法使其工作。出於某種原因,recyclerview.adapter使用nestedscrollview分解。任何想法? BTW:我在Xamarin開發AndroidLinearLayout和RecyclerView不會一起滾動

這是我的佈局

<LinearLayout 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     ... 
    </LinearLayout> 
    <FrameLayout 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

然後,我在其中佈局是FrameLayout里加載一個片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<ProgressBar 
    android:id="@+id/spinner" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" /> 
<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/refresher" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:scrollbars="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</android.support.v4.widget.SwipeRefreshLayout> 
<TextView 
    android:id="@+id/empty_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</LinearLayout> 

回答

0

LinearLayouts本身不滾動,他們只是爲了方便地以線性的方式一個接一個地觀看一個又一個的視圖。如果你想得到一個滾動,你需要將它嵌套在ScrollView中。

鑑於你正在努力完成的任務,我不知道在ScrollView裏面嵌套一個RecyclerView是否會起作用。 (我還沒有證實)

你想用LinearLayout的行爲是什麼?它應該包含頁眉和頁腳?如果是這樣,您可以通過爲您的RecyclerView創建獨特的ViewHolders來更好地服務您的RecyclerView(即您的標題,一個用於頁腳,另一個用於常規內容)。然後,您的適配器根據索引指出要顯示哪種類型的ViewHolder。 (例如0 - > Header,n + 2 - > Footer,1:n-1 - > Content(偏移1))

如果您只是在尋找一個靜態頭文件與所述內容的其餘部分,就可以實現這很容易地:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="5"> 

    <!-- Replace this with your header view --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="Your Header" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="4" > 

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/refresher" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recyclerView" 
       android:scrollbars="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" /> 
     </android.support.v4.widget.SwipeRefreshLayout> 

     <ProgressBar 
      android:id="@+id/spinner" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" /> 
    </RelativeLayout> 
</LinearLayout> 

這將使您將佔用含有視圖的總高度的1/5的頭部視圖RecyclerView(包括含有其。視圖和進度條)的視圖將包含剩餘的包含視圖總高度的五分之四。

+0

這是一個標題,但考慮到我的解決方案,創建標題視圖不是最好的主意r爲recyclerview。我使用LinearLayout來組織視圖。語義上是一個標題。您是否建議將LinearLayout更改爲其他佈局? –

+0

這取決於。當您滾動時,您是否希望標題消失?或者你想讓它永遠在場?您是否希望頁腳只在滾動到底部時出現?你可以保持LinearLayout *不帶* ScrollView,在那裏插入標題,並且只有第二個元素是RecyclerView(它會自己滾動)。 爲什麼定義Header ViewHolders不是您理想的解決方案? – themarshal

+0

假設我希望它像第一個項目。我不希望它消失,只是爲了頂部。定義標題ViewHolders並不理想,因爲recyclerview是我在其他視圖中使用的組件,其他視圖中我不需要標題。如果您不知道解決方案,這很複雜,但請相信我,最好將其分開。我沒有頁腳,只有標題。我不明白你給我的解決方案,你有一個例子/教程嗎?謝謝! –