首页 > 解决方案 > 无法从 GreenRobotEventBus 更新回收站视图

问题描述

从过去两天开始,我正在尝试设置从 Eventbus 收到的回收器视图值,但出现错误 No adapter attach; 跳过布局。我已经尝试并测试了互联网上可用的每个代码,例如 1. https://github.com/gunhansancar/eventbus-example在适配器 2 中附加数据。试图通过 EventBus 影响 RecyclerView 项目- 在适配器类中创建方法但仍然没有正在发生

我收到事件响应,但无法在回收站视图中使用该响应

代码

  @Subscribe(threadMode = ThreadMode.MAIN)
    fun pickDropChooseEvent (data : PickAddressDone){
        try {
//vehicle list result is a model
            vehicleListResult = data.vehicleListResult

            vehicleAdapter.updateData(vehicleListResult.vehicleCategory!!);
            vehicleAdapter.notifyDataSetChanged();
        }
        catch (exception : Exception )
        {
            Log.d("vehicle info" ,"catch pick drop "+exception.toString())
        }
    }

然后在 ViewCreated 我声明了一个方法“initMembers”来初始化片段的视图

 private fun initMembers() {
        try{
            vehicleList = ArrayList()
            vehicleList.addAll(vehicleListResponse.vehicleListResult!!.vehicleCategory!!)
            tvDistance.text = vehicleListResult.ride!!.distanceText
            rvVehicleList.setHasFixedSize(true)
            rvVehicleList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            rvVehicleList.setAdapter(vehicleAdapter)
           vehicleAdapter = VehicleCategoryAdapter(context!!, vehicleList)


          // i have even tried notify change

        }catch (exception: Exception)
        {
            Log.d("vehicle info", " exception "+exception)
        }

    }

更新:已解决,代码中没有问题,但问题出在 Xml ConstraintLayout..

   <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="@dimen/margin_2dp"
        android:background="@drawable/grey_shadow_background"
        app:layout_constraintBottom_toTopOf="@+id/clRideInfo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintTop_toTopOf="parent" // this was causing the issue as height was not assigned to the constraint view. Recycler view is showing after removing it
        app:layout_constraintStart_toStartOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvVehicleList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/layout_vehiclelist_item" />
    </androidx.constraintlayout.widget.ConstraintLayout> 

谢谢大家

标签: androidandroid-recyclerviewgreenrobot-eventbus

解决方案


您应该在 onCreateView 回调方法中调用 initMembers() 方法而不是在 ViewCreated


推荐阅读