在RecycleView中保存并读取选定的卡位置

时间:2017-09-25 13:27:24

标签: android android-recyclerview

我有名单卡。每张卡都有选项:edit,deleteCard,information和defaultCard。当添加第一张卡时,此卡会自动将卡设置为边框绿色。当我们有更多的卡,我们可以选择卡。但我选择例如第二张卡时有问题,然后退出我的应用程序。当我运行我的应用程序然后我的列表卡被选中第一张卡但我选择了第二张卡。如何保存所选卡的位置,然后读取所选的位置卡。

List Card

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

1 个答案:

答案 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