У меня проблема с тем, что мой RecyclerView
ничего не показывает, пока я не выключу и не включу экран.
Мое приложение состоит из получения 5 файлов из репозитория GitHub с помощью библиотеки Volley, а затем,привязка текста к некоторому TextViews
и изображения к ImageView
с Glide.Но иногда данные в моем 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>