我的问题是,RecyclerView
会在我关闭屏幕并重新打开之前什么都不会显示。
我的应用程序包括使用Volley库从GitHub存储库中获取5个文件,然后使用Glide将文本绑定到某些TextViews
并将图像绑定到ImageView
。但是有时我的RecyclerView中的数据没有显示。
代码如下:
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.layoutManager = LinearLayoutManager(this)
val queue = Volley.newRequestQueue(this)
val baseurl = "https://raw.githubusercontent.com/nubicoud/Dealist/master/"
val appNames:ArrayList<String> = ArrayList()
val appDesc:ArrayList<String> = ArrayList()
val appPrice:ArrayList<String> = ArrayList()
val appImage:ArrayList<String> = ArrayList()
val appDownload:ArrayList<String> = ArrayList()
var url = "${baseurl}name?"
val appNamesReq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
val appNamesResponse = response.toString()
for (i in 1..appNamesResponse.lines()[0].toInt()){
appNames.add(appNamesResponse.lines()[i])
}
},
Response.ErrorListener {
errorRequest()
})
queue.add(appNamesReq)
url = "${baseurl}desc?"
val appDescReq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
val appDescResponse = response.toString()
for (i in 1..appDescResponse.lines()[0].toInt()){
appDesc.add(appDescResponse.lines()[i])
}
},
Response.ErrorListener {
errorRequest()
})
queue.add(appDescReq)
url = "${baseurl}price?"
val appPriceReq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
val appPriceResponse = response.toString()
for (i in 1..appPriceResponse.lines()[0].toInt()){
appPrice.add(appPriceResponse.lines()[i])
}
},
Response.ErrorListener {
errorRequest()
})
queue.add(appPriceReq)
url = "${baseurl}image?"
val appImageReq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
val appImageResponse = response.toString()
for (i in 1..appImageResponse.lines()[0].toInt()){
appImage.add(appImageResponse.lines()[i])
}
},
Response.ErrorListener {
errorRequest()
})
queue.add(appImageReq)
recyclerView.adapter = AppsAdapter(appNames, appDesc, appPrice, appImage, appDownload)
AppsAdapter.kt
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppsAdapter.ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.apps_row, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: AppsAdapter.ViewHolder, position: Int) {
val Context = holder.itemView.context
holder.name.text = appNames[position]
holder.description.text = appDesc[position]
holder.price.text = appPrice[position]
holder.download.setOnClickListener {
Context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(appDownload[position])))
}
Glide.with(Context).load(appImage[position]).into(holder.image)
}
override fun getItemCount() = appNames.size
class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
val image = itemView.findViewById<ImageView>(R.id.image)!!
val name = itemView.findViewById<TextView>(R.id.name)!!
val description = itemView.findViewById<TextView>(R.id.description)!!
val price = itemView.findViewById<TextView>(R.id.price)
val download = itemView.findViewById<Button>(R.id.download)
}
activity_main.xml
<androidx.constraintlayout.widget.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"
android:background="#FFFFFF"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FFFFFF"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationMenu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:itemIconTint="@color/bottomnavbarTextColor"
app:itemTextColor="@color/bottomnavbarTextColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:0)
通过在其他所有操作之前更改下载行为来解决该问题。
首先下载数据,然后执行setContentView(R.layout.activity_main)
和recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = AppsAdapter(appNames, appDesc, appPrice, appImage, appDownload)
recyclerView.setHasFixedSize(true)
(recyclerView.adapter as AppsAdapter).notifyDataSetChanged()
,然后设置RecyclerView。
{{1}}