我正在尝试使一个彩色的空视图占用RecyclerView之后的剩余空间。 我看过不同的StackOverflow解决方案,但没有任何效果。 本质上,我想要这样的东西:
________________________
| |
| |
| |
| Recycler view |
| |
| ------------------- |
| colored empty view |
| |
| |
| |
| |
| |
_________________________
It should shrink as the RecyclerView grows.
________________________
| |
| |
| |
| Recycler view |
| |
| |
| |
| |
| |
| ------------------- |
| colored empty view |
| |
| |
_________________________
但是,如果RecyclerView超出一个屏幕的值,则不应有空白视图。 这是我到目前为止的内容:
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:id="@+id/bla_bla_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:background="@color/grayBackground"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
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" />
编辑
这不是所提到问题的重复项,如果您阅读了两个问题,都可以看到。 我的问题的解决方案应该纯粹是基于布局的xml。"空视图"是指仅在现有RecyclerView下面的一个彩色矩形,而不是在RecylerView中没有数据时实际上显示"空视图"的文本视图。
如果您不赞成我的问题,请告诉我原因...
如何使用RecyclerView显示空视图的可能重复项?
那根本不是我的问题。 他的问题实际上甚至不是布局问题。 相信我,我到处都是,并尝试了许多解决类似问题的方法。 如果您发现一个可行的方法,我会很高兴的。
回收站视图是否总是小于屏幕的一个高度,而实际上却从未在其上滚动?
嗯,我的RecylerView始终不至少是屏幕的一个高度。 通过超滚动效果可以看出
stackoverflow.com/questions/27475178/
是什么使您无法为FrameLayout指定背景颜色并彻底摆脱空的View?
Recycler视图在运行时计算其高度,我们使用LinearLayoutManager来指定其高度和宽度。
但是问题是,现在LinearLayoutManager不支持wrap_content,它总是使用match_parent作为高度。
因此,您必须使用LinearLineManager而不是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);
我对其进行了测试,并根据需要进行了测试。
这工作谢谢。但是,我在这里使用了LinearLayoutManger的更简单扩展版本:stackoverflow.com/questions/26649406/,该版本还考虑了滑动,拖动排序和项目修饰。
附言时限到了,我会奖励我赏金。谢谢!
希望使用自定义的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();
}
}
}
如果您只想在剩余空间中使用其他颜色,请将FrameLayout的背景设置为所需的颜色,并将RecyclerView项目的layout背景设置为白色。
这将导致剩余空间使用单独的颜色,并且RecyclerView中填充项目的长度将根据您的需要为白色。
如果您想在剩余空间中找到其他视图,例如textView或ImageView,我想您将必须扩展RecyclerView class和override的计算高度的方法。
看起来您可以使用一些align命令使用RelativeLayout完成。
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/recycler_view"
android:layout_alignParentBottom="true"
android:background="@color/blue"/>
我包含FrameLayout是因为您在另一条评论中提到您需要它与FAB一起使用。
如果将其加载到AndroidStudio中并开始为RecyclerView的高度设置样本宽度(50dp,150dp,300dp ...),您将看到其下方的View也开始调整其高度。
再次无效,因为RecyclerView占据了整个空间。我尝试将"视图"的高度设置为0,将权重设置为1,但这没有用。在将View保持在match_parent高度的情况下,对RecyclerView进行了相同的尝试,但这也不起作用。我的RecyclerView内容是动态设置的。我不知道是什么问题,但感谢您的尝试。
那么,RecyclerView中是否有占据整个屏幕的项目?如果您的收藏夹中有足够多的物品填满屏幕,我看不到这是怎么可能的。
目前暂不,我只是测试一个占屏幕约十分之一的项目。但是,当我尝试滚动时,我从超滚动效果中得知RecyclerView占据了整个屏幕。
这不会起作用,因为绘制新元素时,RecyclerView不会更新其高度。但是有一个变通办法,您所需要的只是将RecyclerView保留在文件的最后,请检查我的答案。
尝试这个
android:id="@+id/bla_bla_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/bla_bla_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/array_sample" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/grayBackground" />
不起作用。 RecyclerView仅占用整个空间。无论如何,由于FAB,我必须使用FrameLayout。
但是,您可以将LinearLayout放在FrameLayout里面,对吗?
是的,我也尝试过。它具有相同的效果。 RecyclerView刚刚占用了所有空间。