gpt4 book ai didi

具有数据绑定(bind)的 Android 分页库

转载 作者:行者123 更新时间:2023-11-29 23:01:53 25 4
gpt4 key购买 nike

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

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

}

View 模型:

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" />

绑定(bind)适配器

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

最佳答案

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

https://github.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide

关于具有数据绑定(bind)的 Android 分页库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com