自定义listview setbackground问题

时间:2018-08-14 09:06:01

标签: android

我具有自定义列表视图,因此,当用户单击某个项目时,我想更改所选项目的背景,但是现在,当我单击位置2上的示例元素时,背景已在另一个位置更改。 / p>

if (view == null)
{
    holder = new ViewHolder();
    view = mInflater.inflate(R.layout.custom_clients_row, null);
    holder.nameClient = (TextView) view.findViewById(R.id.clientName);
    holder.address = (TextView) view.findViewById(R.id.addressName);

    view.setTag(holder);
} else {
    holder = (ViewHolder) view.getTag();
}       
    holder.nameClient.setText(mDisplayedValues.get(position).getNameClient());
    holder.address.setText(mDisplayedValues.get(position).getAddressClient());

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setBackgroundColor(Color.parseColor("#F0F0F0"));

        }
    });
return view;

2 个答案:

答案 0 :(得分:1)

尝试一下

在列表中获取一个名为color的int变量。首先为列表中的所有元素设置默认颜色。

if (view == null)
{
holder = new ViewHolder();
view = mInflater.inflate(R.layout.custom_clients_row, null);
holder.nameClient = (TextView) view.findViewById(R.id.clientName);
holder.address = (TextView) view.findViewById(R.id.addressName);

view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}       

view.setBackgroundColor(mDisplayedValues.get(position).getColor());

holder.nameClient.setText(mDisplayedValues.get(position).getNameClient());
holder.address.setText(mDisplayedValues.get(position).getAddressClient());

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mDisplayedValues.get(position).setColor(Color.parseColor("#F0F0F0"));
        notifyAdapterDataSetChange();
    }
});
return view;

在您的适配器中实现此方法。

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

@Override
public int getItemViewType(int position) {
    return position;
}

答案 1 :(得分:1)

步骤1:在适配器类中添加以下行

private int mSelectedPosition = -1;

步骤2:在步骤3上方添加此行

if(mSelectedPosition == position){
   view.setBackgroundColor(Color.parseColor("#F0F0F0"));
} else {
   view.setBackgroundColor(Color.parseColor("#00000000"));
}

步骤3:如下更改您的点击方法

   view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             notifyItemChanged(mSelectedPosition);
             mSelectedPosition = position;
             notifyItemChanged(mSelectedPosition);

        }
    });

最终步骤:这是完整的修改后的代码。

if (view == null)
{
    holder = new ViewHolder();
    view = mInflater.inflate(R.layout.custom_clients_row, null);
    holder.nameClient = (TextView) view.findViewById(R.id.clientName);
    holder.address = (TextView) view.findViewById(R.id.addressName);

    view.setTag(holder);
} else {
    holder = (ViewHolder) view.getTag();
}       
    holder.nameClient.setText(mDisplayedValues.get(position).getNameClient());
    holder.address.setText(mDisplayedValues.get(position).getAddressClient());

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             notifyItemChanged(mSelectedPosition);
             mSelectedPosition = position;
             notifyItemChanged(mSelectedPosition);

        }
    });

    if(mSelectedPosition == position){
      view.setBackgroundColor(Color.parseColor("#F0F0F0"));
    } else {
       view.setBackgroundColor(Color.parseColor("#00000000"));
    }
return view;