Я получаю свои данные с помощью модификации и адаптера подкачки. Когда я хочу показать свои данные с помощью recyclerView в MovieFragment, я вижу, что данные не поступают. В чем причина этой ошибки? Я боролся с этой проблемой уже 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
}
Код: Выделить всё
class PopularMovieAdapter : PagingDataAdapter(
diffCallBack) {
class PopularMovieHolder(val binding : PopularMovieRowBinding) : ViewHolder(binding.root)
companion object {
val diffCallBack = object : DiffUtil.ItemCallback(){
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(){
override suspend fun load(params: LoadParams): LoadResult {
return try {
val currentPage = params.key ?: 1
val response = repository.getPopularMovie(currentPage)
val data = response.body()!!.results
val responseData = mutableListOf()
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? {
return null
}
Код: Выделить всё
@HiltViewModel class MovieViewModel @Inject constructor(
private val repository: MovieShowRepository,
private val apiService: ApiService
Код: Выделить всё
val loading = MutableLiveData()
val moviesList = Pager(PagingConfig(1)) {
PopularMovieSource(repository)
}.flow.cachedIn(viewModelScope)
//Api
val detailsMovie = MutableLiveData()
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)
Подробнее здесь: https://stackoverflow.com/questions/759 ... in-android
Мобильная версия