将特定项目的背景更改为颜色和其他项目为不同的颜色

时间:2016-10-21 06:41:18

标签: android

Display

我将列表传递给对话框。如果我选择一个项目,背景应该变为绿色。如果我选择另一个项目,项目背景应更改为绿色,之前选择的项目背景应分配为默认值。

例如:如果我选择"图3",背景应变为绿色。如果我选择"图5","图5"的背景应改为绿色和"图3"背景应更改为默认值。

请帮我解决这个问题。提前谢谢。

public ConfigureDialogAdapter(Context context, List<PhrToolBar> listData) {
  mInflater = LayoutInflater.from(context);
  mToolBarList = listData;
}

public void setDialogClickListener(DialogClickListener dialogClickListener) {
  mDialogClickListener = dialogClickListener;
}

public void setToolBarList(List<PhrToolBar> list) {
  mToolBarList = list;
  notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

  View rootView = mInflater.inflate(R.layout.configure_dialog_list_adapter, parent, false);
  return new ViewHolder(rootView);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

  PhrToolBar current = mToolBarList.get(position);
  viewHolder.textView.setText(current.getTitle());

  if (current.isVisible()){
     viewHolder.checkBox.setChecked(true);
  } else {
     viewHolder.checkBox.setChecked(false);
  }
}

public class ViewHolder extends RecyclerView.ViewHolder {

  CheckBox checkBox;
  TextView textView;

  public ViewHolder(View itemView) {
    super(itemView);

    checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
    textView = (TextView) itemView.findViewById(R.id.text_view);
    textView.setOnClickListener(onTextClick);
    checkBox.setOnCheckedChangeListener(onCheckBoxClick);
  }

  View.OnClickListener onTextClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      textView.setTextColor(Color.WHITE);
      textView.setBackgroundResource(R.color.colorPrimary);

      if (mDialogClickListener != null) {
         mDialogClickListener.onItemClick(getAdapterPosition());
      }
    }
  };

  CompoundButton.OnCheckedChangeListener onCheckBoxClick = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

      if (mDialogClickListener != null) {
        if (isChecked) {
           mDialogClickListener.onCheckBoxClick(true, getAdapterPosition());
        } else {
           mDialogClickListener.onCheckBoxClick(false, getAdapterPosition());
        }
      }
    }
  };
}

@Override
public int getItemCount() {
  return mToolBarList.size();
}

1 个答案:

答案 0 :(得分:1)

创建一个像这样的可绘制选择器(在res / drawable文件夹中):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true">
   <shape>
         <solid android:color="@color/green" />
   </shape>
</item>

<item android:state_selected="false">
    <shape>
       <solid android:color="@android:color/transparent" />
    </shape>
</item>
</selector>

然后,在原始布局中添加背景布局并将raw标记为可点击:

android:background="@drawable/selector"
android:clickable="true"

另外,将setSelected添加到OnClickListener

View.OnClickListener onTextClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      textView.setTextColor(Color.WHITE);
      textView.setSelected(true);

      if (mDialogClickListener != null) {
         mDialogClickListener.onItemClick(getAdapterPosition());
      }
    }
  };

每当您将商品标记为setSelected(true)时,它都会为绿色! 此外,您应该将选择状态保持在数组中并将代码更改为以下内容:

public class ViewHolder extends RecyclerView.ViewHolder {
 //Add this, this array will store selected items
 SparseBooleanArray selectedItems;

 [...]
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

  PhrToolBar current = mToolBarList.get(position);
  viewHolder.textView.setText(current.getTitle());

  //add this, If the item position is in the array, it will be marked as selected (so green), otherwise background will be default one (transparent)
  viewHolder.textView.setSelected(selectedItems.get(position, false));

  if (current.isVisible()){
     viewHolder.checkBox.setChecked(true);
  } else {
     viewHolder.checkBox.setChecked(false);
  }
}
[...]

View.OnClickListener onTextClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      //Add this to add the position to the array storing selected item !       
      if (selectedItems.get(getAdapterPosition(), false)) {
         selectedItems.delete(getAdapterPosition());
      } else {
         selectedItems.put(getAdapterPosition(), true);
      }  
 notifyDataSetChanged ();

      textView.setTextColor(Color.WHITE);

      if (mDialogClickListener != null) {
         mDialogClickListener.onItemClick(getAdapterPosition());
      }
    }
  };