简体   繁体   English

回收者查看项目选择

[英]Recycler View Item Selection

I have a recyclerview Adapter coded in kotlin. 我有一个用kotlin编码的recyclerview适配器。 I am trying to highlight first four item of recycler view. 我试图强调回收者视图的前四项。 How can I do it? 我该怎么做?

class BodyPartAdapter(var context: Context,var mRecyclerList: RecyclerView,var todayDate:Int, var bodyPartData: ArrayList<BodyPart>, var bodyPartSelection: BodyPartSelection, var abc: Int, var firsttime: Boolean, var todayBodypartid: Int, b1: Boolean,var selectedBodypartId: Int, var bodyPartSelected: Boolean): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    interface BodyPartSelection {
        fun onbodyPartClicked(firsttime:Boolean,bodyPart: BodyPart,selected:Boolean)
    }

    override fun getItemCount(): Int {
        return bodyPartData.size
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        for(i in bodyPartData) {
            Log.e("IntialBodypartData",i.body_part_name + " " + i.selected.toString())
        }
        val v = LayoutInflater.from(context).inflate(R.layout.body_part_item, parent, false)
        return Item(v)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        (holder as Item).bindData(context,mRecyclerList, bodyPartData[position],bodyPartData.size, bodyPartSelection = bodyPartSelection, selectedDay = abc, adapter = this@BodyPartAdapter,todayBodypartid = todayBodypartid, firstTime = firsttime,selectedBodypartId = selectedBodypartId,isBodypartSelected = bodyPartSelected)
    }
}

class Item(itemView: View) : RecyclerView.ViewHolder(itemView) {
    lateinit var context:Context
    fun bindData(context: Context, mRecyclerList: RecyclerView,bodyPart: BodyPart, size: Int,bodyPartSelection: BodyPartAdapter.BodyPartSelection, selectedDay: Int, adapter: BodyPartAdapter, todayBodypartid: Int, firstTime:Boolean, selectedBodypartId: Int, isBodypartSelected:Boolean) {
        Log.e("TodayBodyPart",todayBodypartid.toString())
            this.context = context
            itemView.bodyPartName.text = bodyPart.body_part_name
            if (WorkoutPresenter.firstTime1) {
                Log.e("Firsttime",WorkoutPresenter.firstTime1.toString())
                if (bodyPart.body_part_id == todayBodypartid) {
                    itemView.relativeLayout.isSelected = true
                    itemView.body_part_image.setColorFilter(Color.argb(255, 255, 255, 255))
                    bodyPartSelection.onbodyPartClicked(true,bodyPart = bodyPart, selected = itemView.relativeLayout.isSelected)
                } else {
                    itemView.relativeLayout.isSelected = false
                    Log.e("currentstatus",WorkoutPresenter.isBodyBodyPartSelected.toString() + " " + WorkoutPresenter.selectedBodyPartid.toString())
                    itemView.body_part_image.setColorFilter(Color.parseColor("#e0e0e0"))
                }
            } else {
               if(bodyPart.body_part_id==WorkoutPresenter.selectedBodyPartid) {
                    Log.e("currentstatus0",WorkoutPresenter.isBodyBodyPartSelected.toString() + " " + WorkoutPresenter.selectedBodyPartid.toString())
                    if(WorkoutPresenter.isBodyBodyPartSelected){
                        itemView.relativeLayout.isSelected = true
                        Log.e("currentstatus1",WorkoutPresenter.isBodyBodyPartSelected.toString())
                        itemView.body_part_image.setColorFilter(Color.argb(255, 255, 255, 255))
                    } else {
                        itemView.relativeLayout.isSelected = false
                        itemView.body_part_image.setColorFilter(Color.parseColor("#e0e0e0"))
                        // Log.e("first time", BodyPartAdapter.firstTime.toString())
                    }
                } else {
                   // Log.e("elseCurrentStatus3",WorkoutPresenter.isBodyBodyPartSelected.toString() + " " +WorkoutPresenter.selectedBodyPartid.toString())
                   //itemView.body_part_image.setColorFilter(Color.parseColor("#e0e0e0"))
                }
            }

            Log.e("Imageuri", bodyPart.body_part_url)

            loadImage(bodyPart.body_part_url, context, imageView = itemView.body_part_image)

           // adapter.updatePrevBodyPart(bodyPart.body_part_name)
            itemView.setOnClickListener {
           Log.e("ITEMcLICKEDOkay","done");
                if (itemView.relativeLayout.isSelected) {
                    itemView.relativeLayout.isSelected = false
                    Log.e("ItemviewSeelectedisyehi",itemView.relativeLayout.isSelected.toString()  + adapterPosition.toString())
                    bodyPartSelection.onbodyPartClicked(false,bodyPart = bodyPart, selected = false)
                    WorkoutPresenter.upadteStaticValuesforRecyclerView(false,bodyPart.body_part_id,false)
                    itemView.body_part_image.setColorFilter(Color.parseColor("#e0e0e0"))
                } else {
                    itemView.relativeLayout.isSelected = true;
                    itemView.body_part_image.setColorFilter(Color.parseColor("#e0e0e0"))
                    bodyPartSelection.onbodyPartClicked(false,bodyPart = bodyPart, selected = true)
                    WorkoutPresenter.upadteStaticValuesforRecyclerView(false,bodyPart.body_part_id,true)
                }
            }
    }
    private fun loadImage(imageIcon: String, context: Context, imageView: ImageView) {
        Picasso.with(context).load(imageIcon).into(imageView)
    }
}

Whenever this recycler view load I want to show first four items highlighted by default. 每当加载此回收器视图时,我都希望显示默认情况下突出显示的前四个项目。 Data coming from backend in the form array list stored in body part data. 来自后端的数据存储在表单数组列表中的身体部位数据中。 Any suggestions how can I do it? 有什么建议我该怎么做?

add the following line of code in your override method and viewHolderClass(Items) 在您的重写方法和viewHolderClass(Items)中添加以下代码行

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if(position in 0..3)
            holder.highLight()
        //and your rest of the hodler view class methods here
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)  {
    val textView = view.textView
    fun highLight()
    {
       textView.setBackgroundColor(Color.YELLOW)
       //or you can set the background color of entire holder by accessing through 
       //some id like textView stated above
    }
    // rest of the code of view holder class here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM