再循环查看项目选择

我有一个在kotlin中编码的recyclerview适配器。 我正在努力突出回收商视图的前四项。 我该怎么做?

class BodyPartAdapter(var context: Context,var mRecyclerList: RecyclerView,var todayDate:Int, var bodyPartData: ArrayList, var bodyPartSelection: BodyPartSelection, var abc: Int, var firsttime: Boolean, var todayBodypartid: Int, b1: Boolean,var selectedBodypartId: Int, var bodyPartSelected: Boolean): RecyclerView.Adapter() { 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) } } 

每当这个回收者视图加载,我想显示默认高亮的前四个项目。 来自后端的数据存储在正文部分数据中。 任何建议我怎么能做到这一点?