Reputation: 2204
I got this problem several times and even though I can't solve it
here's code from Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.place_item_layout, parent, false);
//HERE's my check
(convertView.findViewById(R.id.divdePlaces)).setVisibility(View.GONE);
if (place.isTOP()==false && position!=0) if (places.get(position-1).isTOP()==true) {
//turn back on
(convertView.findViewById(R.id.divdePlaces)).setVisibility(View.VISIBLE);
}
.. etc..
}
return convertView;
}
So, this just turn on the divider line if conditions are met. I thought here the position should be the real number but it repeats again when ListView goes 1 screen up, even though the conditions for it are not met for sure.
How do I fix that?
Upvotes: 0
Views: 43
Reputation: 55340
This problem is derived from view reuse in ListView
. As you scroll down, the item views that exit from the top are reused (supplied as convertView
) for the new items. Therefore, you need to make sure that you "undo customizations" if they no longer apply.
For example:
if (convertView == null)
{
// create new view.
convertView = inflater.inflate(...);
}
// OUTSIDE the previous if, and with both branches implemented.
if (check)
(convertView.findViewById(R.id.divdePlaces)).setVisibility(View.VISIBLE);
else
(convertView.findViewById(R.id.divdePlaces)).setVisibility(View.GONE);
...
Upvotes: 1
Reputation: 104514
As you scroll one page up, your views start to get recycled. Hence, convertView is already inflated and non-null. Toggle the visibility of the divider outside of the if (convertView==null) block.
Something like this:
public View getView(final int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.place_item_layout, parent, false);
}
int visibility = (place.isTOP()==false && position!=0) ? View.GONE : View.VISIBLE;
convertView.findViewById(R.id.divdePlaces).setVisibility(visibility);
return convertView;
}
Upvotes: 1