android - 无法从 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>
谢谢大家
解决方案
您应该在 onCreateView 回调方法中调用 initMembers() 方法而不是在 ViewCreated
推荐阅读
- asp.net-core - 在为托管的 Blazor WebAssembly 应用提供服务之前,如何使用 Azure AD B2C 对服务器上的用户进行身份验证?
- python - 无法从 Node.js 运行 python 文件
- java - 使用 CameraX 保存图像时出现“错误的偏移可能导致无限循环”错误
- reactjs - 尝试使用 react-to-print 从组件中打印
- javascript - Foreach 获取响应 javascript
- r - ggplot_bars 渐变填充
- file - Flink - StreamingFileSink 到 s3 无法从上一个快照中恢复 (java.io.FileNotFoundException)
- javascript - 是否可以在使用 if 或 v-if 按下按钮后显示文本?
- java - 如何确定按字母顺序排列的列表或数字?
- python - MDDropDownMenu KivyMD的绑定按钮