第一个是我的回购https://github.com/Berkayszk/FiWoApp
我正在使用reflect和分页适配器获取数据。当我想在MovieFragment中使用recyclerView显示数据时,我看到没有数据。这个错误的原因是什么?我已经处理这个问题3天了,我找不到解决方案。
电影片段=显示我的电影。
@AndroidEntryPoint
class MovieFragment : Fragment(R.layout.fragment_movie) {
private var _binding: FragmentMovieBinding? = null
private val binding get() = _binding!!
private val viewModel: MovieViewModel by viewModels()
private lateinit var movieAdapter: PopularMovieAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentMovieBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadingData()
setUpRv()
}
private fun loadingData() {
movieAdapter = PopularMovieAdapter()
lifecycleScope.launch {
viewModel.moviesList.collect { pagingData ->
movieAdapter.submitData(pagingData)
}
}
}
private fun setUpRv(){
movieAdapter = PopularMovieAdapter()
binding.recyclerView.apply {
layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
adapter = movieAdapter
setHasFixedSize(true)
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
PopularMovieAdapter=寻呼适配器
class PopularMovieAdapter : PagingDataAdapter<com.example.fiwoapp.model.popularmovie.Result,PopularMovieAdapter.PopularMovieHo lder>(
diffCallBack) {
class PopularMovieHolder(val binding : PopularMovieRowBinding) : ViewHolder(binding.root)
companion object {
val diffCallBack = object : DiffUtil.ItemCallback<com.example.fiwoapp.model.popularmovie.Result>(){
override fun areItemsTheSame(oldItem: com.example.fiwoapp.model.popularmovie.Result, newItem: com.example.fiwoapp.model.popularmovie.Result): Boolean {
return oldItem.id==newItem.id
}
override fun areContentsTheSame(oldItem: com.example.fiwoapp.model.popularmovie.Result, newItem: com.example.fiwoapp.model.popularmovie.Result): Boolean {
return oldItem == newItem
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PopularMovieHolder {
return PopularMovieHolder(PopularMovieRowBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}
override fun onBindViewHolder(holder: PopularMovieHolder, position: Int) {
val currentItem = getItem(position)
holder.binding.apply {
movieName.text = currentItem!!.title
val imageLink = "${Constants.IMAGE_BASE_UR}+${currentItem.poster_path}"
imageView.load(imageLink){
crossfade(true)
crossfade(100)
}
}
}
寻呼源
class PopularMovieSource(private val repository: MovieShowRepository) :PagingSource<Int,Result>(){
override suspend fun load(params: LoadParams<Int>): LoadResult<Int,Result> {
return try {
val currentPage = params.key ?: 1
val response = repository.getPopularMovie(currentPage)
val data = response.body()!!.results
val responseData = mutableListOf<Result>()
responseData.addAll(data)
LoadResult.Page(
data = responseData,
prevKey = if (currentPage == 1) null else -1,
nextKey = currentPage.plus(1)
)
} catch (e: Exception) {
LoadResult.Error(e)
} catch (exception: HttpException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, com.example.fiwoapp.model.popularmovie.Result>): Int? {
return null
}
我的视图模型
@HiltViewModel class MovieViewModel @Inject constructor(
private val repository: MovieShowRepository,
private val apiService: ApiService
public void run(){
val loading = MutableLiveData<Boolean>()
val moviesList = Pager(PagingConfig(1)) {
PopularMovieSource(repository)
}.flow.cachedIn(viewModelScope)
//Api
val detailsMovie = MutableLiveData<DetailsResponse>()
fun loadDetailsMovie(id: Int) = viewModelScope.launch {
loading.postValue(true)
val response = repository.getMovieDetails(id)
if (response.isSuccessful) {
detailsMovie.postValue(response.body())
}
loading.postValue(false)
}
我的回购
class MovieShowRepository @Inject constructor(val apiService : ApiService){
suspend fun getPopularMovie(page : Int) = apiService.getPopularMovie()
suspend fun getMovieDetails(id:Int) = apiService.getMovieDetails(id)
你可以在我的代码库中看到我所有的代码。当我的应用程序运行时,我不会得到任何错误,但它不会显示在任何收益中。当我检查不同的代码库时,我看到应用程序在与我相同的代码行的项目中工作,但它在我的项目中不工作,非常感谢你的帮助。
1条答案
按热度按时间zc0qhyus1#
错误:
正确:
在您的代码中,
loadingData()
函数在setUpRv()
函数之前被调用。因此,当调用setUpRv()
时,movieAdapter
将使用默认值而不是pagingData
的当前数据创建。