2015-10-11 16 views
12

我有一个显示动态内容的RecyclerView。我会在其他视图之间显示此RecyclerView。例如,一个视图上方和下方RecyclerView显示一个视图中显示:查看上方和下方RecyclerView

View 
RecyclerView 
View 

然而,无论我尝试似乎并没有被工作和RecyclerView似乎占据整个空间。我相信问题在于wrap_content issue,尽管我已经尝试了一些建议的解决方案,例如this custom LinearLayoutManager,但似乎无法解决我的问题。

尝试:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <!-- AppBar works fine; no issue here --> 
     <android.support.design.widget.AppBarLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:id="@+id/app_bar_layout"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_width="match_parent" 
       app:layout_collapseMode="pin"/> 

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

     <!-- Not displaying; Tried different views --> 
     <com.chrynan.taginput.view.TextInputWrapper 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text_container"> 
      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/edit_text"/> 
     </com.chrynan.taginput.view.TextInputWrapper> 

     <!-- Takes up entire screen space --> 
     <android.support.v4.widget.SwipeRefreshLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/swipe_refresh"> 

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

     </android.support.v4.widget.SwipeRefreshLayout> 

     <!-- Not displaying --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/text_view"/> 

    </LinearLayout> 

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

问:有没有办法来放置和上面显示视图和RecyclerView下方查看?如果是这样,怎么样?还是使用ListView更好?

回答

25

怎么样的weight的使用情况如何?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://..." 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"   
    android:orientation="vertical" 
    > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 
+0

因为我的布局比我给出的例子稍微复杂一些,或者因为使用RecyclerView的已知错误,所以只需添加layout_weight即可。但是,我决定使用ListView而不是RecyclerView,并将layout_weight添加到ListView的确给了我期望的效果。 – chRyNaN

+1

很好的答案。这对我来说工作得很好。 – Mythul

+0

Excellent Super Awesome ...... –

1

这可能不是您的典型解决方案,但您可以尝试。

如何使用RelativeLayout而不是LinearLayout,并简单地将SwipeRefreshLayout的顶部和底部填充设置为视图的高度,如下所示。

<android.support.design.widget.CoordinatorLayout 
     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:fitsSystemWindows="true"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <!-- your app bar --> 

    <!-- Not displaying; Tried different views --> 
    <com.chrynan.taginput.view.TextInputWrapper 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:layout_alignParentTop="true" 
     android:id="@+id/edit_text_container"/> 

    <!-- Takes up entire screen space --> 
    <android.support.v4.widget.SwipeRefreshLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="150dp" 
     android:paddingBottom="250dp" 
     android:id="@+id/swipe_refresh"> 

     <android.support.v7.widget.RecyclerView 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/recycler_view"/> 

    </android.support.v4.widget.SwipeRefreshLayout> 

    <!-- bottom view --> 
    <TextView 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="250dp" 
     android:id="@+id/text_view"/> 

</RelativeLayout> 

+1

不幸的是,这是不是一个可行的解决方案,因为我的顶部和底部的意见动态地改变大小。 – chRyNaN

+0

为什么不重置填充也是动态的? – Ari

+0

动态填充动态不适合我。我导致使用ListView,并在其上添加了layout_weight属性,这产生了我期望的效果。谢谢你的帮助。 – chRyNaN