[英]Recyclerview Items not visible. Empty Screen is Coming
我在一项活动中有一个回收站视图。 我无法显示列表项。
在我的活动中,我将适配器与 arrayList favCityList 绑定。
活动:
recyclerView.apply {
layoutManager = LinearLayoutManager(this@SearchFavrouiteActivity)
city_list.adapter = com.example.diyatest.view.CityAdapter()
(adapter as CityAdapter).updateItems(favCitesList);
}
这是我的适配器。 我在 UpdateItems ListView 中传递列表。 在调试时,我可以看到编译器在那里运行,但它不可见。 看起来上下文有些问题。
class CityAdapter() : RecyclerView.Adapter<CityAdapter.MyViewHolder>() {
val mCityData = ArrayList<WeatherResponseData>()
fun updateItems(list: ArrayList<WeatherResponseData>){
mCityData.clear()
mCityData.addAll(list)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.content_search_favrouite, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
mCityData[position].let {
holder.bind(mCityData = it)
}
}
override fun getItemCount(): Int {
return mCityData.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(mCityData: WeatherResponseData) {
itemView.city_Country_txt.text = mCityData.name
itemView.date_txt.text = (mCityData.date)?.toLong()?.let { getDate(it) }
itemView.temp_txt.text=mCityData.main?.temp.toString()
}
private fun getDate(date: Long): String {
val timeFormatter = SimpleDateFormat("dd.MM.yyyy")
return timeFormatter.format(Date(date*1000L))
}
}
}
这是我的活动 main_Xml。 在这个我叫recyclerview。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
android:background="@drawable/bg_gradient"
tools:context=".view.SearchFavrouiteActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saved Cities"
android:textSize="40dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@android:color/white"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/city_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginTop="30dp"
android:background="@android:color/darker_gray"
android:gravity="center_horizontal"
/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是我的适配器视图。 我在这里使用cardview。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:layout_marginLeft="@dimen/main_padding"
android:layout_marginTop="@dimen/main_padding"
android:layout_marginRight="@dimen/main_padding">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="@dimen/main_padding">
<TextView
android:id="@+id/city_Country_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:text="No City Data" />
<TextView
android:id="@+id/temp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/city_Country_txt"
android:layout_marginTop="10dp"
tools:text="Degrees" />
<TextView
android:id="@+id/date_txt"
android:layout_width="@dimen/list_weather_icon_size"
android:layout_height="@dimen/list_weather_icon_size"
android:layout_below="@+id/temp_txt"
android:layout_alignParentRight="true"
android:contentDescription="@string/weather_icon_description"
android:text="date" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
我有建议让您检查并让我知道是否可行更改 xml 中的 recyclerview 代码
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/city_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginTop="30dp"
/>
这可能是问题所在
city_list.apply {
layoutManager = LinearLayoutManager(this@SearchFavrouiteActivity,LinearLayoutManager.VERTICAL, false)
val cityAdapter = CityAdapter()
adapter = cityAdapter
cityAdapter.updateItems(favCitesList);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.