How do I get the current ArrayList length after removing an element from it?
I have a fragment that contains a RecyclerView
. In the adapter I delete the respective element in the list in the onBindViewHolder()
by clicking on an icon – everything works wonderfully.
But after removing an element, I need the array length in my fragment.
How do I get this, what method do I need to call from the onbindViewHolder()
?
Adapter Code:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val cR = context.contentResolver
val type = cR.getType(listeImg[position])
if(type.toString().contains("mp4"))
{
val con = MediaController(context)
con.setAnchorView(holder.imageForList)
con.setMediaPlayer(holder.playing)
holder.playing.setOnPreparedListener {it->
it.setVolume(0F,0F)
it.isLooping = true
}
holder.playing.keepScreenOn = true
holder.playing.setVideoURI(listeImg[position])
holder.playing.start()
holder.playVideo.visibility = View.VISIBLE
holder.playing.visibility = View.VISIBLE
}
else
{
if(listeImg.size == 0)
{
holder.delete.visibility = View.INVISIBLE
}
else if(listeImg.size > 0)
{
holder.delete.visibility = View.VISIBLE
}
holder.playing.visibility = View.INVISIBLE
holder.playVideo.visibility = View.INVISIBLE
Glide.with(context).load(listeImg[position]).placeholder(R.drawable.no_image)
.skipMemoryCache(false)
.into(holder.imageForList)
}
holder.delete.setOnClickListener {
listeImg.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position,listeImg.size)
}
}
override fun getItemCount(): Int {
return listeImg.size
}
4
Answers
if you are trying to get the list size of your data which is added to recyclerView. you can create your list at the top of the fragment class, and initialize it with your list of where come from. then you can access this from all of your fragment.
also, you achieve it like this, create this method in your adopter.
then call it from your fragment. like ( yourAdopter.getListSize())
I think you should first call
linkeimg.remove(getAdapterPosition());
than call
notifyItemRemoved(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(),mDataSet.size());
try like this using higher-order function.
and in fragment you can do something like below
First you remove a item and notified the array list then down code is used