【发布时间】:2021-05-25 07:43:26
【问题描述】:
我已从 kotlin-extention 转移到绑定使用,因为 extention 已被弃用,因此我正在使用绑定重建整个应用程序。
我在从位于我的 Activity 中的片段访问 BottomAppBar 时遇到问题。
在绑定之前,我使用的代码如下:
class CorpoFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as LetturaActivity).bottomAppBar.navigationIcon = ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_baseline_menu_24
)
}
}
但现在我已经在该片段中设置了绑定,例如:
class CorpoFragment : Fragment() {
private var fragmentCorpoBinding: FragmentCorpoBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = FragmentCorpoBinding.inflate(inflater, container, false)
fragmentCorpoBinding = binding
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// how can i access the bottomAppBar from the activity at this point?
}
删除 kotlin 扩展后,我在第一个代码块上得到 Unresolved reference: bottomAppBar..
【问题讨论】:
-
使用 fragmentCorpoBinding.bottomAppBar
-
@AlphaOne bottomAppBar 设置在Activity中,所以
fragmentCorpoBinding.bottomAppBar仍然返回Unresolved reference: bottomAppBar -
这是一种奇怪的做法,但无论如何。您需要将
Activity中的绑定设为公开,然后您可以使用(activity as LetturaActivity).binding.bottomAppBar访问它。
标签: android kotlin android-databinding