简体   繁体   中英

ListView setting background color to an item ERROR

Well, I have a horizontal LinearLayout, with items of a custom class.

What I want: Change background color of the item i've clicked (like if it's selected). If user clicks other item, the first one changes again to original color and the new one changes to "selected color"

What is my problem: This works fine when all items of the listView fit in the screen ( in this case 3 items). If there are more items, let's say 7, if user clicks item number 0, it changes color, but if user scrolls to the last item and clicks item number 6, it changes color, but item 0 doesn't change. If both items are visible, it works fine.

this is the code of my onItemClickListener.

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> parent, View view, int position, long id) {

                for(int i=0;i<listView.getCount();i++){
                    try{

                        getViewByPosition(i, listView).setBackgroundResource(R.drawable.skin);
                        listView.getChildAt(i).setBackgroundResource(R.drawable.skin);
                    }
                    catch(Exception e){
                        System.out.println(e.toString());
                    }                       
                }

                view.setBackgroundResource(R.drawable.skin_selected);   
            }                                       
});

why not use the adapter for the modification?I presume you are using a custom adapter?

in You Adapter

public int selectedItem;
.
//Rest of the stuff
.
.
 @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    if (null == convertView) {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, parent, false);
    }

    if(selectedItem==position){
        convertView.setBackgroundResource(R.drawable.skin_selected);
    }else{
        convertView.setBackgroundResource(R.drawable.skin);
    }

    return convertView;
}

and your onclick listener becomes :

 listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> parent, View view, int position, long id) {
            ((NameOfYouAdpater)listView.getAdapter()).selectedItem=position;
            ((NameOfYouAdpater)listView.getAdapter()).notifyDataSetChanged();
        }                                       
});

And I noticed you are using a custom list view in itself.Maybe look into the implementation of the library to check for interference?

EDIT:the cause of your issue is most probably dude to view recycling.The background colour does not get updated because everytime the list item is outside your ViewPort,the view is recycled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM