如何解决RecyclerView显示最后一个嵌套数组项形成父数组Kotlin
我已经在 Kotlin 中使用 Refrofit,MVVM,DataBinding,协程在我的应用中实现了RecyclerView。问题是我有一些嵌套数组,我想使用viewModel进行解析。 。这是api的结构:
我想要的是解析categoryService数组内的内部服务 arrays 。 xml项布局如下:
<data>
<variable
name="services"
type="com.techease.fitness.models.categoryServices.Service" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_margin="@dimen/tenDP"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_card_view"
android:orientation="vertical"
android:padding="@dimen/tenDP"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/ivService"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:padding="@dimen/tenDP"
android:src="@drawable/test_image_24"
bind:avatar="@{services.attachment}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:hint="@string/user_name"
android:text="@{services.title}"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:hint="@string/user_name"
android:text="@{services.duration}" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我现有的viewModel,它仅返回categoryService [last]中的最后一项并显示服务项。
private lateinit var job: Job
private val catServices = MutableLiveData<List<Service>>()
val discoverServices: MutableLiveData<List<Service>>
get() = catServices
fun discoverServices(context: Context) {
job = CoroutinesIO.ioThenMain(
{
repository.discoverServices(context)
},{
for (i in 0 until it!!.data.categoryServices.size) {
for (services in 0 until it.data.categoryServices.get(i).services.size) {
catServices.value = it.data.categoryServices.get(i).services
}
}
}
)
}
我在viewModel中嵌套了for循环,该循环正确地解析了目标数组,但仅显示最后一项服务数组。这是recyclerView的实现:
val apiRepository = ApiRepository()
factory = DiscoverViewModelFactory(apiRepository)
viewModel = ViewModelProvider(this,factory).get(DiscoverServiceViewModel::class.java)
binding.viewModel = viewModel
viewModel.discoverServices(this)
viewModel.discoverServices.observe(this,Observer { services ->
rvFeatured.also {
it.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
it.setHasFixedSize(true)
if (services != null) {
it.adapter = DiscoverServicesAdapter(services,this)
}
}
})
这是完全绑定的适配器:
private val services: List<Service>,private val listenerHomeServices: DiscoverServicesClickListener
) : RecyclerView.Adapter<DiscoverServicesAdapter.ServicesViewHolder>() {
override fun getItemCount() = services.size
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int) =
ServicesViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),R.layout.custom_discover_layout,parent,false
)
)
override fun onBindViewHolder(holder: ServicesViewHolder,position: Int) {
holder.recyclerViewServicesBinding.services = services[position]
}
class ServicesViewHolder(
val recyclerViewServicesBinding: CustomDiscoverLayoutBinding
) : RecyclerView.ViewHolder(recyclerViewServicesBinding.root)}
再次,我想解析嵌套的服务数组,但是目前recyclerView仅显示最后一个categoryService 1服务数组项。希望这有道理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。