2017-08-08 49 views
0

如何在应用程序重新启动时使用回收视图中所选卡片项目开始活动。开始活动,在回收视图中选择位置项目

详情:

我有三个活动:闪屏卡册卡详细

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

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

问题:

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

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

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

我的代码:

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

} 
+0

将您的lastCheckedPos存储到SharedPreferences并使用它来填充适配器 – anonymous

回答

1

您可以通过创建后,移动到特定位置recyclerView:

recyclerView.getLayoutManager().scrollToPosition(position); 
相关问题