0
votes

I have created a custom selector for my listview which changes the background color of the list item using a drawable. I previously having issues with the background color extending to random views in the list when scrolling and I fixed this by making the view in the adapter always null however when I scroll and as soon as the item is no longer visible the background color that was set is no longer there, it's like it's not remembering it. This is probably due to the view being null always?

I am basically wanting the item selected to be changed and not affect other items in the list and when scrolling, the item that was changed should be remembered.

public View getView(int pos, View view, ViewGroup viewgroup)
{
    View row = null;
    view = null;
    row = view;
    if(view == null)
        row =   layoutInflater.inflate(R.layout.layout_list, viewgroup, false);
    row.setPadding(0, 10, 0, 10);
    listText      = (MusicTextView)row.findViewById(R.id.listLabels);
    listText.setTypeface(Typeface.createFromAsset(
                                                    context.getAssets(),
                                                    "fonts/Roboto-Light.ttf"
    return row;
}
2

2 Answers

0
votes

Well every item that becomes "invisible" due to the scroll down/up will be sent to the adapter in order to avoid creating it in memory again (second parameter), so basically you have to set up and return the view again.

Same thing will happen not matter you scrolled up or down they will be sent to the adapter over and over again.

On the other hand (as a suggestion), you can use a wrapper class for (MusicTextView) and use setTag() function to the item view so you can avoid to find this again and again.

So View -> Tag -> Wrapper -> Pointer to MusicTextView

0
votes

You may try to override this method to prevent view recycling

@Override
public int getViewTypeCount() {
    return getCount();
}

and remove this view = null;