更改适配器中项目的背景

时间:2016-03-17 18:14:11

标签: android list listview adapter

我想更改适配器中所选列表项的背景。

列表适配器

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Card card = getItem(position);

        final ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = from(context).inflate(R.layout.card_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }


       viewHolder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));
            }
       });
        return convertView;
    }

    private class ViewHolder {
        Button select;
        RelativeLayout cardLayout;

        public ViewHolder(View view) {
            select = (Button) view.findViewById(R.id.carditem_btn_upvote);
            cardLayout = (RelativeLayout) view.findViewById(R.id.cardlist_item);
        }
    }

问题是当我选择前。列表的第一项,也选择了第7项。我注意到有一种赞助人。当我选择第二个项目时,也会选择第8个项目,依此类推。

enter image description here

1 个答案:

答案 0 :(得分:3)

让'假设您在列表中有10个项目,并且最初您将所有项目颜色存储为白色。

 HashMap<Integer,Integer> mhashColorselected=new HashMap();
HashMap<Integer,Integer> mHashBtnVisibility=new HashMap();

  for(i=0;i<10;i++){
//Put Default Color of your All list item will be here 
mhashColorselected.put(i,R.color.white)

mHashBtnVisibility.put(i,View.Invisible);

} 

现在在你的适配器getView();

   @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                final Card card = getItem(position);

                final ViewHolder viewHolder;
                if (convertView != null) {
                    viewHolder = (ViewHolder) convertView.getTag();
                } else {
                    convertView = from(context).inflate(R.layout.card_item, parent, false);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                }


               viewHolder.select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));

    //Here we are storing Selected item By User 
         mhashColorselected.put(position,R.color.md_grey_600);
          mHashBtnVisibility.put(i,View.Visible);

        notifitydataSetChange();
                    }
               });


        //Here it's will take fault value or else it will set color as selected item 
        viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, mhashColorselected.getValue(position)));


 if(View.VISIBLE==mHashBtnVisibility.get(position)){

 viewHolder.select.setVisibility(View.VISIBLE)
    }else{
 viewHolder.select.setVisibility(View.INVISIBLE)

    }



                return convertView;
            }