Search code examples
androidandroid-recyclerviewonclicklistener

Changing color statements in Viewholder don't work; only one statement works properly


I'm trying to make an adapter for recyclerview listen click event and change color of text and edittext. When I add just

text.settextView.setHighlightColor(Color.parseColor("~~"));

it works properlybut when I use if-else and add

text.settextView.setHighlightColor(Color.parseColor("~~"));

it doesn't work... At first I thought some statement is skipped, so I added Log.i to check, and I think there are no skipped statements.

static class ViewHolder extends RecyclerView.ViewHolder{
            public  MyCustomEditTextListener myCustomEditTextListener2;
            TextView textView;
            EditText editText;
            public ViewHolder(@NonNull View itemView, MyCustomEditTextListener myCustomEditTextListener2) {
                super(itemView);
                textView=itemView.findViewById(R.id.textView2);
                editText=itemView.findViewById(R.id.editText2);
                this.myCustomEditTextListener2=myCustomEditTextListener2;
                this.editText.addTextChangedListener(myCustomEditTextListener2);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos=getAdapterPosition();
                        if(swtich==0) {   //swtich is defined in adapter class(static int swtich=0;)
                            Log.i("info", "click event & swith");
                            textView.setHighlightColor(Color.parseColor("#e1bee7"));
                            //editText.setHighlightColor(Color.parseColor("#FFF1FF"));
                            swtich = 1;
                        }else if(swtich==1){


                            textView.setHighlightColor(Color.parseColor("#FFFFFF"));
                            editText.setHighlightColor(Color.parseColor("#000000"));
                            swtich=0;}


                        if(pos!=RecyclerView.NO_POSITION){
                            if(mListener!=null){
                                mListener.onItemClick(view,pos);


                            }
                        }
                    }
                });
            }

Solution

  • Try like this. You need to else condition (instead of if-else):

    textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos=getAdapterPosition();
                if(swtich==0) {   //swtich is defined in adapter class(static int swtich=0;)
                    Log.i("info", "click event & swith");
                    textView.setHighlightColor(Color.parseColor("#e1bee7"));
                    editText.setHighlightColor(Color.parseColor("#FFF1FF"));
                    swtich = 1;
                }else{
                    textView.setHighlightColor(Color.parseColor("#FFFFFF"));
                    editText.setHighlightColor(Color.parseColor("#000000"));
                    swtich=0;
                }
    
    
                if(pos!=RecyclerView.NO_POSITION){
                    if(mListener!=null){
                        mListener.onItemClick(view,pos);
                    }
                }
            }
        });