要解决Android RecyclerView或GridView的高度被拉伸的问题,可以采取以下步骤:
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
int totalHeight = 0;
for (int i = 0; i < adapter.getItemCount(); i++) {
View itemView = layoutManager.findViewByPosition(i);
itemView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
totalHeight += itemView.getMeasuredHeight();
}
recyclerView.getLayoutParams().height = totalHeight;
通过采取上述步骤,您应该能够解决Android RecyclerView或GridView的高度被拉伸的问题。