如何为RecyclerView设置最大高度?

时间:2018-04-03 03:22:47

标签: android android-recyclerview

如何设置RecyclerView的最大高度? 我有一个RecyclerView如下:

                <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将不会长大,并且可以滚动recyclelerview项目。

4 个答案:

答案 0 :(得分:0)

最简单的解决方案:您可以将RecyclerViewandroid:layout_height"match_parent"放在具有特定高度的父布局(RelativeLayout,LinearLayout,...)中。

答案 1 :(得分:0)

创建RecyclerView的子类并覆盖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);
    }
}

答案 2 :(得分:0)

您可以使用RecyclerView打包minHeight和其他人,设置约束并为RecyclerView以下的视图设置/\d+/g

答案 3 :(得分:0)

试试这个:

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>

java代码

如果您的项目在 换行内容

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);