滚动时片段中的Collapsingtoolbar不显示/隐藏标题

时间:2018-08-06 04:09:51

标签: android android-layout android-coordinatorlayout

我在一个活动中替换了片段,而在片段中我需要一个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 个答案:

答案 0 :(得分:1)

  

我的代码中发生了什么?以及我该如何解决?

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

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

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

P.s:设计似乎完全是错误的。使用第一部分,这是创建此类布局的重要部分。请注意,java-kotlin内部也将进行一些更改。 (更改布局地址等)