getView中的ArrayAdapter中的setLayoutParams必须仅将params设置为一个视图

时间:2015-01-28 12:52:52

标签: android listview android-listview 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。

1 个答案:

答案 0 :(得分:1)

那是因为你必须设置其他的。由于视图已被回收,因此如果您未处理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);
    }