【问题标题】:One part of Fragment layout is not showing in ActivityFragment 布局的一部分未在 Activity 中显示
【发布时间】:2019-07-30 11:18:23
【问题描述】:

当我点击第一个片段的 FAB 时,我在主活动中有 2 个 片段,它将被第二个片段替换,当我在片段 2 中按下返回按钮时,它将返回片段 1,但它不显示布局的很大一部分,请查看此图像以了解问题:

1: When the app launched

2: when I pressed back button in 2nd fragment and cames back to 1st fragment

在第一个片段 xml 中,我有 3 部分主要项目:

   <androidx.coordinatorlayout.widget.CoordinatorLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <com.google.android.material.appbar.AppBarLayout>
             ...
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView>
             ...
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout >

实际上,当我从第二个片段返回到第一个片段时,NestedScrollview 及其子项不显示。

在 Fragment 1st 中,我使用以下代码开始 Fragment 2nd:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_first, container, false);
    FloatingActionButton fab = view.findViewById(R.id.fabAddWord);
    fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager()
                    .beginTransaction()
                    .addToBackStack(TAG)
                    .replace(R.id.fragment_container, SecondFragment.newInstance(), 
                            WordSearchFragment.class.getSimpleName())
                    .commit();            
            }
    });

    return view;
}

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您应该覆盖 onBackPressed 并在按下后退按钮时显示第一个片段

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 1) 
            getFragmentManager().popBackStack();
        else 
           super.onBackPressed();
    }
    

    【讨论】:

      【解决方案2】:

      2 天后我解决了问题,问题出在我用作 Fragment ContainerFrameLayout 上。我学习了 Use FrameLayout as Container 并且到处都看到人们使用 Framelayout,但实际上 FrameLayot 并不适用于复杂的 Layout Fragments。 所以在 MainActivity.xml 中,我将 FrameLayout 更改为 ConstraintLayout 并且工作了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多