【发布时间】:2020-12-31 05:10:40
【问题描述】:
我正在尝试制作一个可展开的 TextView,当用户按下按钮时它会展开/折叠。 TextView 和 ImageButton 在 CardView 中,添加到 RecyclerView 中。
展开/折叠效果很好,但现在我只想在 TextView 高度超过某个最大高度时添加 ImageButton。但是当我在 LayoutManager 的 addView 方法中计算高度时,它返回 0,可能是因为 TextView 还没有任何文本。
是否有任何我可以覆盖的回调方法在 CardView 获取它的新内容后触发,以便我可以评估 TextView 的高度?
编辑:我已经按照 yigit 的建议尝试了 onLayoutChildren
@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());
}
}
结果输出:
D/LayoutManager﹕ State Count: 4
D/LayoutManager﹕ Comment height: 0
D/LayoutManager﹕ Comment height: 0
D/LayoutManager﹕ Comment height: 0
【问题讨论】:
标签: android runtime android-recyclerview