Closed liangjianqi closed 4 years ago
抱歉,现在才看到。你创建对象时传了 requireActivity(),这是在 Activity 添加了标题栏,所以每创建一个 Fragment 都在单 Activity 的布局里添加,才出现了多个的情况。在 Fragment 应该用 View 来创建,即 LoadingHelper(view)。可以封装个 fun view.setToolbar() 方法,下面给个参考示例。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_register, container, false)
val loadingHelper = view.setToolbar("注册", NavIconType.BACK)
return loadingHelper.decorView
}
fun Fragment.setToolbar(title: String, type: NavIconType = NavIconType.BACK) = LoadingHelper(requireActivity()).apply { register(ViewType.TITLE, ToolbarAdapter(title, type)) setDecorHeader(ViewType.TITLE) toolbar?:apply { Log.e("louis","toolbar is null") } }
hello,请教一下,假如项目中是多Fragment,如何在同一个Activity下给不同的Fragment添加不同的toolbar,我尝试这样做之后,会添加多个,并且打印Log日志看到toolbar is null