In Recycle view how to Highlight always one adapter item with the click and without click

前端 未结 4 1694
刺人心
刺人心 2021-01-29 14:24

Here I clicked on the item to change item background and color. I\'ve stored the clicked item value in the database and change the layout color and text color and recreating the

4条回答
  •  礼貌的吻别
    2021-01-29 15:00

    on BindViewHolder method you'll use this code and set I=0 on globally

      @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(final CarTypesHolder holder, int position) {
    
        SharedPreferences sharedPreferences = activity.getSharedPreferences("mSelected", Context.MODE_PRIVATE);
        TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
        holder.mCarType.setText(carTypesModel.getName());
        holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
        int color = Color.parseColor(PreferenceHandler.readString(mContext, PreferenceHandler.SECONDRY_COLOR, "#006fb6"));
        holder.mCarType.setTextColor(color);
    
        holder.setOnItemClickListner(new setOnitemclick() {
            @Override
            public void ImageClick(View v, int position1) {
                I = position1;
                notifyDataSetChanged();
                try {
                    if (list.size() != 0) {
                        VehicleTypeFragment.myAppRoomDataBase.userDao().delete();
                        list.clear();
                    }
                    VehicleClick vehicleClick = new VehicleClick();
                    vehicleClick.setRideId(String.valueOf(position1));
                    VehicleTypeFragment.myAppRoomDataBase.userDao().insert(vehicleClick);
                    list.add(vehicleClick);
                } catch (Exception e) {
    
                }
    
            }
        });
    
        if (I == position) {
            holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
            holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
            Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
            holder.llRoot.startAnimation(bounce);
        } else {
            holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
            holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
        }
    
        Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
    }
    

提交回复
热议问题