2017-02-13 105 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是我在其他视图中使用的组件,其他视图中我不需要标题。如果您不知道解决方案,这很复杂,但请相信我,最好将其分开。我没有页脚,只有标题。我不明白你给我的解决方案,你有一个例子/教程吗?谢谢! –