RecyclerView显示最后一个嵌套数组项形成父数组Kotlin

问题描述

我已经在 Kotlin 中使用 Refrofit,MVVM,DataBinding,协程在我的应用中实现了RecyclerView。问题是我有一些嵌套数组,我想使用viewModel进行解析。 。这是api的结构:

enter image description here

我想要的是解析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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...