2016-07-25 105 views
0

當活動打開時,它顯示RecyclerView佈局的頂部而不是活動佈局的頂部。開活動開始佈局跳轉到RecyclerView的頂部

活動佈局.xml文件:

<ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingBottom="20dp" 
       android:paddingTop="20dp"> 

      ... 
      <RelativeLayout/> 
      <View /> 
      <LinearLayout/> 
      ... 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/venue_place_info_gallery_recycler_view" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp"/> 

      </LinearLayout> 
    </ScrollView> 

活動的onCreate:

RecyclerView galleryRecyclerView = (RecyclerView) findViewById(R.id.venue_place_info_gallery_recycler_view); 
    galleryRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(gridLayoutColumns, StaggeredGridLayoutManager.VERTICAL)); 
    VenueGalleryAdapter venueGalleryAdapter = new VenueGalleryAdapter(VenuePlaceInfoActivity.this, images); 
    galleryRecyclerView.setAdapter(venueGalleryAdapter); 

適配器非常簡單。它在構造函數中接收圖像作爲參數,並且以後不能更改數據。 我試過將各種設置應用於RecyclerView佈局,但它的作用與使用或不使用它們相同。例如:

galleryRecyclerView.setNestedScrollingEnabled(false); 
    galleryRecyclerView.setHasFixedSize(true); 
    galleryRecyclerView.setLayoutFrozen(true); 
    galleryRecyclerView.setPreserveFocusAfterLayout(false); 

UPDATE:

我發現更多的信息上的answer of another question主題。這完全是因爲即使將setNestedScrollingEnabled設置爲true,ScrollView和RecyclerView也無法一起生活。但它仍然不能解決我的問題。我需要在RecyclerView畫廊上方放置一些東西,並且我想向下滾動瀏覽所有圖像(而不是將它們放入容器中)。

+0

你可以附加任何截圖嗎? –

+0

刪除galleryRecyclerView.setPreserveFocusAfterLayout(false); – vinoth12594

+0

我已經說過這些額外的參數目前被刪除。我以前只是嘗試過,但他們沒有改變一件事。 – Galya

回答

0

如果你想讓你的佈局看起來不錯,我會使用CoordinatorLayout和AppBarLayout作爲其中的第一個元素,並將RecyclerView作爲第二個元素。在AppBarLayout內部,你應該把你想要的東西放在RecyclerView之上。一些與此類似:

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/white"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:elevation="0dp"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsing_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:titleEnabled="false"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:clipChildren="false" 
        android:clipToPadding="false" 
        android:orientation="vertical" 
        android:paddingTop="20dp" 
        app:layout_collapseMode="parallax"> 

        <!-- stuff you want above your recyclerview --> 

       </LinearLayout> 
      </android.support.design.widget.CollapsingToolbarLayout> 
     </android.support.design.widget.AppBarLayout> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    </android.support.design.widget.CoordinatorLayout> 
</LinearLayout> 

有人做類似這裏使用工具欄的東西,而不是僅僅在appbarlayout,你可以使用自己喜歡的任意視圖佈局。 AppBarLayout是一個擴展的垂直LinearLayout:http://www.basagee.tk/handling-scrolls-with-coordinatorlayout/

+0

這可能不是我尋找的簡單而簡單的解決方案,但它可以完成這項工作,所以它是公認的答案。我編輯它以添加爲我工作的實際代碼。 – Galya