选择或取消选择Adapter Kotlin中的Android问题? [英] Android issue in selection and deselection of items in an Adapter kotlin?

查看:131
本文介绍了选择或取消选择Adapter Kotlin中的Android问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个视图以分组方式显示时间.为此,我将项目分组在一个哈希图中,然后将其传递给活动.从我的活动中,我正在初始化父适配器,以线性方式显示列表.在该父适配器中,还有另一个带有时间项的子适配器,这些时间项使用gridLayout manager以网格方式显示.

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:

现在是实际问题,我一次只能选择1次.但是如您所见,不同的arraylist在这里有不同的选择.当我从同一组中选择一项时,它可以正常工作.但这并不是取消选择其他组的时间.我该如何实现?

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?

父适配器:

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)

}

我的孩子Timeslots适配器->在此适配器中,我编写了代码以选择取消选择时间.但这仅在同一组中发生.

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()

        }
    }
}

}

推荐答案

尝试将一个适配器与网格管理器一起使用

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.

这篇关于选择或取消选择Adapter Kotlin中的Android问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆