Recyclerview row item selection color change

后端 未结 3 1483
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 19:22

I am able to change the color of the text and background of imageview of the row clicked of my recyclerview in my navigation dra

3条回答
  •  情话喂你
    2021-01-17 20:04

    You can use class for Recycler view TouchListener

        class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
    
            private ClickListener clicklistener;
            private GestureDetector gestureDetector;
    
            public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){
    
                this.clicklistener=clicklistener;
                gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        return true;
                    }
    
                    @Override
                    public void onLongPress(MotionEvent e) {
                        View child=recycleView.findChildViewUnder(e.getX(),e.getY());
                        if(child!=null && clicklistener!=null){
                            clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
    
                        }
                    }
                });
            }
    
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                View child=rv.findChildViewUnder(e.getX(),e.getY());
    
                RecyclerView.ViewHolder viewHolder=rv.findContainingViewHolder(child);
                if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
                    clicklistener.onClick(child,rv.getChildAdapterPosition(child),viewHolder);
    
                }
    
                return false;
            }
    
            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    
            }
    
            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    
            }
        }
    }
    

    Implement an Interface

    public interface ClickListener{
        public void onClick(View view, int position,RecyclerView.ViewHolder viewHolder);
        public void onLongClick(View view, int position);
    }
    

    And in your Main Activity (Here I set Image resource You can set as You like ,Any thing)

    Create an arraylist named "selected" as Integer and add following code as OnClickListener

    recyclerViewSchedule.addOnItemTouchListener(new RecyclerTouchListener(this,
                    recyclerViewSchedule, new ClickListener() {
                @Override
                public void onClick(View view, final int position, RecyclerView.ViewHolder v) {
    
    
    
                    //Values are passing to activity & to fragment as well
    
                      if(!selected.contains(position))
                {
                    selected.add(position);
    
                    ((ScheduleAdapter.MyHolder) v).txtImage.setBackgroundColor(getResources().getColor(R.color.colorAccent));
    
                }else {
    
                    ((ScheduleAdapter.MyHolder) v).txtImage.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
                    selected.remove(new Integer(position));
    
                }
                Toast.makeText(ScheduleActivity.this, "Single Click on position        :"+position,
                        Toast.LENGTH_SHORT).show();
    
    
            }
    
    
                    Toast.makeText(ScheduleActivity.this, "Single Click on position        :"+position,
                            Toast.LENGTH_SHORT).show();
    
    
                }
    
                @Override
                public void onLongClick(View view, int position) {
                    Toast.makeText(ScheduleActivity.this, "Long press on position :"+position,
                            Toast.LENGTH_LONG).show();
                }
            }));
    

    The txtImage I specified here is from RecyclerAdapter,Replace it with Your View

    Happy Coding :-)

提交回复
热议问题