【发布时间】: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