此CardAdapter.class
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private int lastCheckedPos = 0;
private Context mContext;
private List<Card> cardsList = new ArrayList<>();
private MySharepreference mySharepreference;
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_card, parent, false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(CardViewHolder holder, final int position) {
final Card card = cardsList.get(position);
databaseHandler = new CardDatabaseHandler(mContext);
mySharepreference = new MySharepreference();
holder.nameCard.setText(card.getNumberCard());
holder.setCurrentDateText.setText(card.getCreateDate());
holder.setExpirationDateText.setText(card.getExpirationDate());
if (lastCheckedPos == position) {
mySharepreference.saveNumberCard(mContext, card.getId());
holder.cardView.setBackgroundResource(R.drawable.bordercardview);
holder.menu.setOnClickListener(new View.OnClickListener() {
Typeface custom_fonts = Typeface.createFromAsset(mContext.getAssets(), "fonts/OpenSans-Regular.ttf");
Typeface custom_fonts_Bold = Typeface.createFromAsset(mContext.getAssets(), "fonts/OpenSans-Bold.ttf");
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(mContext, v);
popupMenu.inflate(R.menu.cardmenudefault);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.editCardD:
break;
case R.id.deleteCardD:
break;
case R.id.informationD:
break;
}
return false;
}
});
popupMenu.show();
}
});
} else {
holder.cardView.setBackgroundResource(R.drawable.bordercardviewwhite);
holder.menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(mContext, v);
popupMenu.inflate(R.menu.cardmenu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.editCard:
break;
case R.id.deleteCard:
break;
case R.id.informationAboutCard:
break;
case R.id.defaultCard:
lastCheckedPos = position;
notifyDataSetChanged();
break;
}
return false;
}
});
popupMenu.show();
}
});
}
}
@Override
public int getItemCount() {
return cardsList.size();
}
}
答案 0 :(得分:-1)
使用onSaveInstanceState()
回调方法存储所选卡的位置。
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt("CARD_POSITION", 12);
}
并在onCreate()
添加以下代码
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if (savedInstanceState != null) {
index = savedInstanceState.getString("CARD_POSITION");
}
}
现在您可以将此索引分配给您的Recyclerview。
有关参考,请查看this link。