Нужна ваша помощь, ребята. У меня есть фрагмент, где, по нажатию кнопки, я показываю диалог с вложенным recyclerView с кучей иконок. Для щелчка по значку мне нужно отправить идентификатор значка обратно во фрагмент или его viewModel, а затем закрыть диалоговое окно.
Фрагмент (очень просто)
class NewItemFragment : Fragment() {
private lateinit var viewModel: NewItemViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel = ViewModelProvider(this).get(NewItemViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_new_i, container, false)
root.new_Item_icon.setOnClickListener {
val dialog = Dialog(root.context,R.style.AlertDialogStyle)
dialog.setContentView(R.layout.icon_picker_layout)
val recyclerView = dialog.findViewById<RecyclerView>(R.id.icon_picker_recycler)
recyclerView.apply {
layoutManager = LinearLayoutManager(root.context,
RecyclerView.VERTICAL, false)
adapter = IconParentAdapter(IconParentData
.getParents())
}
dialog.show()
}
return root
}
}
Тогда родительский адаптер
class IconParentAdapter(private val parents : List<IconParentModel>) : RecyclerView.Adapter<IconParentAdapter.ViewHolder>(){
private val viewPool = RecyclerView.RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.parent_recycler,parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return parents.size
}
override fun onBindViewHolder(holder: ViewHolder,
position: Int) {
val parent = parents[position]
holder.textView.text = parent.title
val childLayoutManager = GridLayoutManager(holder.recyclerView.context, 4)
childLayoutManager.initialPrefetchItemCount = 4
holder.recyclerView.apply {
layoutManager = childLayoutManager
adapter = IconChildAdapter(parent.children)
setRecycledViewPool(viewPool)
}
}
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
val recyclerView : RecyclerView = itemView.rv_child
val textView: TextView = itemView.icon_title
}
}
И дочерний адаптер
class IconChildAdapter(private val children : List<IconChildModel>)
: RecyclerView.Adapter<IconChildAdapter.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.child_recycler,parent,false)
return ViewHolder(v)
}
override fun getItemCount() = children.size
override fun onBindViewHolder(holder: ViewHolder,
position: Int) {
val child = children[position]
holder.imageView.setImageResource(child.image)
holder.itemView.setOnClickListener {
//Send icon ID back to fragment
//and close Dialoge
}
}
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
val imageView: ImageView = itemView.child_imageView
}
}
Любая помощь будет оценена.