Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

androidx.fragment.app.FragmentContainerView #42

Closed
luojt opened this issue Feb 15, 2022 · 17 comments
Closed

androidx.fragment.app.FragmentContainerView #42

luojt opened this issue Feb 15, 2022 · 17 comments

Comments

@luojt
Copy link

luojt commented Feb 15, 2022

如果parent为androidx.fragment.app.FragmentContainerView,会报错

@luojt
Copy link
Author

luojt commented Feb 15, 2022

使用navigation组件,单activity嵌套多个fragment,parent为androidx.fragment.app.FragmentContainerView

@nukc
Copy link
Owner

nukc commented Feb 15, 2022

请了解 FragmentContainerView 的特性,你可以 inject activity 也可以 inject 具体的 fragment,也可以对具体的 view

@luojt
Copy link
Author

luojt commented Feb 15, 2022

FragmentContainerView 继承 FrameLayout,FrameLayout无法使用addview方法

@nukc
Copy link
Owner

nukc commented Feb 15, 2022

不要去 inject FragmentContainerView ,https://developer.android.google.cn/reference/kotlin/androidx/fragment/app/FragmentContainerView
FragmentContainerView 虽然是继承了 FrameLayout,但并不是作为普通的 ViewGroup 那样。
你应该在 fragment 里使用 StateView

@luojt
Copy link
Author

luojt commented Feb 15, 2022

fragment 里面使用了stateview,布局文件是用了fragment标签,他的view.parent为FragmentContainerView

@luojt
Copy link
Author

luojt commented Feb 15, 2022

 <fragment
            android:id="@+id/host_fragment"
            android:name="*.navigation.NavHostFragmentHideShow"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_main_graph" />

@nukc
Copy link
Owner

nukc commented Feb 15, 2022

该 fragment 的 onCreateView 返回的 view 是什么,你是直接 inject 的这个 view 吗

@luojt
Copy link
Author

luojt commented Feb 15, 2022

是的,我新增了上述的判断,解决了该问题

@luojt
Copy link
Author

luojt commented Feb 15, 2022

override fun createStateView(){
        mStateView = StateView.inject(mViewBind.root)
    }

@luojt
Copy link
Author

luojt commented Feb 15, 2022

如果使用jetpack中的navigation组件,会有这样的场景

@nukc
Copy link
Owner

nukc commented Feb 15, 2022

看样是需要加多个判断 FragmentContainerView,估计以后也会有新的这类特殊 ViewGroup

@nukc
Copy link
Owner

nukc commented Feb 15, 2022

@luojt 已发版,v3.0.2

@luojt
Copy link
Author

luojt commented Feb 16, 2022

@nukc 收到,很优秀

@luojt
Copy link
Author

luojt commented Feb 16, 2022

@nukc 我觉得这样判断会不会更好一点

return if (viewGroup.parent is ViewGroup && viewGroup.parent !is FrameLayout) {
      wrap(viewGroup)
} else {
      Injector.wrapChild(viewGroup)
}

FragmentContainerView是FrameLayout的子类,我测试了一下可以

@nukc
Copy link
Owner

nukc commented Feb 16, 2022

FragmentContainerView 才是特殊的,FrameLayout 是很常用的,且是正常的

@luojt
Copy link
Author

luojt commented Feb 16, 2022

好的,明白了

@nukc nukc closed this as completed Apr 14, 2022
@csh159
Copy link

csh159 commented Nov 17, 2022

当不引用 androidx.fragment:fragment 的时候回报 java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/fragment/app/FragmentContainerView;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants