How to highlight selected item in RecyclerView

后端 未结 8 1310
猫巷女王i
猫巷女王i 2020-12-14 17:49

I have used RecyclerView for showing thumbnails in my Image Editing app.Each item of its comprises of a ImageView(thumbnail) and a textView.In my application I want to

相关标签:
8条回答
  • 2020-12-14 18:50

    update your enum to class first for flag

    public class Type {
        public int type; // 0-Original,1-Grayscale,2-Sepia.... same as enum
        public int selected;
    }
    

    now in adapter

    @Override
    public void onBindViewHolder(FiltersAdapter.ViewHolder holder, int position) {
        int typeOfItem = mDataSet.get(position).type
        switch (mDataSet.get(position)) {
            case 0:
                holder.image.setImageResource(R.drawable.no_filter);
                break;
            case 1:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new GrayscaleTransformation())
                        .into(holder.image);
                break;
            case 2:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new SepiaFilterTransformation(mContext))
                        .into(holder.image);
                break;
            case 3:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new ContrastFilterTransformation(mContext, 2.0f))
                        .into(holder.image);
                break;
            case 4:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new InvertFilterTransformation(mContext))
                        .into(holder.image);
                break;
            case 5:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new PixelationFilterTransformation(mContext, 20))
                        .into(holder.image);
                break;
            case 6:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new SketchFilterTransformation(mContext))
                        .into(holder.image);
                break;
            case 7:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
                        .into(holder.image);
                break;
            case 8:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new BrightnessFilterTransformation(mContext, 0.5f))
                        .into(holder.image);
                break;
            case 9:
                Picasso.with(mContext)
                        .load(R.drawable.no_filter)
                        .transform(new VignetteFilterTransformation(mContext, new PointF(0.5f, 0.5f),
                                new float[]{0.0f, 0.0f, 0.0f}, 0f, 0.75f))
                        .into(holder.image);
                break;
            default:
                holder.image.setImageResource(R.drawable.no_filter);
                break;
        }
        holder.title.setText(mDataSet.get(position).name());
        if(mDataSet.get(position).selected == 0){
            holder.title.setTypeface(null, Typeface.BOLD);
        } else {
            holder.title.setTypeface(null,Typeface.NORMAL);
        }
    }
    

    not on every click or select update your list then call notifydatasetChanged() of adapter

    0 讨论(0)
  • 2020-12-14 18:52

    If you don't want to use notifyDataSetChanged
    Implemented by kotlin

     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        adapterOrders = OrdersSendAdapter(this, listOrders)
        recyclerOrders = view.findViewById<RecyclerView>(R.id.recycler_orders)
        recyclerOrders.layoutManager = LinearLayoutManager(activity)
        recyclerOrders.setHasFixedSize(true)
        recyclerOrders.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
        recyclerOrders.adapter = adapterOrders
      }
    
    /* This method will be call when item of adapter has an event */
    fun getListDetail(position: Int, idOrders: String) {
        for (index in 0 until recyclerOrders.childCount) {
             recyclerOrders[index].setBackgroundColor(Color.parseColor("#ffffff"))
        }
        recyclerOrders[position].setBackgroundColor(ContextCompat.getColor(context!!, R.color.color_select))
    }
    
    0 讨论(0)
提交回复
热议问题