2016-10-21 79 views
0

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(); 
} 

回答

0

創建一個選擇繪製這樣一個(在res /可繪製文件夾):

<?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> 

然後,在您的原始佈局只需添加背景佈局和標記原料爲可點擊:

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)時,它就會變綠!
此外,你應該保持在一個arrayand選擇狀態更改您的代碼是這樣的:

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()); 
     } 
    } 
    }; 
+0

當我點擊的項目,背景會變爲綠色。如果我移開手指,背景變成白色。 – Harshith

+0

這個工程。但是當我點擊其他項目時,先前選擇的項目背景仍爲綠色。我的要求是以前選擇的項目背景應該變成白色。編輯 – Harshith

+0

,您需要跟蹤選定/未選定的項目! – HelloSadness

相關問題