Android 如何设置RecyclerView最大高度

Android 如何设置RecyclerView最大高度,android,android-recyclerview,Android,Android Recyclerview,我想设置RecylerView的最大高度。我可以使用下面的代码设置最大高度。下面的代码使高度为当前屏幕的60% DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int a = (displaymetrics.heightPixels * 60) / 100; recycl

我想设置RecylerView的最大高度。我可以使用下面的代码设置最大高度。下面的代码使高度为当前屏幕的60%

     DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int a = (displaymetrics.heightPixels * 60) / 100;
    recyclerview1.getLayoutParams().height = a;
但现在的问题是,若它并没有项目,那个么它的高度也是60%。 所以我想在没有项目时将其高度设置为0

我想取得一些成就

    if(recyclerview's height > maxHeight)
then set recyclerview's height to maxHeight
    else dont change the height of recyclerview.
我如何设置它?
请帮帮我,我被它困住了

如果还有其他的话,请把这句话写在里面 让我知道

ViewGroup.LayoutParams params=recyclerview.getLayoutParams();
params.height=100;
recyclerview.setLayoutParams(params);

您可以在RecyclerView中使用WRAP_内容。它将根据内容自动测量您的recyclerview

您还可以计算当前高度并设置最大高度。因此,Recyclerview将使用wrap_内容属性值,直到最大高度

public static void getTotalHeightofRecyclerView(RecyclerView recyclerView) {

        RecyclerView.Adapter mAdapter = recyclerView.getAdapter();

        int totalHeight = 0;

        for (int i = 0; i < mAdapter.getItemCount(); i++) {
            View mView = recyclerView.findViewHolderForAdapterPosition(i).itemView

            mView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

            totalHeight += mView.getMeasuredHeight();
        }

        if (totalHeight > 100) {
            ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
            params.height = 100;
            recyclerView.setLayoutParams(params);
        }
    }
publicstaticvoid getTotalHeightoRecyclerView(RecyclerView-RecyclerView){
RecyclerView.Adapter mAdapter=RecyclerView.getAdapter();
int totalHeight=0;
对于(int i=0;i100){
ViewGroup.LayoutParams params=recyclerView.getLayoutParams();
参数高度=100;
recyclerView.setLayoutParams(参数);
}
}

这是你需要的东西。将RecyclerView和override onMeasure子类化,如下所示:

@Override
protected void onMeasure(int widthSpec, int heightSpec) {
    heightSpec = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(240), MeasureSpec.AT_MOST);
    super.onMeasure(widthSpec, heightSpec);
}

请确保在
makeMeasureSpec()
中的第一个参数中指定了适当的像素数。我个人需要RecyclerView,最多240 dps。

我们可以稍微优化@Fattum的方法。 我们可以使用
app:maxHeight
设置
maxHeight
。你可以随意使用
dp
px

public class MaxHeightRecyclerView extends RecyclerView {
    private int mMaxHeight;

    public MaxHeightRecyclerView(Context context) {
        super(context);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
        mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightScrollView_maxHeight, mMaxHeight);
        arr.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mMaxHeight > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
值/attrs.xml

<declare-styleable name="MaxHeightScrollView">
    <attr name="maxHeight" format="dimension" />
</declare-styleable>

ConstraintLayout为其子项提供最大高度

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

如果您有一个设计用于容纳相同物理尺寸的物品的RecyclerView,并且您想要一个不需要扩展RecyclerView的简单方法来限制其高度,请尝试以下方法:

int maxRecyclerViewHeightPixels = (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    MAX_RECYCLERVIEW_HEIGHT_DP,
    getResources().getDisplayMetrics()
);

MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(getContext(), myElementList);
myRecyclerView.setAdapter(myRecyclerViewAdapter);

ViewGroup.LayoutParams params = myRecyclerView.getLayoutParams();

if (myElementList.size() <= MAX_NUMBER_OF_ELEMENTS_TO_DISPLAY_AT_ONE_TIME) {
    myRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.MATCH_PARENT,
         LinearLayout.LayoutParams.WRAP_CONTENT));
    //LinearLayout must be replaced by whatever layout type encloses your RecyclerView
}
else {
    params.height = maxRecyclerViewHeightPixels;
    myRecyclerView.setLayoutParams(params);
}
int-maxRecycleServiceWheeghtPixels=(int)TypedValue.applyDimension(
TypedValue.COMPLEX\u UNIT\u DIP,
最大回收视图高度DP,
getResources().getDisplayMetrics()
);
MyRecycleServiceAdapter MyRecycleServiceAdapter=新建MyRecycleServiceAdapter(getContext(),myElementList);
设置适配器(myRecycleServiceAdapter);
ViewGroup.LayoutParams params=myRecyclerView.getLayoutParams();

if(myElementList.size()最简单的方法是使用ConstraintLayout并在Recycleview上设置高度约束

<android.support.constraint.ConstraintLayout
    android:id="@+id/CL_OUTER_RV_Choose_Categories "
    android:layout_width="match_parent"
    android:layout_height="200dp">

   <android.support.v7.widget.RecyclerView
        android:id="@+id/RV_Choose_Categories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constrainedHeight="true" >
    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

请注意,在棒棒糖之前,recycleview不会滚动。作为解决方案,请在活动的oncreate中添加以下代码

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ConstraintLayout CL_OUTER_RV_Choose_Categories = findViewById(R.id.CL_OUTER_RV_Choose_Categories);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) CL_OUTER_RV_Choose_Categories.getLayoutParams();
        lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
    }
if(android.os.Build.VERSION.SDK\u INT
这将确保仅在受支持的设备上固定高度,并且在旧设备上显示完整的回收视图


请告诉我是否有更好的方法。

您只需在xml代码中将特定高度设置为任意值,并将可见性设置为gone。然后在Java中将可见性设置为Visible,当您要对其进行充气时。下面是一个示例

.xml

.爪哇


在尝试了太多复杂的建议后,我终于通过反复试验找到了答案

首先,在某种布局中包装RecyclerView。在我的例子中,我使用了约束布局,甚至在该布局中的其他元素位于RecyclerView的上方和下方。通过将布局的高度设置为0dp,将其设置为自动调整,然后将默认布局高度约束设置为包装,并定义布局最大高度约束

然后,在RecyclerView上,将“包裹内容”的高度和“布局约束高度”设置为true

下面是它的外观:

<android.support.constraint.ConstraintLayout
    android:layout_width="600px"
    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintHeight_max="450px">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintTop_ToTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>


希望这能有所帮助!

我会加上我的2美分。我需要一个具有最大高度和最小高度的回收器视图,并在2美分之间包装内容

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/group_card_recycler_view_holder"
            app:layout_constraintHeight_default="wrap"
            app:layout_constraintHeight_max="@dimen/group_recycler_view_height"
            app:layout_constraintHeight_min="@dimen/card_sentence_height"
            android:layout_marginTop="@dimen/activity_horizontal_margin_8dp"
            app:layout_constraintTop_toBottomOf="@id/group_name_container"
            app:layout_constraintBottom_toTopOf="@id/myButtonLayout">


            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constrainedHeight="true"
                android:id="@+id/group_card_recycler_view"/>


        </androidx.constraintlayout.widget.ConstraintLayout>

我想这至少对我有用

<androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/Id_const_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               >
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/Id_my_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_top_space"
            android:scrollbars="vertical"
            app:layout_constrainedHeight="true"
            app:layout_constraintBottom_toBottomOf="@+id/Id_const_layout"
            app:layout_constraintEnd_toEndOf="@+id/Id_const_layout"
            app:layout_constraintHeight_max="150dp"
            app:layout_constraintHeight_min="30dp"
            app:layout_constraintStart_toStartOf="@+id/Id_const_layout"
            app:layout_constraintTop_toTopOf="@+id/Id_const_layout"
            >
        </androidx.recyclerview.widget.RecyclerView>
            </androidx.constraintlayout.widget.ConstraintLayout>


您可以根据需要更改
constraintheighth_max
constraintheighth_min

我也遇到了同样的问题,想分享一个新的最新答案。这个答案至少适用于Android 9和Android Studio v.4.0

正如这里的一些答案所建议的那样,将RecyclerView包装在ConstraintLayout中。但是,您似乎不能用XML设置该布局的最大高度。相反,您必须在代码中进行设置,如下所示:为ConstraintLayout指定一个ID,然后在活动或片段的onCreate或onViewCreated方法中分别设置为question-例如,要将最大高度设置为200 dp:

findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // activity
view.findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // fragment
findViewById(R.id.[您的id]).maxHeight=200//activity
view.findviewbyd(R.id.[您的id]).maxHeight=200//fragment
令人烦恼的是,ConstraintLayout确实公开了一个XML属性
android:minHeight
,但没有
android:maxHeight


更令人烦恼的是,RecyclerView本身显然带有一个名为“
android:maxHeight
”的XML属性,但该属性不起作用。

如果你说的是哪个?>当其中没有任何项目时,将其高度设置为0。你如何确定这一点?@Maulik009,在那个地方。是的@Piyus,bhai:)我一直在版面中包装内容。但是我想设置它的最大高度,这样它就会在cu中滚动
<androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/Id_const_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               >
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/Id_my_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_top_space"
            android:scrollbars="vertical"
            app:layout_constrainedHeight="true"
            app:layout_constraintBottom_toBottomOf="@+id/Id_const_layout"
            app:layout_constraintEnd_toEndOf="@+id/Id_const_layout"
            app:layout_constraintHeight_max="150dp"
            app:layout_constraintHeight_min="30dp"
            app:layout_constraintStart_toStartOf="@+id/Id_const_layout"
            app:layout_constraintTop_toTopOf="@+id/Id_const_layout"
            >
        </androidx.recyclerview.widget.RecyclerView>
            </androidx.constraintlayout.widget.ConstraintLayout>
findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // activity
view.findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // fragment