0

How to get number of items submitted to PagingDataAdapter

I have to display a RecyclerView with multiple viewType as Start, Middle and End RecyclerView.ViewHolder

Start and End ViewHolder is round where as inner items are using MiddleViewHolder

enter image description here

After debug I evaluate that when i submit 2 items to PagingDataAdapter itemCount is still 1.

How to check total item submit in PagingDataAdapter

StartViewHolder -> bindData(item,itemCount == 1)

RecentPagerAdapter.kt

const val VIEW_TYPE_FIRST = 0
const val VIEW_TYPE_MIDDLE = 1
private const val VIEW_TYPE_LAST = 2
class RecentPagerAdapter(val clicked:(recent:RecentBO) -> Unit) : PagingDataAdapter<RecentBO, RecyclerView.ViewHolder>(recentDiffUtil) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        getItem(position)?.let { item ->
            when (getItemViewType(position)) {
                VIEW_TYPE_FIRST -> (holder as StartViewHolder).bindData(item,itemCount == 1)
                VIEW_TYPE_MIDDLE -> (holder as RecentViewHolder).bindData(item)
                VIEW_TYPE_LAST -> (holder as EndViewHolder).bindData(item)
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> VIEW_TYPE_FIRST
            1 -> VIEW_TYPE_MIDDLE
            else -> VIEW_TYPE_LAST
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType) {
            VIEW_TYPE_FIRST -> StartViewHolder(ItemCurveStartBinding.inflate(LayoutInflater.from(parent.context),parent,false)){
                getItem(it)?.let { clicked.invoke(it) }
            }
            VIEW_TYPE_LAST -> EndViewHolder(ItemCurveEndBinding.inflate(LayoutInflater.from(parent.context),parent,false)){
                getItem(it)?.let { clicked.invoke(it) }
            }
            else -> RecentViewHolder(ItemRecentBinding.inflate(LayoutInflater.from(parent.context),parent,false)){
                getItem(it)?.let { clicked.invoke(it) }
            }
        }
    }

    companion object {
        private val recentDiffUtil = object : DiffUtil.ItemCallback<RecentBO>() {
            override fun areItemsTheSame(oldItem: RecentBO, newItem: RecentBO): Boolean {
                return oldItem.localPath == newItem.localPath && oldItem.imageUrl == newItem.imageUrl
            }

            override fun areContentsTheSame(oldItem: RecentBO, newItem: RecentBO): Boolean {
                return oldItem == newItem
            }
        }
    }
}

If there is only one item in list then all sides should be rounded.otherwise above image and last item should be rounded also.

StartViewHolder.kt

class StartViewHolder(val itemBinding: ItemCurveStartBinding,
                       val clicked:(pos:Int) -> Unit): RecyclerView.ViewHolder(itemBinding.root) ,
    View.OnClickListener{

    init {
        itemView.setOnClickListener(this)
    }
    fun bindData(recentBO: RecentBO,onlyOneItem:Boolean){
        itemBinding.item = recentBO
        val radius: Float = 30.0f//getResources().getDimension(R.dimen.default_corner_radius)

        if(onlyOneItem){
            itemBinding.ivPhoto.shapeAppearanceModel = itemBinding.ivPhoto.shapeAppearanceModel
                .toBuilder()
                .setTopLeftCorner(CornerFamily.ROUNDED,radius)
                .setBottomLeftCorner(CornerFamily.ROUNDED,radius)
                .setTopRightCorner(CornerFamily.ROUNDED,radius)
                .setBottomRightCorner(CornerFamily.ROUNDED,radius)
                .build();
        }else {
            itemBinding.ivPhoto.shapeAppearanceModel = itemBinding.ivPhoto.shapeAppearanceModel
                .toBuilder()
                .setTopLeftCorner(CornerFamily.ROUNDED,radius)
                .setBottomLeftCorner(CornerFamily.ROUNDED,radius)
                .build();
        }
    }

    override fun onClick(p0: View?) {
        clicked.invoke(absoluteAdapterPosition)
    }
}

I think something mistake in below code

(holder as StartViewHolder).bindData(item,itemCount == 1)

there are 2 items in RecyclerView but itemCount shows 1

What am i missing or doing wrong??

When I added an item to it First item remain fully rounded.

Code for initializing adapter

private val recentPagerAdapter by lazy { RecentPagerAdapter{
        openPreview(PhotoBO(it.localPath))
    }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mViewModel.getRecentPhotos().observe(viewLifecycleOwner)
    {
            recentPagerAdapter
             .submitData(viewLifecycleOwner.lifecycle,it)
            recentPagerAdapter.notifyDataSetChanged()
    }
}

Please See Edit itemCount and snapshot().items.size is 1 where as I have submitData 2 items to PagingDataAdapter

How to know total item submitted to PagingDataAdapter inside onBindViewHolder ?

Taimoor Khan
  • 541
  • 4
  • 20

0 Answers0