碎片中的Collapsingtoolbar在滚动时无法显示/隐藏标题头

3

我替换了活动中的片段,并且在我的片段中需要一个 CoordinatorLayout。 以下是我的代码:

CoordinatorActivity 类:

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 类:

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不会显示或隐藏。它总是显示在我的片段屏幕上。 我的代码出了什么问题? 我该如何修复它?

1个回答

1

我的代码出了什么问题?我该如何修复它?

有几件事情需要考虑。首先,你在AppBarLayout中打开一个视图(LinearLayout),其中可能会有一个Toolbar,但这不是唯一的情况。

第一部分:(重要的一部分): 创建这样的layout的最佳想法和正确方式是,首先从fragment_coordinator布局的根中删除LinearLayout,然后将代码从fragment_coordinator复制粘贴到activity_coordinator中。之后,在NestedScrollView内使用FrameLayout作为内容容器

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

P.s:设计完全看起来是错误的。使用创建此类布局的重要部分第一部分。请注意,在Java-Kotlin两侧也会有一些更改(更改布局地址等)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接