2016-07-25 66 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