我有一个用kotlin编码的recyclerview适配器。我正在努力突出回收者视图的前四项。我该怎么办?
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)
}
}
每当此回收站视图加载时,我想显示默认突出显示的前四个项目。来自形式数组列表中后端的数据存储在正文部分数据中。有什么建议吗?
答案 0 :(得分:0)
在您的重写方法和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
}