If you looking for the simplest answer here it is. This solution works in 100%. I search whole stackoverflow and internet for solution, but unfortunately most of them dont work for me. In this solution you just put one object in spinner, this object have RecycleView layout and then you attach everything to recycleView as you attach to spinner
At the beginning you have to create layout with RecycleView
spinner_recycleview.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintHeight_max="300dp" />
Then change your layout in custom adapter to new that you created before.
class NotificationSpinnerAdapter(
notificationList: List<Notification>,
var context: Context,
var homeNotificationAdapterInterface: HomeNotificationAdapterInterface
) : BaseAdapter() {
private val notificationList = notificationList
override fun getCount(): Int {
return 1
}
override fun getItem(p0: Int): Any {
return 1
}
override fun getItemId(p0: Int): Long {
return 1
}
@SuppressLint("ViewHolder")
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val inflter = (LayoutInflater.from(context));
val view = SpinnerRecycleviewBinding.inflate(inflter) // here I using buildFeatures -> dataBinding true in gradle
if (notificationList.isEmpty()){
val newList = notificationList.toMutableList()
newList.add(0, Notification(0,"POW","","","","Brak powiadomień.","","","T"))
view.recyclerView.adapter = NotificationRvAdapter(newList,context,homeNotificationAdapterInterface)
}else
view.recyclerView.adapter = NotificationRvAdapter(notificationList,context,homeNotificationAdapterInterface)
view.recyclerView.layoutManager = LinearLayoutManager(context)
return view.root
}
}
And the last step is create adapter for recycleview and attach layout that you used before change in spinner.
lass NotificationRvAdapter(list: List<Notification>,private val context: Context,private val homeNotificationAdapterInterface: HomeNotificationAdapterInterface): RecyclerView.Adapter<NotificationRvAdapterViewHolder>(),innerNotificationInterface {
private var notificationList = list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotificationRvAdapterViewHolder =
NotificationRvAdapterViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context), R.layout.card_spinner_notification, parent, false
)
)
override fun onBindViewHolder(holder: NotificationRvAdapterViewHolder, position: Int) {
val item = notificationList[position]
holder.bind(item,context,this)
}
override fun getItemCount(): Int {
return notificationList.size
}
@SuppressLint("NotifyDataSetChanged")
override fun onUpdate(notification: Notification) {
val newList = notificationList.toMutableList()
val updateElement = notification
var index = 0
updateElement.isRead = "T"
index = newList.indexOf(notification)
newList.remove(notification)
newList.add(index,updateElement)
notificationList = newList
notifyDataSetChanged()
homeNotificationAdapterInterface.refreshList(newList)
}
}
class NotificationRvAdapterViewHolder(binding: CardSpinnerNotificationBinding) : RecyclerView.ViewHolder(binding.root){
private val title = binding.titleText
private val body = binding.bodyNotification
private val message = binding.messageText
private val date = binding.dateText
private val iconSeen = binding.imageSeen
@SuppressLint("SetTextI18n", "ResourceAsColor")
fun bind(elem: Notification, context: Context,innerNotificationInterface: innerNotificationInterface){
title.text = elem.title
message.text = elem.message
date.text = elem.data
if(elem.isRead.equals("T")){
iconSeen.gone()
}else{
iconSeen.show()
}
body.setOnClickListener {
innerNotificationInterface.onUpdate(elem)
}
}
}
interface innerNotificationInterface{
fun onUpdate(notification: Notification)
}
interface HomeNotificationAdapterInterface{
fun refreshList(newList: MutableList<Notification>)
}