goo*_*y_j 2 android runtime android-recyclerview
我正在尝试制作一个可扩展的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)
LinearLayout.LayoutParams-根据您的主布局,它可以是RelativeLayout.LayoutParams。mParentContainer-这是RecyclerView-传递给您的适配器Utils.dpToPx(8 + 15); -此处添加的边距coz不计入度量totalHeight + = 1;-使适配器绑定下一个视图持有者;)
在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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9703 次 |
| 最近记录: |