【问题标题】:recyclerview showing empty white screenrecyclerview 显示空白屏幕
【发布时间】:2019-06-14 14:10:57
【问题描述】:

我是新的 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?) {

        }
    })
}

}

在适配器类之下 类 RecyclerAdapter(val context: 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>

【问题讨论】:

  • 你检查过布局检查器吗?您没有提供完整的项目 xml 文件内容,因此可能存在问题。
  • @NoSoyBeto 我添加了缺少的 xml 项,请检查它
  • 没有else 表示HTTP 错误,空onFailure 表示连接错误。你不会知道是否有任何问题。
  • @Miha_x64 在这种情况下你有什么建议
  • 在失败回调中添加这个t?.printStackTrace(),然后在logcat控制台中检查错误。

标签: kotlin retrofit2 android-recyclerview


【解决方案1】:

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>

【讨论】:

  • 这是一个原因,另一个是你忽略了MainActivity上的失败回调。 InternetPermission 的错误可能会导致问题。请打印堆栈跟踪并将其添加到此处。
  • if(response?.body() != null) 的情况是否曾经发生过?添加一个 else 子句并检查它是否正在发生。
  • 其他条款我必须写的内容
  • 至少有一个 Log.i("ELSE CLAUSE", "The response is empty")。如果发生这种情况,则 api 调用有问题。
  • 还是一样的白屏