获取高度在运行时RecyclerView子视图 [英] Get height for RecyclerView child views at runtime

查看:1906
本文介绍了获取高度在运行时RecyclerView子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个可扩展的TextView的扩展,当用户presses按钮/崩溃。 TextView的和的ImageButton处于CardView,其被添加到RecyclerView

展开/折叠的工作很好,但现在我想只有当TextView的高度超过一定的最大高度添加的ImageButton。但是,当我评价LayoutManager的addView方法的高度,则返回0,可能是因为TextView中没有任何文字呢。

有没有办法,我可以覆盖CardView得到它的新内容之后被激发,这样我可以评估的TextView的高度,任何回调方法?

编辑:我已经试过onLayoutChildren作为yigit建议

  @覆盖
公共无效onLayoutChildren(RecyclerView.Recycler回收,RecyclerView.State州){
    super.onLayoutChildren(回收,状态);
    Log.d(LayoutManager的,状态计数:+ state.getItemCount());    的for(int i = 1; I< state.getItemCount();我++){
        视图V = recycler.getViewForPosition(I)
        TextView的CT =(TextView中)v.findViewById(R.id.comment_text);
        Log.d(LayoutManager的,评论高度:+ ct.getMeasuredHeight());
    }
}

输出结果:

  D / LayoutManager的:状态计数:4
D / LayoutManager的:评论高度:0
D / LayoutManager的:评论高度:0
D / LayoutManager的:评论高度:0


解决方案

LinearLayout.LayoutParams - 这可能会根据你的主要布局是RelativeLayout.LayoutParams
mParentContainer - 这是RecyclerView - 传递给你的适配器
Utils.dpToPx(8 + 15); - 这里除了利润的表兄弟姐妹他们并不指望措施
totalHeight + = 1; - 是导致适配器下一个视图占位符绑定;)

在View持有人:

  INT totalHeight = 0;
        的for(int i = 0; I< getItemCount();我++){            查看chView = viewHolder.itemView;
            chView.measure(
                   View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
            totalHeight + = chView.getMeasuredHeight();
            totalHeight + = Utils.dpToPx(8 + 15);
        }        totalHeight + = 1;
        LinearLayout.LayoutParams listParams =(LinearLayout.LayoutParams)mParentContainer.getLayoutParams();
        listParams.height = totalHeight;
        mParentContainer.setLayoutParams(listParams);

I'm trying to make an expandable TextView that expands/collapses when a user presses a button. The TextView and ImageButton are in a CardView, which is added to a RecyclerView.

The expand/collapse work nicely, but now I want to add the ImageButton only when the TextView height exceeds a certain maximum height. But when I evaluate the height in the LayoutManager's addView method, it returns 0, probably because the TextView does not have any text yet.

Is there any callback method that I can override that is fired after the CardView gets it's new content, so that I can evaluate the height of the TextView?

Edit: I've tried onLayoutChildren as yigit suggested

@Override
public void onLayoutChildren (RecyclerView.Recycler recycler, RecyclerView.State state) {
    super.onLayoutChildren(recycler, state);
    Log.d("LayoutManager","State Count: " + state.getItemCount());

    for (int i = 1; i < state.getItemCount(); i++) {
        View v = recycler.getViewForPosition(i);
        TextView ct = (TextView) v.findViewById(R.id.comment_text);
        Log.d("LayoutManager", "Comment height: " + ct.getMeasuredHeight());
    }
}

The resulting output:

D/LayoutManager﹕ State Count: 4
D/LayoutManager﹕ Comment height: 0
D/LayoutManager﹕ Comment height: 0
D/LayoutManager﹕ Comment height: 0

解决方案

LinearLayout.LayoutParams - this could be RelativeLayout.LayoutParams according to yours main layout mParentContainer - this is RecyclerView - passed to yours adapter Utils.dpToPx(8+15); - here addition of margins coz they are not counted on measure totalHeight += 1; - is to cause adapter to bind next view holder ;)

in View Holder:

int totalHeight = 0;
        for(int i=0; i<getItemCount(); i++){

            View chView = viewHolder.itemView;
            chView.measure(
                   View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalHeight += chView.getMeasuredHeight();
            totalHeight += Utils.dpToPx(8+15);
        }

        totalHeight += 1;
        LinearLayout.LayoutParams listParams = (LinearLayout.LayoutParams) mParentContainer.getLayoutParams();
        listParams.height = totalHeight;
        mParentContainer.setLayoutParams(listParams);

这篇关于获取高度在运行时RecyclerView子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆