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

前端 未结 4 1693
刺人心
刺人心 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 14:41

    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.

    0 讨论(0)
  • 2021-01-29 14:49

    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"));
     }
    
    0 讨论(0)
  • 2021-01-29 14:52

    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.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题