这是我的适配器与show settiing 公共类CardAdapter扩展了RecyclerView.Adapter {
boolean isSelected;
private Card card;
private Context mContext;
private ArrayList<Card> cardslist = new ArrayList<>();
private MySharedPreference mySharedPreference;
public CardAdapter(Context mContext, ArrayList<Card> cardslist) {
this.mContext = mContext;
this.cardslist = cardslist;
notifyDataSetChanged();
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview, parent, false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(final CardViewHolder holder, final int position) {
card = cardslist.get(position);
mySharedPreference = new MySharedPreference();
holder.nameCard.setText(card.getNameCard());
holder.setDate.setText(card.getCreateDate());
holder.expirationDate.setText(card.getExpirationDate());
holder.menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupMenu popupMenu = new PopupMenu(mContext, holder.menu);
popupMenu.inflate(R.menu.cardmenu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
case R.id.defaultCard:
//this option set card as defult
holder.cardView.setBackgroundResource(R.drawable.style_card_shodow);
Toast toast2 = Toast.makeText(mContext, "Podana karta została ustawiona jako domyślna.", Toast.LENGTH_LONG);
LinearLayout layout = (LinearLayout) toast2.getView();
if (layout.getChildCount() > 0) {
TextView tv = (TextView) layout.getChildAt(0);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
toast2.show();
break;
答案 0 :(得分:2)
您可以在卡片模型中添加一个额外的布尔变量。并且可以像这样在onbindViewHolder中添加一个检查:
*if(card.booleanIsTrue())
{
put green color
}
else
{
put other color
}*
you can set it to true or false onClick like this:
*if(card.booleanIsTrue())
{
put other color
card.setbooleanFalse;
}
else
{
put green color
card.setbooleanTrue;
}
notifyDataSetChanged();*
答案 1 :(得分:0)
我会尝试尝试一下:
修改如下代码:
@Override
public int getItemViewType(int position) {
if (selectedPos == position) {
return 1;
return 2;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view;
if (viewType == 1) {
view = layoutInflater.inflate(R.layout.selected_layout, parent, false);
return new CardViewHolder(view);
}
else {
view = layoutInflater.inflate(R.layout.default_layout, parent, false);
return new CardViewHolder(view);
}
}
public void onBindViewHolder(final CardViewHolder holder, final int position) {
card = cardslist.get(position);
mySharedPreference = new MySharedPreference();
holder.nameCard.setText(card.getNameCard());
holder.setDate.setText(card.getCreateDate());
holder.expirationDate.setText(card.getExpirationDate());
holder.menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupMenu popupMenu = new PopupMenu(mContext, holder.menu);
popupMenu.inflate(R.menu.cardmenu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
case R.id.defaultCard:
//this option set card as defult
selectedPos = position;
notifyDataSetChanged();
break;
...