Android issue in selection and deselection of items in an Adapter kotlin?

一世执手 提交于 2019-12-23 16:46:12

问题


I am creating a view to display time in grouping manner. For that I grouped items in an hashmap and passed it to the activity. From my activity, I am initializing the parent adapter to display the list in Linear fashion. In that parent adapter there is another child adapter with times items which are displayed using gridLayout manager in grid fashion.

The Image of the current scenario and the issue is as below:

Now the actual issue, I want to select only 1 time at a time. But as you see, different arraylist has different selections here. When I select 1 item from the same group, it works fine. But it is not deselecting other groups time. How can I achieve that?

Parent Adapter:

class TimePickerAdapter(context: Context, arrTimeSlots: ArrayList<ListItem>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

val mContext = context
val mList = arrTimeSlots

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
    return if (viewType == ListItem.TYPE_HEADER) {
        TimeHeaderHolder(LayoutInflater.from(parent?.context).
                inflate(R.layout.item_time_group, parent, false))
    } else {
        TimeSlotsHolder(LayoutInflater.from(parent?.context).
                inflate(R.layout.item_time_child, parent, false))
    }
}

override fun getItemCount() = mList.size

override fun getItemViewType(position: Int): Int {
    return mList[position].type
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
    if (holder is TimeHeaderHolder) {
        holder.itemView?.apply {
            with(mList[position] as TimeHeaderItem) {
                tvTimeGroupHeader.text = date
            }
        }
    } else {
        holder?.itemView?.apply {
            with(mList[position] as TimeSlotItem) {
                val mAdapter = TimeSlotsAdapter(mContext, pojoOfTimeSlots)
                rvTimeSlotsChild.layoutManager = GridLayoutManager(mContext, 3)
                rvTimeSlotsChild.adapter = mAdapter
            }
        }
    }
}

inner class TimeHeaderHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

inner class TimeSlotsHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

}

My child Timeslots adapter -> In this adapter, I have written a code to select deselect time. But it is happening for the same group only.

class TimeSlotsAdapter(context: Context, arrTimeSlots: List<TimeSlots>?) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

val mContext = context
val mList = arrTimeSlots!!

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
    return TimePickerHolder(LayoutInflater.from(parent?.context).
            inflate(R.layout.item_time_picker, parent, false))
}

override fun getItemCount() = mList.size

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
    holder?.itemView?.apply {
        with(mList[position]) {
            tvTimeView.text = convertedTime
            if (isSelected) {
                tvTimeView.background = ContextCompat.getDrawable(context,
                        R.drawable.time_rounded_corner_selected)
                tvTimeView.setTextColor(ContextCompat.getColor(context, R.color.white))
            } else {
                tvTimeView.background = ContextCompat.getDrawable(context,
                        R.drawable.time_rounded_corner)
                tvTimeView.setTextColor(ContextCompat.getColor(context, R.color.grey_text_7))
            }
        }
    }
}

inner class TimePickerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    init {
        itemView.setOnClickListener {
            for (i in mList.indices) {
                mList[i].isSelected = false
            }
            mList[adapterPosition].isSelected = true
            notifyDataSetChanged()

        }
    }
}

}


回答1:


Try to use one adapter with grid manager

GridLayoutManager gridLayoutManager = new GridLayoutManager(getAppContext(), 3); 
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
        @Override
        public int getSpanSize(int position) {
        //define span size for this position
        //for example, if you have 2 column per row, you can implement something like that:
        if(position == youRule) {
            return 3; // 3 item in row
        } else {
            return 1; // 1 item in row
        }
    }
});

Also you should merge your two adapters in one. Unlike hashmap for your items, use sorted list item [header, time, time, time, header, time, time, time, etc]. Now you can simple check what item need to mark as selected.



来源:https://stackoverflow.com/questions/46217375/android-issue-in-selection-and-deselection-of-items-in-an-adapter-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!