我正在尝试将 executePendingBinding 与 gridView 适配器一起使用。在某些数据更改上,绑定最初并未执行,但在此之前正在执行其他 UI 操作。我该如何防止这种情况。这是我的ImageAdapter
:
class ImageAdapter constructor(
private val mContext: Context,
private val resource_layout: Int,
private val viewModelList: ArrayList<ProfileViewModel>
) :
ArrayAdapter<ImageAdapter.ViewHolder>(mContext, resource_layout) {
private lateinit var imageCardBinding: ImageCardBinding
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var convertView = convertView
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(resource_layout, null)
imageCardBinding = DataBindingUtil.bind(convertView)!!
convertView.tag = position
convertView.tag = imageCardBinding
} else imageCardBinding = convertView.tag as ImageCardBinding
imageCardBinding.profileViewModel = viewModelList[position]
imageCardBinding.executePendingBindings()
return imageCardBinding.root
}
override fun getCount(): Int {
return viewModelList.size
}
inner class ViewHolder
}
在某些值成功更改后,我正在执行此操作:
gridView.get(imageGridPosition).grid_item_progress.visibility = View.GONE
profile_root.snackBar("Image is set ! You can tap on the image to change it")
但是由于绑定没有立即完成,因此gridView.size
为 0,如果我注释掉该行,图片会在稍后设置,但snackbar
在此之前显示。可以修复executePendingBinding()
吗,如果是这样,那该怎么做?还是有其他方法?