recyclerview,单击“突出显示",并使其他未突出显示 [英] recyclerview, on click Highlight and make others not highlighted

查看:85
本文介绍了recyclerview,单击“突出显示",并使其他未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张信用卡的资源回收视图,如果您单击其中的一个,我希望它更改该信用卡的背景资源,并使所有其他资源恢复默认值,出于某种原因,我为此奋斗了大约5个小时,并且无法做到正确.

I have a recyclerview of credit cards, if you click on one I want it to change the background resource of that one and make all the other ones back to default, for some reason I have been fighting this for about 5 hours and cant get it right.

这是不及格的课程

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

    private List<Card> checkOutCard;
    private int screenWidth;
    private int selected;






    public CheckOutCardRecycler(Activity activity, List<Card> checkOutCard){


        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        this.screenWidth = size.x;
        this.checkOutCard = checkOutCard;
        selected=-1;


    }



    public static class CardViewHolder extends RecyclerView.ViewHolder {
         CardView card;
         LinearLayout row;
        static TextView etCardNumber;
        static TextView etName;
        static TextView etDate;
        static LinearLayout creditCard;



        CardViewHolder(View itemView) {
            super(itemView);
            card = (CardView)itemView.findViewById(R.id.card_view);
            etCardNumber = (TextView)itemView.findViewById(R.id.etCardNumber);
            etName = (TextView)itemView.findViewById(R.id.etName);
            etDate = (TextView)itemView.findViewById(R.id.etDate);
            creditCard = (LinearLayout) itemView.findViewById(R.id.creditCard);

        }
    }

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

    public int getSelected() {
        return selected;
    }

    public int getPaymentMethod(int i) {
        return Integer.parseInt(checkOutCard.get(i).getId());
    }


    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_checkout_cards, viewGroup, false);
        CardViewHolder pvh = new CardViewHolder(v);

        return pvh;
    }

    @Override
    public void onBindViewHolder(final CardViewHolder cardViewHolder, final int i) {
        CardViewHolder.etCardNumber.setText("**** **** **** " + checkOutCard.get(i).getLast_four_digits());
        CardViewHolder.etName.setText(checkOutCard.get(i).getName());
        CardViewHolder.etDate.setText(checkOutCard.get(i).getExpiry_month() +"/"+checkOutCard.get(i).getExpiry_year().substring(2,4));
        if (selected<0)
            if (checkOutCard.get(i).getDefaultcard()==1){
                selected=i;
            }

        CardViewHolder.creditCard.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                if (i!=selected) {

                    checkOutCard.get(selected).setDefaultcard(0);
                    checkOutCard.get(i).setDefaultcard(1);
                    selected=-1;

                    notifyDataSetChanged();




                }
            }
        });


        if (selected==i){
            if (checkOutCard.get(i).getBrand().toLowerCase().equals("visa"))
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenvisaselected);
            else if (checkOutCard.get(i).getBrand().toLowerCase().equals("mastercard"))
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenmastercardselected);
            else
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenselected);
        }
        else {

            if (checkOutCard.get(i).getBrand().toLowerCase().equals("visa"))
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenvisa);
            else if (checkOutCard.get(i).getBrand().toLowerCase().equals("mastercard"))
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenmastercard);
            else
                CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreen);

        }


    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

    }




}

推荐答案

感谢大家的回答,终于在尝试了所有解决方案数天之后,没有一个工作并且不知道为什么我终于弄清楚了,我一个愚蠢的语法问题,这让我头疼.

Thanks everyone for your answers, finally after days of trying all of your solutions and none of them working and not working out why I finally figured it out, I had a stupid syntax problem that was causing me headaches.

我要做的就是在onBindViewHolder内部,我必须将所有CardViewHolder更改为cardViewHolder.我想这使这个问题对其他人来说毫无意义,因此,我的天职表示,并再次感谢其他长时间做出回答的人所做的一切努力

All I had to do was inside onBindViewHolder I had to change all of the CardViewHolder to cardViewHolder. I guess that makes this question pretty pointless for other people so my apolgies and thanks again for all the effort the other people who made lengthly answers put in

这篇关于recyclerview,单击“突出显示",并使其他未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆