0

I want the last cell to have for example 300dp height. Here is my getView method

@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;
}

I want the last cell to have 300dp height. It works good, last cell have 300dp height, when I'a scroll to bottom. But when I scroll up, appears, that a lot of other elements of list become 300dp in height.

LamaUltramarine
  • 115
  • 1
  • 1
  • 7
  • That's because the position gives the current position of the element. It's not a static number. The ordering changes continually. Position 1 can be in the dead center of the list while 2 is drawn above 1 and 3 is below 2. You have NO guarantees about the positions and they will change every time you scroll. – G_V Jan 28 '15 at 12:57

1 Answers1

1

That's because you have to set an else too. Since views are recycled you will have to restore their height if you're not dealing with position == getCount() - 1

It should look like:

 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);
    }
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82