滚动时片段中的 Collapsingtoolbar 不是 show/hide header

Collapsingtoolbar in fragment not show/hide header when scrolling

我替换了 activity 中的片段,在我的片段中我需要一个 CoordinatorLayout。 这是我的代码:

CoordinatorActivity class:

public class CoordinatorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);
        Fragment newFragment = new FragmentCoordinator();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fr_main, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

activity_coordinator.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:id="@+id/fr_main"
    tools:context="com.app.hoatv.CoordinatorActivity">
</FrameLayout>

FragmentCoordinator class:

public class FragmentCoordinator extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_coordinator, container, false);
    }
}

fragment_coordinator.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.app.hoatv.CoordinatorActivity">
    <android.support.design.widget.CoordinatorLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:elevation="0dp"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme">
            <include layout="@layout/layout_group_header"/>
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fillViewport="true">
            <android.support.v7.widget.AppCompatTextView
                android:layout_height="1000dp"
                android:layout_width="match_parent"
                android:text="@string/lorem"
                android:background="@color/colorAccent"
                android:id="@+id/container"/>
        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

layout_group_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#a70000">

</LinearLayout>

但是当我滚动时 NestedScrollView layout_group_header.xml 不显示或隐藏。它总是显示在我的片段屏幕中。 我的代码中发生了什么?以及如何修复它?

What happened in my code? and How I can fix it?

您应该考虑的事情很少。首先,您在 AppBarLayout 中打开一个视图 (LinearLayout),其中可能有一个 Toolbar ,但情况不仅如此。

第一部分:(重要部分)创建此类 layout 的最佳想法和正确方法 是首先,从 fragment_coordinator 布局的根目录中删除 LinearLayout,然后将代码从 fragment_coordinator 复制粘贴到 activity_coordinator。然后在NestedScrollView.

里面使用FrameLayout作为内容的Container

进行更改部分(根据您的要求): 毕竟,您可以在主布局中的任何位置放置代码,例如,如果您想要 layout_group_headerlayout 中滚动后隐藏,您可能想在 fragment_coordinator 中使用 layout_group_header 代码,这将是一个滚动内容。否则,您可以使用 CoordinatorLayout 内或任何地方的代码,而不是像那样包含它。

P.s: 设计好像完全不对。使用第一部分,这是创建此类布局的重要部分。请注意,java-kotlin 方面也会有一些变化。 (更改布局地址等)