首先是我的repo:https://github.com/Berkayszk/FiWoApp
在我的应用程序中,我从API提取数据,并在Recyclerview中使用PagingData显示。我已经在Details片段中创建了一个类似的部分,我正在考虑通过调用类似的API来提取我的数据,但我看到我的数据没有加载,无论我如何努力尝试,我无法理解为什么没有发生这种情况。我试图将数据从ViewModel中的列表拉到适配器,但数据没有加载。
这是我的类似电影适配器
class SimilarMovieAdapter : PagingDataAdapter<Result,SimilarMovieAdapter.SimilarMovieViewHolder>(diffCallBack) {
class SimilarMovieViewHolder(val binding : PopularTvRowBinding): ViewHolder(binding.root)
companion object{
val diffCallBack = object : DiffUtil.ItemCallback<com.example.fiwoapp.model.similarmovie.Result>(){
override fun areItemsTheSame(oldItem: Result, newItem: Result): Boolean {
return oldItem.id==newItem.id
}
override fun areContentsTheSame(oldItem: Result, newItem: Result): Boolean {
return oldItem==newItem
}
}
}
override fun onBindViewHolder(holder: SimilarMovieViewHolder, position: Int) {
val currentItem = getItem(position)
holder.binding.apply {
val imageLinkBackground = "${Constants.IMAGE_BASE_UR}${currentItem!!.poster_path}"
imageViewPoster.load(imageLinkBackground){
crossfade(1000)
crossfade(true)
}
movieName.text = currentItem.title
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimilarMovieViewHolder {
return SimilarMovieViewHolder(PopularTvRowBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}
这是我的DetailsFragment,RecyclerView在DetailsFragment中。在这里,我从viewmodel获取数据,但是当我打开应用程序时,看不到任何数据。
@AndroidEntryPoint
class MovieDetailsFragment : Fragment() {
private var _binding : FragmentMovieDetailsBinding?=null
private val binding get() = _binding!!
private var movieId = 0
private val viewModel: MovieViewModel by viewModels()
private val args : MovieDetailsFragmentArgs by navArgs()
private lateinit var similarMovieAdapter : SimilarMovieAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentMovieDetailsBinding.inflate(layoutInflater,container,false)
return binding.root
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
movieId = args.movieId
if (movieId>0){
viewModel.loadDetailsMovie(movieId)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
similarMovieRv()
loadingSimilarData()
showData()
}
private fun loadingSimilarData(){
similarMovieAdapter = SimilarMovieAdapter()
lifecycleScope.launch {
viewModel.movieSimilarList.collect{pagingData->
similarMovieAdapter.submitData(pagingData)
}
}
}
private fun showDataSimilar(){
}
private fun showData(){
binding.apply {
viewModel.detailsMovie.observe(viewLifecycleOwner) {
val moviePosterUrlOrg = Constants.IMAGE_BASE_OR + it.backdrop_path
val moviePosterUrl = Constants.IMAGE_BASE_UR+it.poster_path
val studioPicture = Constants.IMAGE_BASE_UR+it.production_companies.get(0).logo_path
imageDetailOriginal.load(moviePosterUrlOrg){
crossfade(true)
crossfade(1000)
scale(Scale.FILL)
}
imageViewPoster.load(moviePosterUrl){
crossfade(1000)
crossfade(true)
scale(Scale.FILL)
}
studioImageView.load(studioPicture){
crossfade(true)
crossfade(1000)
scale(Scale.FILL)
}
tagLineText.text = it.tagline
movieName.text = it.title
releaseDate.text = it.release_date
releaseDateInfo.text = "${"Revenue: "+ it.revenue.toString()}"
studioName.text = it.production_companies.get(0).name
subjectText.text = it.overview
runTimeText.text = "${it.runtime.toString() + "min"}"
genreText.text = it.genres.get(0).name
countryText.text = it.production_countries.get(0).name
voteText.text = it.vote_average.toString()
voteTextAll.text = it.vote_count.toString()
}
}
}
private fun similarMovieRv(){
val similarMovieAdapter = SimilarMovieAdapter()
binding.similarMovieRv.apply {
layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
adapter = similarMovieAdapter
setHasFixedSize(true)
}
}
override fun onDestroy() {
super.onDestroy()
_binding=null
}
}
1条答案
按热度按时间cwtwac6a1#
数据未加载的原因是您将
adapter
的不同示例设置为recyclerview
,并将列表提交给不同示例!