简体   繁体   中英

Android:Listview item background issue on list scrolling

I facing below issue with item back ground on scrolling.

In my application I have a listview which require multi-selection. Also this is a custom list where selection needs to be represented by change in list item color instead of check-box based approach. For this: In the OnClick I'm checking if the position is selected or not and then set the background for the item. However this has issue when I scroll the list. Taking an example: suppose the list has 50 items. And 10 are visible at a time. I select say 5th item [thus changing the background]. And then I scroll the list. After scroll the visible part of the list corresponding to earlier 5th item ,say 15th item in item of the list but 5th index in visible portion,still has background corresponding to selected state. Whereas it should not have been set since I have not selected 15th item yet.

I tried: a-In the getView method of adapter, if the item is not one of selected items I'm setting one background else different.Tried - setBackgroundColor as well setBackgrounddrawable. b- In the xml have set the cacheColorHint to transparent c- Have selector attached to items and the items responding to state [pressed,selected] in onlcick.

However still I'm not able to get rid of unwanted background color for item on scrolling.

Any help. I tried various suggestion mentioned in various post in SO but not succcessful yet.

I tried

thanks pradeep

this is a normal behavior of ListView adapter in android, its getView() called on every scroll and for every new list item it call getView, if listview item currently not visible on UI then its convertView is equals to null: At a time listview take load of only visible list items, if it showing at a time 10 element out of 50, then listView.getChildCount() will return only 10 not 50. In your case when you select 5, it reflected selection for 5+10(visible items count) = 15, 25, 35, 45 too. To solve this problem you should have a flag associate with your each listItem data, for example if you have string array itemData[50] as array, then take an array of boolean isSelected[50] with initial value false for each.

Take a look for getView(), in adapter class:

      public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
                    string text = itemData[position]
        if (convertView == null) {
            rowLayout = (RelativeLayout) LayoutInflater.from(context)
                    .inflate(R.layout.list_view_item, parent, false);
            holder = new ViewHolder();

            holder.txtString= (TextView) rowLayout
                    .findViewById(R.id.txtTitle);
            rowLayout.setTag(holder);
        } else {
            rowLayout = (RelativeLayout) convertView;
            holder = (ViewHolder) rowLayout.getTag();
        }


        if(isSelected[position] == true){
                   holder.txtString.setText("Selected")
                   rowLayout.setBackGround(selected)
        }else{
                             holder.txtString.setText("Not Selected")
             rowLayout.setBackGround(notSelected)
        }




    public class ViewHolder {
        public TextView txtString;

    }

and in your Activity class on listView.setOnItemClickListener():

 listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
                      isSelected[position] = true  // on selection

                      RelativeLayout rowLayout = (RelativeLayout) view;
          rowLayout.setBackGround(Selected);


                      // also set here background selected for view by getting layout refference


                    }
            });

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