getView 中 ArrayAdapter 中的 setLayoutParams 必须仅将参数设置为一个视图
setLayoutParams in ArrayAdapter in getView must set params only to one view
我希望最后一个单元格的高度为 300dp。这是我的 getView 方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item_card_list, parent, false);
}
if (position == getCount() - 1) {
ViewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = 300;
rowView.setLayoutParams(layoutParams);
}
return rowView;
}
我希望最后一个单元格的高度为 300dp。效果很好,当我滚动到底部时,最后一个单元格的高度为 300dp。但是当我向上滚动时,列表中的许多其他元素的高度都变成了 300dp。
那是因为你还要设置一个else。由于视图被回收,如果你不处理 position == getCount() - 1
,你将不得不恢复它们的高度
它应该看起来像:
if (position == getCount() - 1) {
ViewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = 300;
rowView.setLayoutParams(layoutParams);
}else{
viewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = ?????; //your other height here.. (WRAP_CONTENT perhaps?)
rowView.setLayoutParams(layoutParams);
}
我希望最后一个单元格的高度为 300dp。这是我的 getView 方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item_card_list, parent, false);
}
if (position == getCount() - 1) {
ViewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = 300;
rowView.setLayoutParams(layoutParams);
}
return rowView;
}
我希望最后一个单元格的高度为 300dp。效果很好,当我滚动到底部时,最后一个单元格的高度为 300dp。但是当我向上滚动时,列表中的许多其他元素的高度都变成了 300dp。
那是因为你还要设置一个else。由于视图被回收,如果你不处理 position == getCount() - 1
它应该看起来像:
if (position == getCount() - 1) {
ViewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = 300;
rowView.setLayoutParams(layoutParams);
}else{
viewGroup.LayoutParams layoutParams = rowView.getLayoutParams();
layoutParams.height = ?????; //your other height here.. (WRAP_CONTENT perhaps?)
rowView.setLayoutParams(layoutParams);
}