我正在使用Kotlin开发新闻应用程序,但它显示空白屏幕,并且logcat上没有错误或异常
在我的SportNewsAdapter.kt下面
class SportNewsAdapter(val context: Context) : RecyclerView.Adapter<SportNewsAdapter.MyViewHolder>() { var articleList : List<Article> = listOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.news_list,parent,false) return MyViewHolder(view) } override fun getItemCount(): Int { return articleList.size } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.tvMovieName.text = articleList.get(position).title Glide.with(context).load(articleList.get(position).urlToImage) .apply(RequestOptions().centerCrop()) .into(holder.image) } fun setMovieListItems(movieList: List<Article>){ this.articleList = articleList; notifyDataSetChanged() } class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) { val tvMovieName: TextView = itemView!!.findViewById(R.id.title) val image: ImageView = itemView!!.findViewById(R.id.image) } }
低于news_item.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" xmlns:app="http://schemas.android.com/apk/res-auto"> <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>
MainActivity.kt以下
class MainActivity : AppCompatActivity() { lateinit var recyclerView: RecyclerView lateinit var sportNewsAdapter: SportNewsAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerView = findViewById(R.id.recyclerView) sportNewsAdapter = SportNewsAdapter(this) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = sportNewsAdapter val apiInterface = SportNewsInterface.create().getNews() apiInterface.enqueue( object : Callback<List<Article>> { override fun onResponse(call: Call<List<Article>>?, response: Response<List<Article>>?) { if(response?.body() != null) sportNewsAdapter.setMovieListItems(response.body()!!) } override fun onFailure(call: Call<List<Article>>?, t: Throwable?) { } }) } }
activity_main.xml下面
<androidx.recyclerview.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" />
SportNewsInterface.kt以下
界面SportNewsInterface {
@GET("v2/top-headlines?country=us&apiKey=da331087e3f3462bb534b3b0917cbee9") fun getNews() : Call <List<Article>> companion object { var BASE_URL = "https://newsapi.org/" fun create() : SportNewsInterface { val retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl(BASE_URL) .build() return retrofit.create(SportNewsInterface::class.java) } } }
在SportNewsResponse.kt下面,其中进行了改造
data class SportNewsResponse(
val articles: List<Article>,
val status: String,
val totalResults: Int
)
答案 0 :(得分:0)
更改回收站的视图高度以包装内容并再次运行