我正在尝试制作一个可扩展的TextView,当用户按下按钮时它会扩展/折叠。TextView和ImageButton位于CardView中,该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());
}
}
Run Code Online (Sandbox Code Playgroud)
结果输出:
D/LayoutManager? State Count: 4
D/LayoutManager? Comment height: 0
D/LayoutManager? Comment height: 0
D/LayoutManager? Comment height: 0
Run Code Online (Sandbox Code Playgroud)