How to Highlighted single Text and background color when using recycle view

ぃ、小莉子 提交于 2019-12-20 05:58:15

问题


I'll use RecyclerView And I've more than 10 items in the list And I Have to change a text color and background layout on a single click of the item and else all the item color not change. Please suggest me the right way to solve this issue. I'll try to change the color in BindViewHolder, ViewHolder and Adapter click item but I'm Successful to change the color but unsuccessful to change the color back.

public class LoadVehicleTypeAdapter  extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {

    private List<TaxiTypeResponse.Message> CarTypesModelsList;
    private Context mContext;
    VehicleTypeView vehicleTypeView;
    setOnitemclick listener;
    private SparseBooleanArray selectedItems = new SparseBooleanArray();
    int I=-1;
    public class CarTypesHolder extends RecyclerView.ViewHolder {
        public CustomTextView mCarType;
        public CircleImageView mCarTypeImage;
        LinearLayout llRoot;
        CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();
        setOnitemclick listener;

        public CarTypesHolder(final View view) {
            super(view);
            mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
            mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
            llRoot = (LinearLayout)view.findViewById(R.id.root1);
            cardView=(CardView) view.findViewById(R.id.cardf);


        }

        public void setOnItemClickListner(setOnitemclick listener12) {
            listener=listener12;
        }

    }
    public void setOnItemClickListner(setOnitemclick listener12) {
        listener=listener12;
    }


    public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView) {
        this.CarTypesModelsList = CarTypesModelsList;
        mContext = context;
        this.vehicleTypeView = vehicleTypeView;
    }


    @Override
    public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.frag_cartype_inflated_view, parent, false);
        return new CarTypesHolder(itemView);
    }

    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(final CarTypesHolder holder, final int position) {

        TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
        I=CarTypesModelsList.get(position).getID();
         holder.mCarType.setText(carTypesModel.getName());
         holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
        int color = Color.parseColor(PreferenceHandler.readString(mContext,PreferenceHandler.SECONDRY_COLOR,"#006fb6"));
        holder.llRoot.setSelected(selectedItems.get(position, false));
        holder.mCarType.setTextColor(color);
        holder.setOnItemClickListner(listener);

        holder. llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                I=position;
                if (I==position)
                {

                    holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
                    holder.mCarType.setTextColor(Color.parseColor("#ffffff"));

                }
                else
                {
                    holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
                    holder.mCarType.setTextColor(Color.parseColor("#00000"));

                }

            }
        });



       Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);

    }

    @Override
    public long getItemId(int position) {
        return CarTypesModelsList.get(position).getID();
    }

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

    public void setSelection(LinearLayout imageView,CustomTextView textView,boolean value,int position){
        if(value){

            imageView.setBackgroundColor(Color.parseColor("#999999"));
            textView.setTextColor(Color.parseColor("#FFFFFF"));

        }else{
            System.out.println("11111111111111111000000111111111111");
            imageView.setBackgroundColor(Color.parseColor("#f3f3f3"));
            textView.setTextColor(Color.parseColor("#2196F3"));

         }
     }
     public interface  setOnitemclick{
        void ImageClick(int position, String Name,String Description,int id);
         void ImageClickfade(int position, String Name,String Description,int id);
     }
    @Override
    public int getItemViewType(int position) {
        return position;
    }



}

回答1:


You can try this, You'll use to update gradle

maven { url "https://maven.google.com" }

and update the code

public class LoadVehicleTypeAdapter  extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {

private List<TaxiTypeResponse.Message> CarTypesModelsList;
private Context mContext;
VehicleTypeView vehicleTypeView;
int I=-1;
public class CarTypesHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {
    public CustomTextView mCarType;
    public CircleImageView mCarTypeImage;
    LinearLayout llRoot;
    CardView cardView;
    setOnitemclick listener;
    public void setOnItemClickListner(setOnitemclick listener)
    {
        this.listener=listener;
    }

    public CarTypesHolder(View view) {
        super(view);

        mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
        mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
        llRoot = (LinearLayout)view.findViewById(R.id.root1);
        cardView=(CardView) view.findViewById(R.id.cardf);
        view.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
    listener.ImageClick(v,getAdapterPosition());
    }
}


public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView) {
    this.CarTypesModelsList = CarTypesModelsList;
    mContext = context;
    this.vehicleTypeView = vehicleTypeView;
}


@Override
public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.frag_cartype_inflated_view, parent, false);
    return new CarTypesHolder(itemView);
}

@SuppressLint("ResourceType")
@Override
public void onBindViewHolder( final  CarTypesHolder holder,  int position) {

    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();

        }
    });
    if (I==position)
    {
        System.out.println("11100011111....");
        holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
        holder.mCarType.setTextColor(Color.parseColor("#ffffff"));

    }
    else
    {
        System.out.println("11100011111----");
        holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
        holder.mCarType.setTextColor(Color.parseColor("#2196F3"));

    }

   Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);

}

@Override
public long getItemId(int position) {
    return CarTypesModelsList.get(position).getID();
}

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

public void setSelection(LinearLayout imageView,CustomTextView textView,boolean value,int position){
    if(value){

        imageView.setBackgroundColor(Color.parseColor("#999999"));
        textView.setTextColor(Color.parseColor("#FFFFFF"));

    }else{
        System.out.println("11111111111111111000000111111111111");
        imageView.setBackgroundColor(Color.parseColor("#f3f3f3"));
        textView.setTextColor(Color.parseColor("#2196F3"));

     }
 }
 public interface  setOnitemclick{
    void ImageClick(View view,int position);
 }
@Override
public int getItemViewType(int position) {
    return position;
}

}




回答2:


Replace your click like below

public void onBindViewHolder(final CarTypesHolder holder, final int position) {

    TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
    I=CarTypesModelsList.get(position).getID();
     holder.mCarType.setText(carTypesModel.getName());
     holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
    int color = Color.parseColor(PreferenceHandler.readString(mContext,PreferenceHandler.SECONDRY_COLOR,"#006fb6"));
    holder.llRoot.setSelected(selectedItems.get(position, false));
    holder.mCarType.setTextColor(color);
    holder.setOnItemClickListner(listener);

    holder. llRoot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            I=position;

                holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
                holder.mCarType.setTextColor(Color.parseColor("#ffffff"));


            notifyDataSetChanged()

        }
    });

    if (I==position)
    {

        holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
        holder.mCarType.setTextColor(Color.parseColor("#ffffff"));

    }
    else
    {
        holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
        holder.mCarType.setTextColor(Color.parseColor("#00000"));

    }



   Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);

}


来源:https://stackoverflow.com/questions/53648621/how-to-highlighted-single-text-and-background-color-when-using-recycle-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!