如何使用绑定从 Fragment 访问 Activity 中的 BottomAppBar?
How to use bindings to access BottomAppBar in Activity from Fragment?
由于 extention
已弃用,我已从 kotlin-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
..
可通过 binding
class.
访问变量
private lateinit var binding: FragmentCorpoBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentCorpoBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.bottomAppBar
}
我建议你不要访问从 Fragment 到 Activity 的视图元素,因为每个 activity/fragment 都应该有其独立的视图(关注点分离)。
如果您确实需要它,只需在 Activity:
中添加类似的内容
class LetturaActivity {
fun updateBottomMenu() {
binding.bottomAppBar.navigationIcon = ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_baseline_menu_24
)
}
}
由于 extention
已弃用,我已从 kotlin-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
..
可通过 binding
class.
private lateinit var binding: FragmentCorpoBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentCorpoBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.bottomAppBar
}
我建议你不要访问从 Fragment 到 Activity 的视图元素,因为每个 activity/fragment 都应该有其独立的视图(关注点分离)。 如果您确实需要它,只需在 Activity:
中添加类似的内容class LetturaActivity {
fun updateBottomMenu() {
binding.bottomAppBar.navigationIcon = ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_baseline_menu_24
)
}
}