簡體   English   中英

將RecyclerView強制布局到頁面底部

[英]Force RecyclerView to bottom of page in layout

我有一個由ImageViewRecyclerView組成的頁面。 RecyclerView包含少量項目(目前為三項),僅占我測試設備屏幕的四分之一左右。 然而,盡管嘗試了許多布局選項,但我無法讓RecyclerView有效地包裝其內容並占用足夠的空間來包含這三行,並為ImageView留下剩余的空間。

為了說明我的意思,我畫了兩張圖。 第一部分顯示了我想要發生的事情,第二部分顯示了發生的事情:

這是正確的布局這是當前(不正確)的布局

到目前為止,我已經嘗試了幾種不同的RelativeLayout組合 - 例如,我將設置RecyclerView layout:align_ParentBottom和包含ImageView的第二個RelativeLayout layout:alignComponent ,使其底部與RecyclerView頂部匹配(這通常會拖動ImageView布局,以填補任何提醒空間,這是我想要發生的。)

RecyclerView雖然只占用了所有空間,但它只包含幾行。 我目前的“解決方案”是將所有內容放在LinearLayout ,並將較少的gravity設置為RecyclerView ,但這並不理想,因為在不同的模擬器上它不會完全與屏幕底部RecyclerView ,而在其他模擬器中則沒有沒有足夠的空間, RecyclerView變得可滾動。

提前感謝任何人提供的任何幫助和建議。

非常感謝所有貢獻者,但我在layout文件之外找到了一個編程解決方案。 如果其他人正在尋找這個問題的解決方案,我在這里找到了一個。

似乎RecyclerView目前存在一個問題,即它不包裝內容。 答案是構造一個擴展LinearLayoutManager的自定義類。 我已經發布了下面適合我的解決方案 - 大部分都是從我引用的鏈接中給出的答案中復制和粘貼的。 唯一的小問題是它沒有考慮裝飾添加的額外空間,這就是為什么我必須對代碼末尾附近的下一行做一個小調整:

//I added the =2 at the end.    
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;

以下是完整的代碼:

public class HomeLinearLayoutManager extends LinearLayoutManager{

    HomeLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    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);
        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }
        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

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

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        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() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
            recycler.recycleView(view);
        }
    }
}

再次感謝。

將兩者放在RelativeLayout中並使ImageView填充父級:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/recyclerView"
    />
</RelativeLayout>

編輯:意外寫了TextView。 固定。

我能想到的唯一解決方案是使用布局權重。 指定70%的圖像屏幕和30%的Recyclerview因為您說只有3行。 使用adjustViewByBounds確保維持圖像寬高比。

我的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <ImageView
        android:src="@drawable/ic_round_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_weight=".9"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".1"/>
</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM