数据未显示在回收站视图中

时间:2021-07-24 12:18:39

标签: android xml kotlin android-recyclerview

我目前正在学习 Android 开发,并且正在构建一个显示来自 OMDB api 的电影的应用程序。但是,在运行应用程序时,recyclerview 不显示任何数据。我什至尝试对数据进行硬编码,但它仍然没有显示任何内容。我感谢任何形式的帮助。谢谢。

这是文件。

movie_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="30dp"
android:layout_marginTop="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="false">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="208dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="@id/movie_image"
            app:layout_constraintStart_toEndOf="@id/movie_image"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">

            <TextView
                android:id="@+id/movie_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp" />


            <TextView
                android:id="@+id/movie_year"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp">

            </TextView>

            <TextView
                android:id="@+id/movie_rating"
                android:layout_width="match_parent"
                android:layout_height="51dp"
                android:paddingTop="10dp" />


        </LinearLayout>

        <ImageView
            android:id="@+id/movie_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
             />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

fragment_movies_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MoviesListFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/movie_layout" />



</RelativeLayout>

电影列表片段.kt

package com.example.moviesapp

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.moviesapp.databinding.FragmentMoviesListBinding
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MoviesListFragment : Fragment(R.layout.fragment_movies_list) {

    private val viewModel by viewModels<MoviesListViewModel>()

    private var _binding: FragmentMoviesListBinding? = null
    private val binding get() = _binding!!


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //View is inflated layout

        _binding = FragmentMoviesListBinding.bind(view)

        val adapter = MoviesListAdapter()

        binding.apply {
            recyclerView.layoutManager = LinearLayoutManager(requireContext())
            recyclerView.setHasFixedSize(true)
            recyclerView.adapter = adapter
        }
        //Observe the movies livedata
        //Use viewLifecycleOwner instead of this because the UI should stop being updated when the fragment view is destroyed
        viewModel.movies.observe(viewLifecycleOwner) {
            //This is the lifecycle of the view of the fragment, not an instance of a fragment
            //It is the paging data itself
            adapter.submitData(viewLifecycleOwner.lifecycle, it)

        }
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}


MoviesListViewModel.kt

package com.example.moviesapp

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.example.moviesapp.network.MoviesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class MoviesListViewModel @Inject constructor(private val repository: MoviesRepository): ViewModel() {
    //const of type mutable live data to observe changes for the query
    private val currentQuery = MutableLiveData(DEFAULT_QUERY)

    //results from search requests
    //The switchMap takes a lambda parameter that will be executed when the value of currentQuery changes
    //We get passed a parameter that has the new value of currentQuery
    val movies = currentQuery.switchMap { queryString ->
    repository.getSearchResults(queryString).cachedIn(viewModelScope)//Use viewModelScope to cache in the livedata
    }

    //This function will be called from the fragment when something is typed into the search field
    fun searchMovies(movieTitle: String) {
        currentQuery.value = movieTitle
    }

    companion object {
        //Creating a default value
        private const val DEFAULT_QUERY = "Joker"
    }




}


电影列表适配器.kt


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.example.moviesapp.databinding.MovieLayoutBinding
import com.example.moviesapp.network.Movies

class MoviesListAdapter : PagingDataAdapter<Movies, MoviesListAdapter.MoviesListViewHolder>(
    MOVIE_COMPARATOR
) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MoviesListViewHolder {
        val binding = MovieLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)

        return MoviesListViewHolder(binding)
    }


    override fun onBindViewHolder(holder: MoviesListViewHolder, position: Int) {
        val currentItem = getItem(position)

        if (currentItem != null) {
            holder.bind(currentItem)
        }

    }


    class MoviesListViewHolder(private val binding: MovieLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(movie: Movies) {
            binding.apply {
            movieTitle.text = movie.title
            movieYear.text = movie.year
            movieRating.text = movie.rating
                Glide.with(itemView)
                    .load(movie.imageUrl)
                    .centerCrop()
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(R.drawable.ic_baseline_error_outline_24)
                    .into(movieImage)

            }
        }

    }

    companion object {
        private val MOVIE_COMPARATOR = object : DiffUtil.ItemCallback<Movies>() {
            override fun areItemsTheSame(oldItem: Movies, newItem: Movies) =
                oldItem.id == newItem.id


            override fun areContentsTheSame(oldItem: Movies, newItem: Movies) =
                oldItem == newItem



        }

    }
}


电影.kt

package com.example.moviesapp.network

import android.os.Parcelable
import com.squareup.moshi.Json
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Movies(
    @Json(name= "Title") val title: String,
    @Json(name="Year") val year: String,
    @Json(name="Plot") val plot: String,
    @Json(name="imdbRating") val rating: String,
    @Json(name="imdbID") val id: String,
    @Json(name="Actors") val cast: String,
    @Json(name="Writer") val writers: String,
    @Json(name="Director") val director: String,
    @Json(name="Poster") val imageUrl: String,
    ): Parcelable {}

MoviesApi.kt

package com.example.moviesapp.network

import retrofit2.http.GET
import retrofit2.http.Query


const val OMDB_API_KEY="[mykey]"
interface MoviesApi {


    companion object {


        const val BASE_URL = "http://www.omdbapi.com/"
    }

    @GET("/")
    suspend fun getMovies(
        @Query("t") movieTitle: String,
        @Query("page") page: Int,
        @Query("type") type: String,
        @Query("apikey") key: String = OMDB_API_KEY
    ): MoviesResponse


MoviesRepository.kt

package com.example.moviesapp.network

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.liveData
import javax.inject.Inject
import javax.inject.Singleton


@Singleton
//We use Inject because I own this class, unlike the Retrofit and MoviesApi class
class MoviesRepository @Inject constructor(private val moviesApi: MoviesApi) {
    //This function will be called later on in the ViewModel
fun getSearchResults(movieTitle: String) =
    Pager(
        config = PagingConfig(
            pageSize = 10,
            //Value at which we want to start dropping items
            maxSize = 50,
            //Disabling placeholders for objects that haven't been loaded yet
            enablePlaceholders = false
        ),
        pagingSourceFactory = {MoviesPagingSource(moviesApi, movieTitle)}
    //Turn this pager into a stream of paging data to get live updates
    ).liveData

}

MoviesPagingSource.kt

package com.example.moviesapp.network

import androidx.paging.PagingSource
import androidx.paging.PagingState

//Declare the const outside of class because it is not related to the class
private const val MOVIES_STARTING_PAGE_INDEX = 1


class MoviesPagingSource(
    private val moviesApi: MoviesApi,
    private val movieTitle: String
): PagingSource<Int, Movies>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Movies> {
       return try {
            val position = params.key ?: MOVIES_STARTING_PAGE_INDEX
            val response = moviesApi.getMovies(movieTitle, position, "movie")
             LoadResult.Page(
                //Data you want to load
                data = response.results,
                //Calculate the number of the previous and next page
                prevKey = if (position == MOVIES_STARTING_PAGE_INDEX) null else position - 1,
                nextKey = if (response.results.isEmpty()) null else position + 1

            )
        } catch (exception: Exception) {
             LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, Movies>): Int? {
        //Used for subsequent refresh calls to PagingSource.load()
        return state.anchorPosition?.let { anchorPosition ->
            state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
        }


    }

}

电影响应.kt

package com.example.moviesapp.network

data class MoviesResponse(
    val results: List<Movies>
)


MoviesRepository.kt

package com.example.moviesapp.network

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.liveData
import javax.inject.Inject
import javax.inject.Singleton


@Singleton
//We use Inject because I own this class, unlike the Retrofit and MoviesApi class
class MoviesRepository @Inject constructor(private val moviesApi: MoviesApi) {
    //This function will be called later on in the ViewModel
fun getSearchResults(movieTitle: String) =
    Pager(
        config = PagingConfig(
            pageSize = 10,
            //Value at which we want to start dropping items
            maxSize = 50,
            //Disabling placeholders for objects that haven't been loaded yet
            enablePlaceholders = false
        ),
        pagingSourceFactory = {MoviesPagingSource(moviesApi, movieTitle)}
    //Turn this pager into a stream of paging data to get live updates
    ).liveData

}

2 个答案:

答案 0 :(得分:0)

不过,您似乎没有调用此方法 searchMovies

//This function will be called from the fragment when something is typed into the search field
fun searchMovies(movieTitle: String) {
    currentQuery.value = movieTitle
}

如果您在某处调用它并且没有为其添加代码,请使用该部分编辑您的问题,我会尽力提供帮助:)

答案 1 :(得分:0)

从您的 "/" 中删除 @GET,因为您已经添加了 baseUrl(它是 baseUrl 的一部分)。这就是这个 api 的设计方式,您已经在 "/" 中添加了 @GET,现在您的 baseUrl 是这样的:
http://www.omdbapi.com//
看到了吗?额外的 / 最后,这是不显示数据的原因。