我正在尝试在我的项目中配置Android分页库,以将分页信息列表加载到RecyclerView中。由于我的API使用偏移量和最大值,所以我正在使用PositionalDataSource。
这是我的数据源实现,其中DataStore使用Retrofit来加载消息,我可以在控制台中看到消息正常加载,并转换为MessageListItem的实例:
class MessageDataSource: PositionalDataSource<MessageListItem>() {
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<MessageListItem>) {
DataStore.shared.loadMessages(params.startPosition, params.loadSize) { result, error ->
if(result != null) {
callback.onResult(result.items)
} else {
callback.onError(MessageDataSourceException(error))
}
}
}
override fun loadInitial(
params: LoadInitialParams,
callback: LoadInitialCallback<MessageListItem>
) {
DataStore.shared.loadMessages(params.requestedStartPosition, params.requestedLoadSize) { response, error ->
if(response != null) {
callback.onResult(response.items, response.offset, response.total)
} else {
callback.onError(MessageDataSourceException(error))
}
}
}
}
class MessageDataSourceException(rootCause: Throwable? = null): Exception(rootCause)
这是我的DataSourceFactory实现代码:
class MessageDataSourceFactory: DataSource.Factory<Int, MessageListItem>() {
val messageLiveDataSource = MutableLiveData<MessageDataSource>()
private lateinit var messageDataSource: MessageDataSource
override fun create(): DataSource<Int, MessageListItem> {
messageDataSource = MessageDataSource()
messageLiveDataSource.postValue(messageDataSource)
return messageDataSource
}
}
这是我的MessageListAdapter实现:
object MessageListItemDiff: DiffUtil.ItemCallback<MessageListItem>() {
override fun areItemsTheSame(oldItem: MessageListItem, newItem: MessageListItem): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: MessageListItem, newItem: MessageListItem): Boolean {
return oldItem == newItem
}
}
class MessageListAdapter(private val clickListener: View.OnClickListener):
PagedListAdapter<MessageListItem, MessageListAdapter.MessageHolder>(MessageListItemDiff) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageHolder {
val inflatedView = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
return MessageHolder(inflatedView, clickListener)
}
override fun onBindViewHolder(holder: MessageHolder, position: Int) {
holder.bind(getItem(position)!!)
}
class MessageHolder(itemView: View, private val clickListener: View.OnClickListener) : RecyclerView.ViewHolder(itemView) {
val unreadIndicator = itemView.findViewById<ImageView>(R.id.unreadIndicator)
val title = itemView.findViewById<TextView>(R.id.title)
val dateSent = itemView.findViewById<TextView>(R.id.dateSent)
val cardView = itemView.findViewById<CardView>(R.id.card_view)
fun bind(message: MessageListItem) {
cardView.tag = message
cardView.setOnClickListener(clickListener)
title.text = message.title
dateSent.text = TimeAgo.using(message.dateSent.time)
if(message.isRead) {
unreadIndicator.setImageResource(0)
} else {
unreadIndicator.setImageResource(R.drawable.ic_unread)
}
}
}
}
最后是我的ViewModel:
class MessageListViewModel: ViewModel() {
val messagePagedList: LiveData<PagedList<MessageListItem>>
val liveDataSource: LiveData<MessageDataSource>
init {
val messageDataSourceFactory = MessageDataSourceFactory()
liveDataSource = messageDataSourceFactory.messageLiveDataSource
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(30)
.setPrefetchDistance(90)
.build()
messagePagedList = LivePagedListBuilder(messageDataSourceFactory, pagedListConfig).build()
}
}
这里是在用于显示RecyclerView的碎片中的onViewCreated实现:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
messageList.layoutManager = LinearLayoutManager(context!!)
messageList.setHasFixedSize(true)
messageListViewModel = ViewModelProvider(this).get(MessageListViewModel::class.java)
messageListAdapter = MessageListAdapter(this)
messageListViewModel.messagePagedList.observe(this, Observer { messages ->
messageListAdapter.submitList(messages)
})
messageList.adapter = messageListAdapter
}
问题在于我可以看到从服务器加载数据,但它从未到达回收视图。如果在观察器行上添加断点(
messageListAdapter.submitList(messages)
),我将得到一次调用,其中包含一个空消息列表,然后就没有了。我必须承认,我对所有这些类及其应该执行的操作感到非常困惑,这是我在Android中进行的第一个分页实现,我不想使用Room数据库、RxJava或PageKeyedDataSource,因为大多数示例都这样做。
有任何想法可能出了什么问题吗?