带数据绑定的Android分页库

带数据绑定的Android分页库,android,kotlin,paging,android-databinding,android-architecture-components,Android,Kotlin,Paging,Android Databinding,Android Architecture Components,我尝试用数据绑定实现Android分页库。 我无法利用现有的公共资源来实施它。 可以将分页库与数据绑定和绑定适配器一起使用吗 Github上的项目: 有人能调查一下吗? 任何帮助都将不胜感激 我的适配器: class OverviewAdapter(val onClickListener: OnClickListener) : PagedListAdapter<VideoProperty, OverviewAdapter.ViewHolder>(DiffCallback) {

我尝试用数据绑定实现Android分页库。 我无法利用现有的公共资源来实施它。 可以将分页库与数据绑定和绑定适配器一起使用吗

Github上的项目:

有人能调查一下吗? 任何帮助都将不胜感激

我的适配器:

class OverviewAdapter(val onClickListener: OnClickListener) : PagedListAdapter<VideoProperty, OverviewAdapter.ViewHolder>(DiffCallback) {

    class ViewHolder private constructor(private var binding: OverviewItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(clickListener: OnClickListener, videoProperty: VideoProperty){
            binding.property = videoProperty
            binding.clickListener = clickListener
            binding.executePendingBindings()
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val binding = OverviewItemBinding.inflate(layoutInflater, parent, false)

                return ViewHolder(binding)
            }
        }

    }

    companion object DiffCallback : DiffUtil.ItemCallback<VideoProperty>() {
        override fun areItemsTheSame(oldItem: VideoProperty, newItem: VideoProperty): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: VideoProperty, newItem: VideoProperty): Boolean {
            return oldItem.videoTableId == newItem.videoTableId
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder.from(parent)
    }

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

        when (holder) {
            is ViewHolder -> {
                val nightItem = getItem(position) as VideoProperty
                holder.bind(onClickListener, nightItem)
            }
        }

    }

    class OnClickListener(val clicklistener: (videoProperty: VideoProperty) -> Unit) {
        fun onClick(videoProperty: VideoProperty) = clicklistener(videoProperty)
    }

}
class OverviewAdapter(val-onClickListener:onClickListener):PagedListAdapter(DiffCallback){
类ViewHolder私有构造函数(私有变量绑定:OverviewItemBinding):RecyclerView.ViewHolder(binding.root){
趣味绑定(clickListener:OnClickListener,videoProperty:videoProperty){
binding.property=videoProperty
binding.clickListener=clickListener
binding.executePendingBindings()
}
伴星{
乐趣来自(父:视图组):视图持有者{
val layoutInflater=layoutInflater.from(parent.context)
val binding=概览项绑定。充气(布局更平坦,父项,false)
返回视图保持器(绑定)
}
}
}
伴生对象DiffCallback:DiffUtil.ItemCallback(){
覆盖项相同(旧项:VideoProperty,新项:VideoProperty):布尔值{
返回oldItem==newItem
}
覆盖相同的内容(旧项:VideoProperty,新项:VideoProperty):布尔值{
返回oldItem.videoTableId==newItem.videoTableId
}
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
返回ViewHolder.from(父级)
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
何时(持有人){
是ViewHolder->{
val nightItem=getItem(位置)作为VideoProperty
holder.bind(onClickListener,nightItem)
}
}
}
类OnClickListener(val clicklistener:(videoProperty:videoProperty)->Unit){
fun onClick(videoProperty:videoProperty)=clicklistener(videoProperty)
}
}
视图模型:

enum class MarsApiStatus { LOADING, ERROR, DONE }

class OverviewViewModel : ViewModel() {

    private val TAG = "OverviewViewModel"

    private var executor: Executor? = null

    // The internal MutableLiveData String that stores the status of the most recent request
    private val _status = MutableLiveData<MarsApiStatus>()

    // The external immutable LiveData for the request status String
    val status: LiveData<MarsApiStatus>
        get() = _status

    private val _properties = MutableLiveData<VideosOverview>()

    val properties: LiveData<VideosOverview>
        get() = _properties

    private val _navigateToSelectedProperty = MutableLiveData<VideoProperty>()
    val navigateToSelectedProperty: LiveData<VideoProperty>
        get() = _navigateToSelectedProperty

    private var viewModelJob = Job()
    private val coroutineScope =  CoroutineScope(viewModelJob + Dispatchers.Main)

    //val itemPagedList: LiveData<PagedList<VideosOverview>>

    var itemPagedList: LiveData<PagedList<VideosOverview>>
    var liveDataSource: LiveData<PageKeyedDataSource<Int, VideosOverview>>? = null

    /**
     * Call getMarsRealEstateProperties() on init so we can display status immediately.
     */
    init {
        //getMarsRealEstateProperties(VideosOverviewApiFilter.SHOW_POOP)

        executor = Executors.newFixedThreadPool(5)

        val feedDataFactory = ItemDataSourceFactory()

        val pagedListConfig = PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPageSize(ItemVideosDataSource.PAGE_SIZE)
                .build()

        /*_properties.value = LivePagedListBuilder(feedDataFactory, pagedListConfig)
                .build()*/


        liveDataSource = feedDataFactory.getItemLiveDataSource()
        itemPagedList = LivePagedListBuilder(feedDataFactory, pagedListConfig).build()

    }

    /**
     * Sets the value of the status LiveData to the VideosOverview API status.
     */
    private fun getMarsRealEstateProperties(filter: VideosOverviewApiFilter, page: Long) {

        coroutineScope.launch {
            //val getPropertiesDeferred =
                try{
                    _status.value = MarsApiStatus.LOADING
                    val listResult = VideosOverviewApi.retrofitService.getProperties(filter.value, page)

                    Log.d("CONTENT: ", filter.value.toString())
                    Log.d("CONTENT: ", page.toString())
                    Log.d("CONTENT: ", listResult.items[0].toString())

                    _status.value = MarsApiStatus.DONE
                    if (listResult.items.size > 0) {
                        _properties.value = listResult
                    }
                } catch (e: Throwable){
                    Log.d(TAG, e.toString())
                    _status.value = MarsApiStatus.ERROR
                    //_properties.value = ArrayList() OLD
                }

        }

    }

    override fun onCleared() {
        super.onCleared()
        viewModelJob.cancel()
    }

    fun displayPropertyDetails(marsProperty: VideoProperty) {
        _navigateToSelectedProperty.value = marsProperty
    }

    fun displayPropertyDetailsComplete() {
        _navigateToSelectedProperty.value = null
    }

    fun updateFilter(filter: VideosOverviewApiFilter, page: Long){
        getMarsRealEstateProperties(filter, page)
    }
}
enum类MarsApiStatus{LOADING,ERROR,DONE}
类OverviewModel:ViewModel(){
private val TAG=“概览模型”
私有变量执行器:执行器?=null
//存储最新请求状态的内部MutableLiveData字符串
private val_status=MutableLiveData()
//请求状态字符串的外部不可变LiveData
val状态:LiveData
获取()
private val_properties=MutableLiveData()
val属性:LiveData
get()=\u属性
private val_navigateToSelectedProperty=MutableLiveData()
val navigateToSelectedProperty:LiveData
get()=\u navigateToSelectedProperty
私有变量viewModelJob=Job()
private val coroutineScope=coroutineScope(viewModelJob+Dispatchers.Main)
//val itemPagedList:LiveData
var itemPagedList:LiveData
变量liveDataSource:LiveData?=null
/**
*在init上调用getMarsReleaseTateProperties(),以便立即显示状态。
*/
初始化{
//GetMarsReleaseStateProperties(VideoSoveriveWapiFilter.SHOW\u POOP)
executor=Executors.newFixedThreadPool(5)
val feedDataFactory=ItemDataSourceFactory()
val pagedListConfig=PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(ItemVideosDataSource.PAGE_大小)
.build()
/*_properties.value=LivePagedListBuilder(feedDataFactory,pagedListConfig)
.build()*/
liveDataSource=feedDataFactory.getItemLiveDataSource()
itemPagedList=LivePagedListBuilder(feedDataFactory,pagedListConfig).build()
}
/**
*将状态LiveData的值设置为VideosOverview API状态。
*/
private fun GetMarsReleaseTateProperties(过滤器:VideoSoveriveWapiFilter,页面:长){
协同观测发射{
//val GetProperties延迟=
试一试{
_status.value=MarsApiStatus.LOADING
val listResult=VideosOverviewApi.reformationservice.getProperties(filter.value,第页)
Log.d(“内容:”,filter.value.toString())
Log.d(“内容:,page.toString())
Log.d(“内容:”,listResult.items[0].toString())
_status.value=MarsApiStatus.DONE
如果(listResult.items.size>0){
_properties.value=listResult
}
}捕获(e:可丢弃){
Log.d(标记,例如toString())
_status.value=MarsApiStatus.ERROR
//_properties.value=ArrayList()旧
}
}
}
覆盖有趣的onCleared(){
super.onCleared()
viewModelJob.cancel()
}
有趣的displayPropertyDetails(marsProperty:VideoProperty){
_navigateToSelectedProperty.value=marsProperty
}
fun displayPropertyDetailsComplete(){
_navigateToSelectedProperty.value=null
}
fun updateFilter(过滤器:VideoSoveriveWapiFilter,页面:长){
GetMarsReleaseTateProperties(筛选器,第页)
}
}
数据源:

class ItemVideosDataSource : PageKeyedDataSource<Long, VideosOverview>() {

    companion object {
        val FIRST_PAGE: Long = 1
        val PAGE_SIZE: Int = 50
    }

    private val TAG = "ItemVideosDataSource"

    private var viewModelJob = Job()
    private val coroutineScope =  CoroutineScope(viewModelJob + Dispatchers.Main)

    override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Long, VideosOverview>) {
        //videos.retrofitService.getProperties(FIRST_PAGE)

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {
                    callback.onResult(lest, null, FIRST_PAGE + 1)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }

    }

    override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<Long, VideosOverview>) {

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {

                    val key: Long? = if(params.key > 1) params.key - 1 else null

                    callback.onResult(lest, key)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }
    }

    override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<Long, VideosOverview>) {

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {

                    val key: Long? = if(params.key > 1) params.key + 1 else null

                    callback.onResult(lest, key)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }

    }

}
class ItemVideosDataSource:PageKeyedDataSource(){
伴星{
val第一页:长=1
val页面大小:Int=50
}
private val TAG=“ItemVideosDataSource”
私有变量viewModelJob=Job()
private val coroutineScope=coroutineScope(viewModelJob+Dispatchers.Main)
重写fun loadInitial(参数:LoadInitialParams,回调:LoadInitialCallback){
//videos.RefughtService.getProperties(第一页)
协同观测发射{
//val GetProperties延迟=
试一试{
val listResult=VideosOverviewApi.reformationservice.getProperties(1,第一页)
瓦莱斯特
class ItemDataSourceFactory : DataSource.Factory<Long, VideosOverview>() {

    //val sourceLiveData = MutableLiveData<ItemVideosDataSource>()
    private val itemLiveDataSource = MutableLiveData<PageKeyedDataSource<Int, VideosOverview>>()

    override fun create(): DataSource<Long, VideosOverview> {

        //getting our data source object
        val itemDataSource = ItemVideosDataSource()

        //posting the datasource to get the values
        //itemLiveDataSource.postValue(itemDataSource)

        //returning the datasource
        return itemDataSource

    }

    fun getItemLiveDataSource(): MutableLiveData<PageKeyedDataSource<Int, VideosOverview>> {
        return itemLiveDataSource
    }

}
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_overview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:clipToPadding="false"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:listData="@{viewModel.itemPagedList}"
    tools:listitem="@layout/overview_item" />
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: PagedList<VideoProperty>?){
//fun bindRecyclerView(recyclerView: RecyclerView, data: List<VideoProperty>?){
    val adapter = recyclerView.adapter as OverviewAdapter
    adapter.submitList(data)
}