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

空扰寡人 提交于 2019-12-02 13:36:12

I am not sure what did you mean by refreshing your list. I am guessing that you are recreating the adapter and showing the list again while you are refreshing. Hence the value of I is initialized with -1 each time you are creating the adapter.

You need to do the initialization as follows. Please consider the following is a pseudo code and you need to implement this of your own.

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

I hope you get the idea. You might consider doing the same for other stored values.

asim

for keep track record you need to add Boolean variable in TaxiTypeResponse.Message boolean isClick=false; and toggle this in

holder.setOnItemClickListner(new setOnitemclick() {

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

and modify below code as follow

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

Note: onBindViewHolder() is not a place to implement the click listeners, but I am just providing you the logic for how to implement single selection in recyclerView.

Now lets jump to the solution, simply follow the below tutorial and change the variable, fields, and background according to your need, you have to implement the below method in onBindViewHolder() method of RecyclerView

First, initialize the lastClickedPosition and isclicked

    int lastPositionClicked = -1;
     boolean isClicked = false;

@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

    holder.YOUR_VIEW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // store the position which you have just clicked and you will change the background of this clicked view
            lastPositionClicked = position;
            isClicked = true;

            // need to refresh the adapter
            SlabAdapter.this.notifyDataSetChanged();
        }
    });

    // only change the background of last clicked item 
    if (isClicked && position == lastPositionClicked) {
        // change clicked view background color
    } else {
        // otherwise set the default color for all positions

    }
}

let me know if this works.

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