选择单卡视图并更改颜色

时间:2017-05-19 07:25:44

标签: android android-recyclerview android-cardview

我有名单卡。单击我的菜单时显示三个选项。第二个选项将卡设为默认卡。此卡设置绿色边框卡。单击另一张卡时,选择此卡边框为绿色。如果您选择另一张卡作为默认卡,请取消选择以前的默认卡并更改默认颜色。

enter image description here

这是我的适配器与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;

2 个答案:

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

我会尝试尝试一下:

  1. 而不是isSelected make int selectedPos和public,并将其赋予-1值,使其不是有效位置。
  2. 修改如下代码:

    @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;
         ...     
    
  3. 根据您展示的布局需求制作R.layout.default_layout和R.layout.selected_layout。