DylanCaiCoding / LoadingStateView

Decoupling the code of toolbar or loading status view. (深度解耦标题栏,解耦加载中、加载失败、无数据等缺省页,支持两行代码集成到基类)
Apache License 2.0
674 stars 53 forks source link

多Fragment中如何使用本库? #7

Closed liangjianqi closed 4 years ago

liangjianqi commented 4 years ago

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

DylanCaiCoding commented 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
  }