Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

android - Itemviewtype not working with recyclerview and paging library 2

I have an application where i'm using paging library 2 , the data shows without any problems , what i'm trying to implement now is the item viewtypes for data and progress , i'm setting the pagesize to limit of 10 items , i want the user to scroll down when he gets to the last item , i show progressbar then right after i load new chunk of data but i couldn't understand well how to handle this logic , this is what i have done so far , thank you for any help in advance

Logic should be this as following:

*Load 10 items *Load Progress *Load 10 items *Load progress and so on

 class PlayNowMoviesAdapter(private val context: Context, private var movieListener: MovieListener)
    : PagedListAdapter<MoviesDataModel, PlayNowMoviesAdapter.MyViewHolder>(callback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var viewHolder : MyViewHolder?  = null
        when (viewType) {
            ExtrasClass.ITEM_VIEW_TYPE_DATA -> {
                val movieBinding: MovieslayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
                        R.layout.movieslayout, parent, false)
                viewHolder = MyViewHolder(movieBinding)
                return viewHolder
            }
            ExtrasClass.ITEM_VIEW_TYPE_PROGRESS -> {
                val progressBinding : ProgresslayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
                        R.layout.progresslayout, parent, false)
                viewHolder = MyViewHolder(progressBinding)
                return viewHolder
            }
        }

        return viewHolder!!

    }

    class MyViewHolder : RecyclerView.ViewHolder {
        var movieBinding: MovieslayoutBinding? = null
         var progressBinding: ProgresslayoutBinding? = null

        constructor(binding: MovieslayoutBinding) : super(binding.root) {
            movieBinding = binding
        }

        constructor(binding: ProgresslayoutBinding) : super(binding.root) {
            progressBinding = binding
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val moviesModel = getItem(position)
         when(holder.itemViewType){
             ExtrasClass.ITEM_VIEW_TYPE_DATA -> {
                // holder.progressBinding?.progressBar?.visibility = View.INVISIBLE
                 holder.movieBinding?.model = moviesModel
                 holder.movieBinding?.movieRating?.numStars = moviesModel!!.vote_average.toDouble().toInt()
                 holder.movieBinding?.listener = movieListener
             }
             ExtrasClass.ITEM_VIEW_TYPE_PROGRESS -> {
                 holder.progressBinding?.progressBar?.visibility = View.VISIBLE
             }
         }
    }

    override fun getItemViewType(position: Int): Int {
        return if (getItem(position) != null ) {
            ExtrasClass.ITEM_VIEW_TYPE_DATA
        } else {
            ExtrasClass.ITEM_VIEW_TYPE_PROGRESS
        }
    }

    companion object {
        private val callback: DiffUtil.ItemCallback<MoviesDataModel> = object : DiffUtil.ItemCallback<MoviesDataModel>() {
            override fun areItemsTheSame(oldItem: MoviesDataModel, newItem: MoviesDataModel): Boolean {
                return oldItem.id == newItem.id
            }

            @SuppressLint("DiffUtilEquals")
            override fun areContentsTheSame(oldItem: MoviesDataModel, newItem: MoviesDataModel): Boolean 
              {
                return oldItem == newItem
            }
        }
    }
question from:https://stackoverflow.com/questions/65909597/itemviewtype-not-working-with-recyclerview-and-paging-library-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...