如果使用CollapsingToolbarLayout,如何在片段中固定视图?

时间:2019-04-22 12:28:33

标签: android android-fragments

我正在将AppBarLayout与CollapsingToolbarLayout一起使用,当用户滚动屏幕时可以更改其高度。另外,我在AppBarLayout下有片段。其中一个片段具有自定义的底部导航栏1。当CollapsingToolbarLayout完全打开时,我看不到该片段的底部导航栏。我想以某种方式固定它。2

我尝试不在该片段上使用layout_behavior =“ @ string / appbar_scrolling_view_behavior”,但在该片段的视图中使用它,但是它不起作用。

Main.xml

<android.support.v4.widget.DrawerLayout>
    <android.support.design.widget.CoordinatorLayout>
        <android.support.design.widget.AppBarLayout
         android:layout_height="@dimen/app_bar_height">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:gravity="center_vertical|start"
                    app:layout_collapseMode="pin"
                    app:navigationIcon="@mipmap/burger"/>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/content_frame"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

some_fragment.xml

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--This view I want to pin-->
    <View
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">
    </View>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation_bar"
        android:layout_alignParentTop="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我设法找到一些奇怪的解决方案。此代码适用于Xamarin Android C#。我只是根据AppBarLayout偏移量

protected override void OnCreate(Bundle bundle)
{
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.home_view);
        var appBar = FindViewById<AppBarLayout>(Resource.Id.app_bar);
        appBar.AddOnOffsetChangedListener(this);
}

public void OnOffsetChanged(AppBarLayout appBarLayout, int verticalOffset)
        {
            var minHeight = Resources.GetDimension(Resource.Dimension.app_bar_min_height);

            var fragment = SupportFragmentManager.FindFragmentById(Resource.Id.content_frame);
            if (fragment is DashboardView) {
                var layout = fragment.View.FindViewById<RelativeLayout>(Resource.Id.dashboard_layout);
                var param = new FrameLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);

                param.SetMargins(0, 0, 0, verticalOffset + (int)minHeight);
                layout.LayoutParameters = param;
            }
        }