2015-08-13 37 views
7

我试图做一个有色的空视图占用RecyclerView后的剩余空间。我查看了不同的StackOverflow解决方案,但没有任何效果。基本上我想要这样的东西:让RecyclerView后空视图占用剩余空间?

________________________ 
|      | 
|      | 
|      | 
| Recycler view  | 
|      | 
| ------------------- | 
| colored empty view | 
|      | 
|      | 
|      | 
|      | 
|      | 
_________________________ 

It should shrink as the RecyclerView grows. 

________________________ 
|      | 
|      | 
|      | 
| Recycler view  | 
|      | 
|      | 
|      | 
|      | 
|      | 
| ------------------- | 
| colored empty view | 
|      | 
|      | 
_________________________ 

但是,如果RecyclerView超过一个屏幕的价值,应该没有空视图。这是我到目前为止有:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:fab="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/bla_bla_layout"> 


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

    <View 
     android:background="@color/grayBackground" 
     android:layout_gravity="bottom" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <com.melnykov.fab.FloatingActionButton 
     android:id="@+id/fab" 
     android:contentDescription="@string/add_bla" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_margin="16dp" 
     android:src="@drawable/add_bla_icon" 
     fab:fab_colorNormal="@color/primary" 
     fab:fab_colorPressed="@color/primary_pressed" 
     fab:fab_colorRipple="@color/ripple" /> 

</FrameLayout> 

编辑 这不是提到的问题的副本,你可以看到这一点,如果你读这两个问题。我的问题的解决方案应该是纯粹的xml,基于布局。通过“空视图”,我的意思是在现有的RecyclerView下面有一些彩色矩形,而不是RecylerView中没有数据时实际上显示为“空视图”的文本视图。

+2

如果你要downvote我的问题,请告诉我为什么...... – Tariq

+0

可能的重复[如何显示与RecyclerView空视图?](http://stackoverflow.com/questions/28217436/how-to-show-an-empty-view-with-a-recyclerview) –

+0

这根本不是我的问题。他的问题甚至不是一个布局问题。相信我,我已经看到了所有,并尝试了许多类似问题的解决方案。如果你找到一个可以工作的东西,我会很高兴。 – Tariq

回答

5

Recycler视图在运行时计算其高度,我们使用LinearLayoutManager指定其高度和宽度。

但问题是,现在LinearLayoutManager不支持wrap_content它总是使用match_parent来表示它的高度。

所以你必须使用这个LinearLayoutManager而不是android.support.v7.widget.LinearLayoutManager。

复制上面的Java类在你的包,并使用它像下面,

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, 
      LinearLayoutManager.VERTICAL, false); 
recyclerView.setLayoutManager(linearLayoutManager); 
// if it has fixed size 
recyclerView.setHasFixedSize(true); 

我测试了它和它的工作,因为你需要。

+0

此作品谢谢。不过,我在这里使用了更简单的LinearLayoutManger扩展版本:http://stackoverflow.com/questions/26649406/nested-recycler-view-height-doesnt-wrap-its-content/28510031#28510031这也考虑到了刷卡,拖动排序和项目装饰。 – Tariq

+0

P.S.当时间限制到来时,我会奖励赏金,它会让我。谢谢! – Tariq

0

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bla_bla_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/bla_bla_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/array_sample" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@color/grayBackground" /> 

</LinearLayout> 
+0

不起作用。 RecyclerView只占用整个空间。无论如何,我必须使用FrameLayout,我认为是因为FAB。 – Tariq

+0

虽然你可以将'LinearLayout'放置在'FrameLayout'中,对吧? –

+0

是的,我也试过。它具有相同的效果。 RecyclerView只占用了所有的空间。 – Tariq

0

它看起来像你可以用RelativeLayout使用一些对齐命令完成。

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

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

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

     <View 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/recycler_view" 
      android:layout_alignParentBottom="true" 
      android:background="@color/blue"/> 

    </RelativeLayout> 

</FrameLayout> 

我包括FrameLayout,因为你在你需要的时候用FAB使用另一则留言中提到。

如果您将其加载到AndroidStudio并开始设置RecyclerView高度的样本宽度(50dp,150dp,300dp ...),您将在其下面看到View,并开始调整它的高度。

+0

由于RecyclerView占用整个空间,所以再次不起作用。我试图将视图的高度设置为0,并将权重设置为1,但这不起作用。尝试与RecyclerView相同,同时保持查看在match_parent高度,但也没有工作。我的RecyclerView内容是动态设置的。我不知道这个问题,但谢谢你的尝试。 – Tariq

+0

那么你有在RecyclerView占用整个屏幕的项目?如果你的收藏品中有足够多的物品来填充屏幕,我不会看到这是可能的。 –

+0

不,现在我只是测试一个占用大约十分之一屏幕的单个项目。但是当我尝试滚动时,我从超滚动效果中知道RecyclerView正在占用整个屏幕。 – Tariq

0

如果您只是想在剩余空间中使用不同的颜色,请将FrameLayout的背景设置为所需颜色,并将RecyclerView项目的layout背景设置为白色。

这将导致剩余空间为单独的颜色,并且根据需要,recyclerView中的填充项目的长度将为白色。

如果你想在剩下的空间像textView或ImageView的其他视图,我想你将不得不延长RecyclerView classoverride计算它的高度的方法。

0

使用自定义的LinearLayoutManager,希望能解决您的问题。

使用此代码在您的活动

RecycleView rv= (RecyclerView) rootView.findViewById(R.id.recycler_view); 
    CustomLinearLayoutManager lm= new CustomLinearLayoutManager(mActivity); 
    lm.setOrientation(LinearLayoutManager.VERTICAL); 
    lm.setHasFixedSize(true); 
    rv.setLayoutManager(lm); 

这里是我的CustomLinearLayoutManager

public class CustomLinearLayoutManager extends LinearLayoutManager { 

public CustomLinearLayoutManager(Context context) { 
    super(context); 
} 

private int[] mMeasuredDimension = new int[2]; 

@Override 
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, 
         int widthSpec, int heightSpec) { 
    final int widthMode = View.MeasureSpec.getMode(widthSpec); 
    final int heightMode = View.MeasureSpec.getMode(heightSpec); 
    final int widthSize = View.MeasureSpec.getSize(widthSpec); 
    final int heightSize = View.MeasureSpec.getSize(heightSpec); 

    measureScrapChild(recycler, 0, 
      View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
      View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
      mMeasuredDimension); 

    int width = mMeasuredDimension[0]; 
    int height = mMeasuredDimension[1]; 

    switch (widthMode) { 
     case View.MeasureSpec.EXACTLY: 
     case View.MeasureSpec.AT_MOST: 
      width = widthSize; 
      break; 
     case View.MeasureSpec.UNSPECIFIED: 
    } 

    switch (heightMode) { 
     case View.MeasureSpec.EXACTLY: 
     case View.MeasureSpec.AT_MOST: 
      height = heightSize; 
      break; 
     case View.MeasureSpec.UNSPECIFIED: 
    } 

    setMeasuredDimension(width, height); 
} 

private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, 
           int heightSpec, int[] measuredDimension) { 
    try { 
     View view = recycler.getViewForPosition(position); 
     if (view != null) { 
      RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); 
      int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, 
        getPaddingLeft() + getPaddingRight(), p.width); 
      int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, 
        getPaddingTop() + getPaddingBottom(), p.height); 
      view.measure(childWidthSpec, childHeightSpec); 
      measuredDimension[0] = view.getMeasuredWidth(); 
      measuredDimension[1] = view.getMeasuredHeight(); 
      recycler.recycleView(view); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

}