recyclerview显示空白屏幕

时间:2019-06-14 14:10:57

标签: kotlin android-recyclerview retrofit2 recycler-adapter

我是新人Kotlin,最近开始进行改造,我已按照本教程http://velmm.com/kotlin-retrofit-android-example-with-recyclerview/

进行操作

但是当我运行代码时,它在下面显示空白屏幕 empty screenshot

在我的MainActivity.kt下面 MainActivity类:AppCompatActivity(){

lateinit var recyclerView: RecyclerView
lateinit var recyclerAdapter: RecyclerAdapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    recyclerView = findViewById(R.id.recyclerview)
    recyclerAdapter = RecyclerAdapter(this)
    recyclerview.layoutManager = LinearLayoutManager(this)
    recyclerView.adapter = recyclerAdapter


    val apiInterface = ApiInterface.create().getMovies()

    //apiInterface.enqueue( Callback<List<Movie>>())
    apiInterface.enqueue( object : Callback<List<Movie>> {
        override fun onResponse(call: Call<List<Movie>>?, response: Response<List<Movie>>?) {

            if(response?.body() != null)
                recyclerAdapter.setMovieListItems(response.body()!!)
        }

        override fun onFailure(call: Call<List<Movie>>?, t: Throwable?) {

        }
    })
}

}

在Adapter类之下 RecyclerAdapter类(val上下文:Context):RecyclerView.Adapter(){

var movieList : List<Movie> = listOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_adapter,parent,false)
    return MyViewHolder(view)
}

override fun getItemCount(): Int {
    return movieList.size
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    holder.tvMovieName.text = movieList.get(position).title
    Glide.with(context).load(movieList.get(position).image)
        .apply(RequestOptions().centerCrop())
        .into(holder.image)
}

fun setMovieListItems(movieList: List<Movie>){
    this.movieList = movieList;
    notifyDataSetChanged()
}

class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

    val tvMovieName: TextView = itemView!!.findViewById(R.id.title)
    val image: ImageView = itemView!!.findViewById(R.id.image)

}

}

在ApiInterface.kt下 接口ApiInterface {

@GET("volley_array.json")
fun getMovies() : Call<List<Movie>>

companion object {

    var BASE_URL = "http://35.200.174.74/apis/"

    fun create() : ApiInterface {

        val retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BASE_URL)
            .build()
        return retrofit.create(ApiInterface::class.java)

    }
}

}

Movie.kt下面

 data class Movie(var title: String, var image: String)

在recyclerview_adapter.xml下面

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             android:layout_width="match_parent"
                                             android:layout_height="150dp">

    <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

activity_main.xml下面

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

recyclerview_adapter.xml文件存在问题。请勿在约束布局内部使用match_parent,而应在0dp内使用,并且在执行此操作时必须考虑到如果要全角则需要约束开始和结束,或者如果要垂直进行则要约束顶部和底部。这里的文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                         android:layout_width="match_parent"
                                         android:layout_height="150dp">

    <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

该活动应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>