带有数据绑定的Android分页库

问题描述 投票:0回答:1

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

Github 上的项目: https://github.com/DoomMortal/paging

有人可以调查一下吗? 任何帮助将不胜感激。

我的适配器:

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)
    }

}

视图模型:

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)
    }
}

数据来源:

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 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)
}
android kotlin android-databinding android-architecture-components android-paging-library
1个回答
1
投票

以防万一有另一种解决方案,我使用了这个示例并工作

https://github.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide

© www.soinside.com 2019 - 2024. All rights reserved.