RecyclerView自适应item高度

本文介绍如何通过自定义LinearLayoutManager解决RecyclerView高度设置问题。关键在于覆盖onMeasure方法,并正确设置测量高度。需要注意的是,item布局需设定固定高度,同时禁用自动测量并设置RecyclerView的尺寸固定属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几个问题,简单搞定高度。

首先,需要自定义一个LinearLayoutManager,这里RecyclerView在onMeasure回调中会调用LinearLayoutManager的onMeasure方法,所以需要在LinearLayoutManager的onMeasure中做一些高度设置的处理,大致内容:

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec,int heightSpec) {
    View view = recycler.getViewForPosition(0);
    measureChild(view, widthSpec, heightSpec);
    int measuredWidth = View.MeasureSpec.getSize(widthSpec);
    int measuredHeight = view.getMeasuredHeight();
    setMeasuredDimension(measuredWidth, measuredHeight);
}

两个注意点:这里获取了view的高度,也就是item布局的高度,所以item的布局需要设定固定的高度,否则获取为0。其次,

mLayoutManager.setAutoMeasureEnabled(false)
mList.setHasFixedSize(false)

这两个设置不能少,否则报错,这里和RecyclerView.onMeasure中的调用顺序有关,源码:

protected void onMeasure(int widthSpec, int heightSpec) {
    if (mLayout == null) {
        defaultOnMeasure(widthSpec, heightSpec);
        return;
    }
    if (mLayout.mAutoMeasure) {     //这里为true会进入该分支
        final int widthMode = MeasureSpec.getMode(widthSpec);
        final int heightMode = MeasureSpec.getMode(heightSpec);
        final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY
                && heightMode == MeasureSpec.EXACTLY;
        mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);   //在调用mLayout的onMeasure方法时(被自定义复写的方法),mState.mItemCount为0,造成越界异常
        if (skipMeasure || mAdapter == null) {
            return;
        }
        if (mState.mLayoutStep == State.STEP_START) {
            dispatchLayoutStep1();
        }
        // set dimensions in 2nd step. Pre-layout should happen with old dimensions for
        // consistency
        mLayout.setMeasureSpecs(widthSpec, heightSpec);
        mState.mIsMeasuring = true;
        dispatchLayoutStep2();

        // now we can get the width and height from the children.
        mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);

        // if RecyclerView has non-exact width and height and if there is at least one child
        // which also has non-exact width & height, we have to re-measure.
        if (mLayout.shouldMeasureTwice()) {
            mLayout.setMeasureSpecs(
                    MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
            mState.mIsMeasuring = true;
            dispatchLayoutStep2();
            // now we can get the width and height from the children.
            mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);
        }
    } else {
        if (mHasFixedSize) {   //这里不设置为false会造成与上面相同的问题
            mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
            return;
        }
        // custom onMeasure
        if (mAdapterUpdateDuringMeasure) {
            eatRequestLayout();
            processAdapterUpdatesAndSetAnimationFlags();

            if (mState.mRunPredictiveAnimations) {
                mState.mInPreLayout = true;
            } else {
                // consume remaining updates to provide a consistent state with the layout pass.
                mAdapterHelper.consumeUpdatesInOnePass();
                mState.mInPreLayout = false;
            }
            mAdapterUpdateDuringMeasure = false;
            resumeRequestLayout(false);
        }

        if (mAdapter != null) {
            mState.mItemCount = mAdapter.getItemCount();
        } else {
            mState.mItemCount = 0;
        }
        eatRequestLayout();
        mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);   //在这里调用时mState.mItemCount才会有值,这与mLayout中获取当前item布局的方式有关:View view = recycler.getViewForPosition(0);
        resumeRequestLayout(false);
        mState.mInPreLayout = false; // clear
    }
}

见注释。

转载于:https://my.oschina.net/buobao/blog/651279

要让 RecyclerView高度随着子项自适应,可以采用以下两种方法: 方法一:使用 WrapContentLinearLayoutManager WrapContentLinearLayoutManager 是 RecyclerView 的一个布局管理器,可以让 RecyclerView高度随着子项自适应。 首先,需要在项目的 build.gradle 中添加以下依赖: ``` implementation 'com.github.azhao1981:WrapContentLinearLayoutManager:v1.0.1' ``` 接着,在代码中设置 RecyclerView 的布局管理器为 WrapContentLinearLayoutManager,如下: ```java WrapContentLinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); ``` 方法二:手动计算高度 另一种方法是手动计算 RecyclerView高度,具体步骤如下: 1. 在 RecyclerView 的 Adapter 中,添加一个方法用于计算 RecyclerView高度: ```java public int getItemHeight() { int height = 0; for (int i = 0; i < getItemCount(); i++) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, null); view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); height += view.getMeasuredHeight(); } return height; } ``` 2. 在 Activity 或 Fragment 中获取 RecyclerView高度,并设置给 RecyclerView: ```java int height = mAdapter.getItemHeight(); ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams(); layoutParams.height = height; recyclerView.setLayoutParams(layoutParams); ``` 其中,getItemHeight() 方法中的 R.layout.item_layout 是 RecyclerView 的子项布局文件。如果 RecyclerView 的子项布局文件中包含了多个子 View,请将它们的高度相加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值