Created
September 28, 2021 12:08
-
-
Save pankajjangid/a8eb52273b7b54fd0213193e918c15a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pankajjangid.meditqrando.base | |
| import android.view.LayoutInflater | |
| import android.view.ViewGroup | |
| import androidx.databinding.DataBindingUtil | |
| import androidx.databinding.ViewDataBinding | |
| import androidx.recyclerview.widget.LinearLayoutManager | |
| import androidx.recyclerview.widget.RecyclerView | |
| import androidx.recyclerview.widget.SimpleItemAnimator | |
| import androidx.viewbinding.ViewBinding | |
| import com.pankajjangid.meditqrando.databinding.RowItemEmptyViewBinding | |
| import com.pankajjangid.meditqrando.databinding.RowItemLoadingVerticalBinding | |
| import com.pankajjangid.meditqrando.networking.response.TimelineResponse | |
| import java.util.* | |
| abstract class BaseRVPaginationAdapter<T : Any, VB : ViewBinding>( | |
| private var listener: (T) -> Unit, | |
| private var dataList: ArrayList<T>, | |
| private var mRecyclerView: RecyclerView | |
| ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
| companion object { | |
| private const val VIEW_TYPE_ITEM = 0 | |
| private const val VIEW_TYPE_LOADING = 1 | |
| private const val EMPTY = 2 | |
| /** | |
| * View Holders | |
| */ | |
| class ItemVH<VB : ViewDataBinding>(val binding: VB) : | |
| RecyclerView.ViewHolder(binding.root) | |
| class LoadingViewHolder(val binding: RowItemLoadingVerticalBinding) : | |
| RecyclerView.ViewHolder(binding.root) | |
| class EmptyViewHolder(private val mBinding: RowItemEmptyViewBinding) : | |
| RecyclerView.ViewHolder(mBinding.root) | |
| } | |
| private var isLoading = false | |
| private val visibleThreshold = 5 | |
| private var lastVisibleItem = 0 | |
| private var totalItemCount = 0 | |
| abstract fun getLayout(): Int | |
| private var mOnLoadMoreListener: OnLoadMoreListener? = null | |
| fun setOnLoadMoreListener(mOnLoadMoreListener: OnLoadMoreListener?) { | |
| this.mOnLoadMoreListener = mOnLoadMoreListener | |
| } | |
| interface OnLoadMoreListener { | |
| fun onLoadMore() | |
| } | |
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
| return when (viewType) { | |
| VIEW_TYPE_ITEM -> { | |
| ItemVH( | |
| DataBindingUtil.inflate( | |
| LayoutInflater.from(parent.context), | |
| getLayout(), | |
| parent, | |
| false | |
| ) | |
| ) | |
| } | |
| VIEW_TYPE_LOADING -> { | |
| LoadingViewHolder( | |
| RowItemLoadingVerticalBinding.inflate( | |
| LayoutInflater.from(parent.context), | |
| parent, | |
| false | |
| ) | |
| ) | |
| } | |
| EMPTY -> { | |
| EmptyViewHolder( | |
| RowItemEmptyViewBinding.inflate( | |
| LayoutInflater.from(parent.context), | |
| parent, | |
| false | |
| ) | |
| ) | |
| } | |
| else -> EmptyViewHolder( | |
| RowItemEmptyViewBinding.inflate( | |
| LayoutInflater.from(parent.context), | |
| parent, | |
| false | |
| ) | |
| ) | |
| } | |
| } | |
| override fun getItemCount(): Int { | |
| return dataList.size | |
| } | |
| override fun getItemViewType(position: Int): Int { | |
| var id: Int = 0 | |
| /** | |
| * Set your response object here which you are using in you recycler view binding | |
| */ | |
| if (dataList[position] is TimelineResponse.Data) { | |
| id = (dataList[position] as TimelineResponse.Data).id | |
| } | |
| /* if (dataList[position] is NotificationResponse.Data.Notification) { | |
| id = (dataList[position] as NotificationResponse.Data.Notification).id | |
| } else if (dataList[position] is AllLanguagesResponse.Data.Language) { | |
| id = (dataList[position] as AllLanguagesResponse.Data.Language).id | |
| }else if (dataList[position] is ChatListResponse.Data.Notification) { | |
| id = (dataList[position] as ChatListResponse.Data.Notification).id | |
| }*/ | |
| return when { | |
| id != 0 -> { | |
| VIEW_TYPE_ITEM | |
| } | |
| id == 0 -> { | |
| VIEW_TYPE_LOADING | |
| } | |
| else -> { | |
| EMPTY | |
| } | |
| } | |
| } | |
| fun setLoaded(newResponseItems: List<T>) { | |
| val lastPosition=dataList.size - 1 | |
| dataList.removeAt(lastPosition) | |
| notifyItemRemoved(dataList.size) | |
| dataList.addAll(newResponseItems) | |
| //removeBlinkAnimationWhenNewItemAdded() | |
| notifyDataSetChanged() | |
| notifyItemRangeChanged(lastPosition, dataList.size); | |
| isLoading = false | |
| } | |
| fun removeBlinkAnimationWhenNewItemAdded(){ | |
| (mRecyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false | |
| } | |
| fun setLoaded() { | |
| isLoading = false | |
| } | |
| init { | |
| val linearLayoutManager = mRecyclerView.layoutManager as LinearLayoutManager? | |
| mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { | |
| override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
| super.onScrolled(recyclerView, dx, dy) | |
| totalItemCount = linearLayoutManager?.itemCount!! | |
| lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition() | |
| if (!isLoading && totalItemCount <= lastVisibleItem + visibleThreshold) { | |
| if (mOnLoadMoreListener != null) { | |
| mOnLoadMoreListener?.onLoadMore() | |
| } | |
| isLoading = true | |
| } | |
| } | |
| }) | |
| } | |
| /* class ItemVH(val mBinding: RowMovieItemBinding) : | |
| RecyclerView.ViewHolder(mBinding.root) { | |
| fun onBind(item: T, postion: Int) { | |
| mBinding.apply { | |
| // mBinding.callback=callbackListner | |
| try { | |
| setIsRecyclable(false) | |
| mBinding.movie=movie | |
| itemView.rootView.setOnClickListener { | |
| listener(item | |
| ) | |
| } | |
| } catch (e: Exception) { | |
| e.printStackTrace() | |
| } | |
| executePendingBindings() | |
| } | |
| } | |
| } | |
| class LoadingViewHolder(private val mBinding: LoadingVerticalBinding) : | |
| RecyclerView.ViewHolder(mBinding.root) { | |
| fun onBind() { | |
| mBinding.apply { | |
| executePendingBindings() | |
| } | |
| } | |
| } | |
| class EmptyViewHolder(private val mBinding: EmptyViewBinding) : | |
| RecyclerView.ViewHolder(mBinding.root) { | |
| fun onBind() { | |
| mBinding.apply { | |
| executePendingBindings() | |
| } | |
| } | |
| } | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment