问题 RecyclerView重新计算WRAP_CONTENT


使用版本23.2,我们现在可以使用 WRAP_CONTENT 对于 recyclerView 身高,这很棒。我这样做,但是我想在将项目添加(或删除)到适配器后重新计算高度(从而增加或减少高度)。

我特别的 RecyclerView 从1项开始,然后在用户进行选择时添加项。所以我需要 RecyclerView 布局增加高度,达到一定程度。理想情况下,当列表增加或减少时,平滑动画会发生这种情况。

我们怎样才能做到 WRAP_CONTENT 它布局后?

尝试:

recyclerview.requestLayout();

recyclerview.invalidate();

3424
2018-03-15 19:56


起源

你能解释一下你想要实现的更多细节吗?添加布局xml和一些代码也会很棒...... - crazyPixel
确保你不打电话 setHasFixedSize 在您的回收站视图上。 - Wukash
我有这个问题。您将需要调用measure,但如果您使用多个子布局,则在Android M中存在错误。为了解决这个问题,添加一个GlobalLayoutListener并在onGlobalLayout中设置高度 - zafrani
我觉得有点不清楚你想要什么。
请查看以下链接 stackoverflow.com/a/33542789/3257178 - Arun Antoney


答案:


我希望它可以合作 View.invalidate()

如果这不起作用,请尝试呼叫 requestLayout 要么 invalidate 在父视图上。


5
2018-03-25 20:25



在父视图上调用invalidate就可以了。当然,它不会增长或缩小。动画是一个更难的问题,但这是我问题的最接近的解决方案。 - Patrick


RecyclerView如何根据新的LayoutManger调整自身大小

RecyclerView小部件为创建列表和网格以及支持动画提供了高级灵活的基础。此版本为LayoutManager API带来了令人兴奋的新功能:自动测量!这允许RecyclerView根据其内容的大小调整自身大小。这意味着现在可以使用以前不可用的方案,例如使用WRAP_CONTENT作为RecyclerView的维度。您会发现所有内置的LayoutManagers现在都支持自动测量。

由于此更改,请务必仔细检查项目视图的布局参数:现在将完全遵循先前忽略的布局参数(例如滚动方向中的MATCH_PARENT)。

如果你有一个不扩展内置LayoutManagers之一的自定义LayoutManager,那么这是一个选择加入的API - 你需要调用setAutoMeasureEnabled(true)以及进行一些细微的更改,如Javadoc中所详述的那样。方法。

请注意,虽然RecyclerView为其子项设置动画,但它不会为其自己的边界更改设置动画。如果要在RecyclerView边界更改时为其设置动画,可以使用Transition API。

请阅读 这个


3
2018-03-30 15:23





选项一

你看到了这个吗? 回答

它没有使用recyclerView WRAP_CONTENT,但它可能会奏效。

您还可以创建自己的自定义recyclerView(扩展RecyclerView)并覆盖 onMeasure() 相反,在链接中使用layoutManager。

备选方案二

尝试在绘制视图之前设置布局参数。我还没有检查它是否在recyclerView布局更改时被调用,但如果确实如此,那么它将起作用。像这样的东西(在你的活动/片段中 onCreate()/onCreateView() 方法:

recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() { 
                recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);

                YourParentLayoutType.LayoutParams params = (YourParentLayoutType.LayoutParams) recyclerView.getLayoutParams();
                params.height = YourParentLayoutType.LayoutParams.WRAP_CONTENT;
                recyclerView.setLayoutParams(params);

                return true;
            }
        });

使用recyclerView的父布局类型而不是 YourParentLayoutType 在代码中。 我不确定这会在布局刷新时起作用,但也许值得一试。


2
2018-03-25 21:10



对我来说有用的是每次添加项目后用wrap_content重新设置布局参数。 - password


使用这个类:

请使用23.2.1作为23.2方式错误。

还有你试过打电话 notifyDataSetChanged 在...上 recyclerview 适配器,据我所知它应该扩展没有问题,如果你已将wrap_content作为recyclerview的高度

否则你可以使用这个类:

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

public class MyLinearLayoutManager extends LinearLayoutManager {

    public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout)    {
        super(context, orientation, reverseLayout);

    }



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



        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            if (getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);

                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }


        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        int widthDesired = Math.min(widthSize,width);
        setMeasuredDimension(widthDesired, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        View view = recycler.getViewForPosition(position);
        // For adding Item Decor Insets to view
        super.measureChildWithMargins(view, 0, 0);
        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                widthSpec,
                getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),
                p.width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(
                heightSpec,
                getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view) ,
                p.height);
        view.measure(childWidthSpec, childHeightSpec);

        // Get decorated measurements
        measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
        measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
        recycler.recycleView(view);
        }
    }

0
2018-03-30 10:15



您应该根据状态的itemCount检查循环计数器 i < state.getItemCount() 在这一个,否则它给IndexOutOfBounds - AA_PV