Android 如何设置RecyclerView的最大高度?
如何设置RecyclerView的最大高度? 我有一个RecyclerView,如下所示:Android 如何设置RecyclerView的最大高度?,android,android-recyclerview,Android,Android Recyclerview,如何设置RecyclerView的最大高度? 我有一个RecyclerView,如下所示: <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_goods">
</android.support.v7.widget.RecyclerView>
当recyclerview的项目太多时,recyclerview将过高
我可以设置回收视图的最大高度吗
当recyclerview增长到最大高度时,recyclerview将不会增长,并且可以滚动recyclerview的项目。最简单的解决方案:您可以将
recyclerview
与android:layout\u height“match\u parent”
放在父布局中(相对布局、线性布局,…)具有特定高度。创建RecyclerView和override-onMeasure方法的子类
public class MyRecyclerView extends RecyclerView {
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
height = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(the_height_you_want), MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, height);
}
}
您可以使用
ConstraintLayout
来包装RecyclerView
和其他人,设置他们的约束并设置minHeight
以查看下面的RecyclerView尝试以下操作:
xml代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title"
android:textSize="21sp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp"> //increases your recycleview height
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
及
250dp
浮动高度=getResources().getDimension(R.dimen.view\U高度)//获得高度
ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
新参数。高度=高度;
回收器视图。setLayoutParams(新参数);
但当项目为0时,布局中有一个空白区域。我不想这样。如果项目大小=0,您可以为其设置可见性(GONE)。我知道最大高度为150dp,如果大小=1或2或3,则空白区域仍然存在。
ViewGroup.LayoutParams params=recycler_view.getLayoutParams();
params.height= RecyclerView.LayoutParams.WRAP_CONTENT;
recycler_view.setLayoutParams(params);
<dimen name="view_height">250dp</dimen>
float height= getResources().getDimension(R.dimen.view_height); //get height
ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
params_new.height=height;
recycler_view.setLayoutParams(params_new);