【问题标题】:Recycler View Item Selection回收商查看项目选择
【发布时间】:2018-02-09 14:43:44
【问题描述】:

我有一个用 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)
    }
}

每当这个回收器视图加载时,我想显示默认突出显示的前四个项目。来自后端的数据以表单数组列表的形式存储在正文部分数据中。有什么建议我该怎么做?

【问题讨论】:

    标签: android listview kotlin android-recyclerview


    【解决方案1】:

    在您的覆盖方法和 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多