强制RecyclerView到页面布局底部 [英] Force RecyclerView to bottom of page in layout
问题描述
我有一个网页,包含一个的ImageView
,而在 RecyclerView
。该 RecyclerView
包含少量项目(目前有三个),只占用了大约四分之一的画面我的测试设备上。然而,尽管尝试多种布局选项,我不能让 RecyclerView
来有效地包装其内容,并采取了只包含这三个行所需的足够的空间,并留下的休息空间我的的ImageView
。
I have a page that consists of an ImageView
, and a RecyclerView
. The RecyclerView
contains a small number of items (currently three) and only takes up around a quarter of the screen on my test device. However, despite trying numerous layout options, I cannot get the RecyclerView
to effectively wrap its content and take up just enough space required to contain these three rows, and leave the rest of the space for my ImageView
.
要帮助说明我的意思,我得出两个图表。第一次显示了我想发生,第二个发生了什么:
To help illustrate what I mean, I have drawn two diagrams. The first shows what I would like to happen, and the second what is happening:
到目前为止,我已经试过 RelativeLayout的
的几种不同的组合 - 例如,我将设置 RecyclerView
到布局:align_ParentBottom
第二 RelativeLayout的
包含的ImageView
来布局:alignComponent
,以便其底部的 RecyclerView
顶级赛事(这通常会拖的ImageView
的布局,使其充满任何reminaing空间,这是我想的事情发生。)
So far, I have tried several different combinations of RelativeLayout
- for instance, I will set RecyclerView
to layout:align_ParentBottom
and the second RelativeLayout
that contains the ImageView
to layout:alignComponent
so that its bottom matches the RecyclerView
top (this would normally drag the ImageView
layout so that it fills any reminaing space, which is what I would like to happen.)
在 RecyclerView
虽然只是一直占据所有空间是可以的,即使它仅包含少数行。目前的解决方案我是把一切都在的LinearLayout
里面,设置较少重力
到 RecyclerView
,但它不是理想的,因为在不同的模拟器,它不会排队完全与屏幕的底部,而在其他没有足够的空间和 RecyclerView
变为滚动的。
The RecyclerView
though just keeps occupying all the space it can, even though it only contains a few rows. The current "solution" I have is to place everything inside a LinearLayout
and set less gravity
to the RecyclerView
, but it isn't ideal, because on different emulators it wont line up completely with the bottom of the screen, and in others there isn't enough room and the RecyclerView
becomes scrollable.
在此先感谢您的帮助和建议,任何人都可以提供。
Thanks in advance for any help and suggestions anyone can offer.
推荐答案
非常感谢大家谁作出了贡献,但我已经找到了布局
文件之外的编程解决方案。如果其他人正在寻找一个解决这个问题,我发现了一个<一个href="http://stackoverflow.com/questions/26649406/nested-recycler-view-height-doesnt-wrap-its-content">here.
Many thanks to everyone who contributed, but I have found a programmatic solution outside of the layout
files. In case anyone else is looking for a solution to this problem, I found one here.
看起来好像有一个问题, RecyclerView
目前,它不换行的内容。答案是构建一个扩展 LinearLayoutManager
自定义类。我已经发布,以下为我工作的解决方案 - 大部分是复制并在我引用的链接中给出的答案粘贴。唯一的小问题是,它并没有考虑通过装饰增加了额外的空间,这就是为什么我不得不做出一个小调整到接近的code月底下面一行:
It appears as if there is an issue with RecyclerView
currently where it doesn't wrap content. The answer is to construct a custom class that extends LinearLayoutManager
. I have posted the solution that worked for me below - most of it is copy and pasted from the answer given in the link I quoted. The only small issue is that it doesn't account for the extra space added by decorations, which is why I had to make a small tweak to the following line near the end of the code:
//I added the =2 at the end.
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
下面是code的全部内容:
Here is the code in its entirety:
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);
}
}
}
再次感谢。
这篇关于强制RecyclerView到页面布局底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!