recycleView implements item Click to change the item color, and other item colors change back to

Keywords: Android

recycleView implements item Click to change the item color, and other item colors change back to

The horizontal scrolling effect is needed in the project. According to the previous ideas, I will write a ScrollView, add a LinearLayout inside, add controls in the code dynamically, and then delete or change colors dynamically. Now android has a new control RecyclerView, which can facilitate the realization of such functions. Please refer to this article for specific use methods: Android custom RecyclerView for real Gallery effect
The main problem is that I click an item, change the color, click other items, change the color, and the last time I clicked changed back to the original color. At the beginning of the implementation, I directly modified the control. When I found that the color would be disordered when sliding, so I added a list in the adapter to mark, and change the color of the control according to the mark
Corresponding adapter

public class AddDeviceAdapter extends RecyclerView.Adapter<AddDeviceAdapter.ViewHolder> /*implements View.OnClickListener*/{

    private OnItemClickListener mOnItemClickListener = null;

    private LayoutInflater mInflater;
    private List<AddDevice> mDatas;

    private AddDevice addDevice;
    private List<Boolean> isClicks;//Whether the control is clicked is false by default. If clicked, change the value. The control changes its color according to the value

    public AddDeviceAdapter(Context context, List<AddDevice> datas) {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        isClicks = new ArrayList<>();
        for(int i = 0;i<mDatas.size();i++){
            isClicks.add(false);
        }
    }

    public void setDatas(List<AddDevice> datas) {
        mDatas = datas;
        L.e("AddDeviceAdapter's mDatas===" + new Gson().toJson(mDatas));
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_add_device, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.viewSpace = view.findViewById(R.id.viewSpace);
        viewHolder.ivAddDevice = (ImageView) view.findViewById(R.id.ivAddDevice);
        viewHolder.tvAddDevice = (TextView) view.findViewById(R.id.tvAddDevice);

        //Register the created View for click events
     //   view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        addDevice = mDatas.get(i);
        viewHolder.ivAddDevice.setImageResource(addDevice.getResId());
        viewHolder.tvAddDevice.setText(addDevice.getItemText());
        //Save the data in the Tag of itemView so that it can be acquired when clicking
        viewHolder.itemView.setTag(viewHolder.tvAddDevice);
        if(isClicks.get(i)){
            viewHolder.tvAddDevice.setTextColor(Color.parseColor("#00a0e9"));
        }else{
            viewHolder.tvAddDevice.setTextColor(Color.parseColor("#ffffff"));
        }
//        if(addDevice.isFirst()) {
//            viewHolder.viewSpace.setVisibility(View.GONE);
//        }

        // If callback is set, click event is set
        if (mOnItemClickListener != null) {
           viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i = 0; i <isClicks.size();i++){
                        isClicks.set(i,false);
                    }
                    isClicks.set(i,true);
                     notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(viewHolder.itemView,viewHolder.tvAddDevice, i);
               }
            });
        }
    }

   /* @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //Note that here we use the getTag method to get the data
            mOnItemClickListener.onItemClick(, textView,(int)v.getTag());
        }
    }*/

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        View viewSpace;
        ImageView ivAddDevice;
        TextView tvAddDevice;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, TextView textView,int position);
    }
}

Posted by I Am Chris on Tue, 31 Mar 2020 08:33:13 -0700