Search code examples
androidonclickonclicklistenerandroid-cardview

How to handle the Second Click on a CardView


Okay, I'm just trying to handle the second click on a CardView.

For example, Now when i(or a user) clicks on the CardView, It will make the CardView, TRANSPARENT and also, Focusable and some other stuffs like adding Android text into a EditText.

So, I need to handle when user or i myself clicked on the CardView for the second time, CardView changes to Color.WHITE and set Focusable to false or other stuffs...

How can i do that?

Here is what i did so far:

 final CardView cvAnd = (CardView) findViewById(R.id.and_cv);
        cvAnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                cvAnd.setTag("FirstClick");
                if (cvAnd.getTag().equals("FirstClick") && TextUtils.isEmpty(mTagField.getText().toString())) {
                    mTagField.setText(R.string.android);
                } else {
                    mTagField.setText(mTagField.getText() + "Android");
                }
                cvAnd.setCardBackgroundColor(Color.TRANSPARENT);
                cvAnd.setFocusable(true);

                //First click

                cvAnd.setTag("SecondClick");
                if (cvAnd.getTag().equals("SecondClick")) {
                    cvAnd.setCardBackgroundColor(Color.WHITE);
                    cvAnd.setFocusable(false);

                }

                //Second click maybe?
            }
        });

The idea was to set the tag(in the first time click) to FirstClick then handle it if tag was the FirstClick doing the first click stuffs and so on...

But, The thing is, I don't really have clue about How to handle that second time click.


Solution

  • Here a proof of concept as you asked. Let me know if you don't understand something.

    public class CardViewAdapter extends extends RecyclerView.Adapter<CardViewAdapter.MyViewHolder> {
    
        public class MyViewHolder extends RecyclerView.ViewHolder {
            public boolean isFirstSelected;
            public CardView cardView;
    
            public MyViewHolder(View view) {
                super(view);
                cardView = (CardView) view.findViewById(R.id.card);
            }
    
            public void reset(){
                //put here all the properties which need to be resetted
                isFirstSelected = false;
            }
        }
    
    
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.card_row, parent, false);
    
            return new MyViewHolder(itemView);
        }
    
        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            //set card click listener here and use
            //holder.isFirstSelected to handle the internal logic
        }
    
        @Override
        public void onViewRecycled(MyViewHolder holder) {
            //reset the viewholder state
            holder.reset();
        }
    
    }
    

    I've took your snippet and tried to translate the logic into this. Probably you need to change the logic but now you have a starting point.

    final CardView cvAnd = (CardView) findViewById(R.id.and_cv);
        cvAnd.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View view) {
                if (!isClicked) {
                    if (TextUtils.isEmpty(mTagField.getText().toString())) {
                        mTagField.setText(R.string.android);
                        cvAnd.setCardBackgroundColor(Color.TRANSPARENT);
                        cvAnd.setFocusable(true);
                    }
    
                } else {
                    if (mTagField.getText().toString().contains("Android")) {
                        mTagField.getText().clear();
                    } else {
                        mTagField.setText(mTagField.getText() + ", Android");
                    }
                    cvAnd.setCardBackgroundColor(Color.WHITE);
                    cvAnd.setFocusable(false);
                }
                //reverse boolean
                isClicked = !isClicked;
            }
        });