【问题标题】:Why my PagingSource doesn't give me any data?为什么我的 PagingSource 没有给我任何数据?
【发布时间】:2021-07-23 02:40:26
【问题描述】:

项目中不起作用的元素

我检查数据是否不为空,并在片段中执行默认提交列表。

顺便说一句,这里是文档的链接

搜索分页来源

这些日志甚至没有显示

    class SearchPagingSource(
    private val api: Api,
    private val query: String
) : PagingSource<Int, Image>
    () {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Image> {
        val position = params.key ?: 0

        Log.d("SUPERTAG", "response")
        return try {
            val response =
                api.search(query, position, params.loadSize, Locale.getDefault().language)
            val list = ArrayList<Image>()
            response.gif.forEach {
                list.add(it.image)
            }
            Log.d("SUPERTAG", "response: $list")
            LoadResult.Page(
                data = list,
                prevKey = null,
                nextKey = if (list.isEmpty()) null else position + 15
            )
        } catch (e: IOException) {
            // no connection
            Log.d("SUPERTAG", "IOException: ${e.message}")
            LoadResult.Error(e)
        } catch (e: HttpException) {
            // error loading
            Log.d("SUPERTAG", "HttpException: ${e.message}")
            LoadResult.Error(e)
        }
    }
}

视图模型

Null 因为存储库返回的 null。

class SearchViewModel : ViewModel() {
    private val searchRepository = SearchRepository.getInstance()

    private val _query = MutableLiveData<String>()

    private val _results = _query.map { data ->
        searchRepository.search(data).value
    }

    val results = _results

    private val _error = MutableLiveData<String>()
    val error: LiveData<String> = _error

    @SuppressLint("StaticFieldLeak")
    private lateinit var progressBar: ProgressBar

    fun initProgress(progress: ProgressBar) {
        progressBar = progress
    }

    fun search(query: String, errorLoading: String) {
        viewModelScope.launch {
            try {
                progressBar.visibility = View.VISIBLE
                _query.value = query
                Log.d("SUPERTAG", "result2: ${searchRepository.search(_query.value!!).value}")
                progressBar.visibility = View.GONE
            } catch (e: Exception) {
                _error.value = e.message
            }
        }
    }
}

存储库 正是这部分代码返回null,我通过日志检查了它。我想我在参数或一般情况下做错了。

object SearchRepository {
    private lateinit var instance: SearchRepository
    private val app: App by lazy {
        App().getInstance()
    }

    fun getInstance(): SearchRepository {
        instance = this
        app.initRetrofit()
        return instance
    }

    fun search(query: String) = Pager(
            config = PagingConfig(
                15,
                maxSize = 50,
                enablePlaceholders = false
            ),
            pagingSourceFactory = {
                SearchPagingSource(app.api, query)
            }
        ).liveData

}

如果我这样做了,我至少会收到小吃栏和错误消息。通常它什么也不显示,甚至没有进度条。 所以,如果我添加 jumpThreshold = 0,我会得到一个带有我通常没有的错误的小吃吧。

fun search(query: String) = Pager(
            config = PagingConfig(
                pageSize = 15,
                jumpThreshold = 0
            ),
            pagingSourceFactory = {
                SearchPagingSource(app.api, query)
            }
        ).liveData

编辑

所以,我使用 flow 完成了它,它工作了一点,但我仍然没有在我的回收站中得到一个列表。

存储库

fun getListData(query: String): Flow<PagingData<Image>> {
        return Pager(
            config = PagingConfig(
                pageSize = 15,
                maxSize = 50,
                enablePlaceholders = true
            ), pagingSourceFactory = {
                SearchPagingSource(query = query, api = app.api)
            }).flow
    }

视图模型

fun search(query: String){
            viewModelScope.launch {
                try {
                    searchRepository.getListData(query).collectLatest {
                        it.map {
                            Log.d("SUPERTAG", "image $it")
                        }
                        Log.d("SUPERTAG", it.toString())
                        _results.value = it
                    }
                } catch (e: Exception){
                    _error.value = e.message
                }
            }
    }

【问题讨论】:

  • 请包括所有相关信息 here ,关于 SO 而不是指向您的代码的链接。这些链接可以随时删除或更改。还要确保将它们包含为实际文本而不是图像
  • @a_local_nobody

标签: android pagination android-pageradapter android-paging android-paging-library


【解决方案1】:

答案在适配器中!

    class SearchAdapter(
    diffCallback: DiffUtil.ItemCallback<Image>,
    private val clickListener: (url: String) -> Unit
) : PagingDataAdapter<Image, SearchAdapterViewHolder>(diffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchAdapterViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.list_item, parent, false)
        return SearchAdapterViewHolder(view)
    }

    override fun onBindViewHolder(holder: SearchAdapterViewHolder, position: Int) {
        val item = getItem(position)

        if(item != null){
        holder.imageView.setOnClickListener {
            clickListener(item.original.url)
        }
        holder.bind(item)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 2019-08-13
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多