在回收视图中,如何通过单击和不单击来始终突出显示一个适配器项

时间:2018-12-17 10:02:34

标签: java android android-recyclerview recycler-adapter

在这里我单击了项目以更改项目的背景和颜色。我已将单击的项目值存储在数据库中,并更改了布局颜色和文本颜色,并重新创建了适配器,并在刷新时再次显示了列表。 但是当我获得位置时,布局颜色没有改变。请显示正确的路径以始终处理背景项颜色。

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

    private List<TaxiTypeResponse.Message> CarTypesModelsList;
    private Context mContext;
    VehicleTypeView vehicleTypeView;
    int I = -1;
    int idd = 0;
    int II = 0;
    Activity activity;
    GoogleMap map;
    List<VehicleClick> list;

    private SparseBooleanArray selectedItems;

    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);
            selectedItems = new SparseBooleanArray();
            view.setOnClickListener(this);
        }

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

    public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView, Activity activity, GoogleMap map, List<VehicleClick> lists) {

        this.CarTypesModelsList = CarTypesModelsList;
        mContext = context;
        this.vehicleTypeView = vehicleTypeView;
        this.activity = activity;
        this.map = map;
        this.list = lists;
    }

    @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) {


        if (list.size() != 0) {
            II = Integer.parseInt(list.get(0).RideId);
            //setSelection(Integer.parseInt(list.get(0).RideId));
        }

        if (II == 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("#2196F3"));
        }

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

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

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

    public void setSelection(int position) {
        II = position;
        //notifyDataSetChanged();
    }

    public interface setOnitemclick {
        void ImageClick(View view, int position);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}

4 个答案:

答案 0 :(得分:0)

我不确定刷新列表是什么意思。我猜测您是在重新创建适配器并在刷新时再次显示该列表。因此,每次创建适配器时,都用I初始化-1的值。

您需要执行以下初始化。请考虑以下是伪代码,您需要自己实现。

// While declaring your I 
// int I = -1;
int I = getTheStoredValueFromDatabase(); // If there is no entry in database, getTheStoredValueFromDatabase function will return -1

希望您能明白。您可能会考虑对其他存储的值执行相同的操作。

答案 1 :(得分:0)

要保持记录,您需要在 TaxiTypeResponse.Message boolean isClick=false;中添加布尔变量,然后在

中进行切换
holder.setOnItemClickListner(new setOnitemclick() {

    @Override
    public void ImageClick(View v, int position) {
           CarTypesModelsList.get(position).isClick=!CarTypesModelsList.get(position).isClick;
           notifyDataSetChanged();
   }
}

并如下修改以下代码

 if (CarTypesModelsList.get(position).isClick) {

        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"));
 }

答案 2 :(得分:0)

  

注意:onBindViewHolder()不是实现点击的地方   听众,但我只是向您提供实现方法的逻辑   recyclerView中的单个选择。

现在让我们跳到解决方案, 只需按照下面的教程并根据需要更改变量,字段和背景,就必须在onBindViewHolder()的{​​{1}}方法中实现以下方法

首先,初始化lastClickedPosition,然后单击

RecyclerView

让我知道这是否可行。

答案 3 :(得分:0)

在BindViewHolder方法上,您将使用此代码并在全局上设置I = 0

  @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);
}