在循环视图中使用选定的位置项启动活动

时间:2017-08-08 12:33:20

标签: android sqlite android-recyclerview

当应用重新开始时,如何使用“回收视图”中所选卡片项启动活动。

详细信息:

我有三项活动:启动画面卡片列表卡片详细信息

  • 卡片列表活动包含卡片列表。每个单项列表都有菜单。在菜单中有一个选项是"设置默认卡"。

  • 卡详情此活动显示卡的详细信息。

问题:

现在,如果我选择设置默认卡,则所选卡包含绿色边框。

当我设置默认卡并退出我的应用程序时,如果我打开我的应用程序,则接下来应该使用所选卡进入卡详细信息活动。

注意:我使用数据库存储卡详细信息和默认卡

我的代码:

CardAdapter.class

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {

    private static int lastCheckedPos = 0;
    private Context mContext;
    private ArrayList<Card> cardsList;
    boolean isError;

    public CardAdapter(Context mContext, ArrayList<Card> cardsList, String key) {
        this.key = key;
        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 DatabaseHandler(mContext);

     [enter image description here][1]
        //ustawienie zaznaczenia na wybranej pozycji
        if (position == lastCheckedPos) {

            holder.cardView.setBackgroundResource(R.drawable.bordercardview);

        } else {

            holder.cardView.setCardBackgroundColor(Color.WHITE);
            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.cardmenu);
                    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {


                                    case R.id.defaultCard:

                                    int prePos = lastCheckedPos;
                                    lastCheckedPos = position;
                                    notifyItemChanged(prePos);
                                    notifyItemChanged(lastCheckedPos);

                                    break;
                            }
                            return false;
                        }
                    });
                    popupMenu.show();
                }
            });
        }

    }

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

}

1 个答案:

答案 0 :(得分:1)

您可以在创建recyclerView后移动到特定位置:

 recyclerView.getLayoutManager().scrollToPosition(position);