2015-08-13 79 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(); 
    } 
} 

}