当图像标题包含大图像时,CollapsingToolbarLayout以最小高度打开

时间:2018-03-23 12:33:27

标签: android appbar

我整合了折叠工具栏。我想要像[Set starting height of CollapsingToolbarLayout这样的功能。我尝试了相同的代码,但没有找到解决方案。

我使用的是编译sdk版本26​​。

是否有人实施过同样的事情或者可以帮助我找到解决方案?

<android.support.constraint.ConstraintLayout 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="test_scroll.com.testscroll.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/llmain_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="16dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/img_user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:maxHeight="256dp"
                    android:src="@drawable/img1"
                    app:layout_collapseMode="parallax" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <LinearLayout
                android:id="@+id/llMainNested"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:orientation="vertical">


            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>


    </android.support.design.widget.CoordinatorLayout>

</android.support.constraint.ConstraintLayout>

onCreate of activity

mCoordinatorLayour = (CoordinatorLayout) findViewById(R.id.llmain_content);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);

        mAppBarLayout.post(new Runnable() {
            @Override
            public void run() {
                int heightPx = findViewById(R.id.img_user_photo).getHeight();
                setAppBarOffset(heightPx/2);
            }
        });


private void setAppBarOffset(int offsetPx){
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
//        behavior.onNestedPreScroll(mCoordinatorLayour, mAppBarLayout, null, 0, offsetPx, new int[]{0, 0});
        if(behavior!=null)
        behavior.onNestedPreScroll(mCoordinatorLayour, mAppBarLayout, null, 0, offsetPx, new int[]{0, 0}, 1);
        else
            Log.i("TAG", "setAppBarOffset: null");
    }

2 个答案:

答案 0 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/llmain_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme">


            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorAccent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


                <ImageView
                    android:id="@+id/img_user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/meeting_bg"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    app:contentInsetStartWithNavigation="0dp"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <include
                    layout="@layout/widget_header_view_top"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone" />
                </android.support.v7.widget.Toolbar>


            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <LinearLayout
                android:id="@+id/llMainNested"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>


    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

在折叠时设置工具栏标题和子标题...我使用了工具栏中包含的布局...检查xml ...&#34; widget_header_view_top&#34; 您可以根据需要自定义.....

Java类:

 setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_nav_white);
    getSupportActionBar().setTitle("");
    collapsing_toolbar.setTitle(" ");

appbar.addOnOffsetChangedListener(this);

 @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;

        if (percentage >= 0.6f && isHideToolbarView) {
            toolbarHeaderView.setVisibility(View.VISIBLE);
            if (menuItemEdit != null) {
                //   menuItemEdit.setIcon(R.drawable.edit_white);
            }
            isHideToolbarView = !isHideToolbarView;

        } else if (percentage < 0.6f && !isHideToolbarView) {
            if (menuItemEdit != null) {
                //menuItemEdit.setIcon(R.drawable.edit);
            }
            toolbarHeaderView.setVisibility(View.GONE);
            isHideToolbarView = !isHideToolbarView;
        }
    }

答案 1 :(得分:0)

我通过setAppBarOffset方法的上述代码得到了试错的答案。 在这个方法中,我设置了偏移量,我希望在页面加载时保持可见。相反,我们需要传递我们需要隐藏的偏移量。所以我添加了下面的代码,以便在创建时保持256 px的图像可见。

public static int getDeviceHeight(Context context) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int height = metrics.heightPixels;

    Rect rectgle = new Rect();
    Window window = ((Activity) context).getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int StatusBarHeight = rectgle.top;
    height = height - StatusBarHeight;
    return height;
}



public static int getMinHeightOfAppbar(int device_height,int view_height){
    int offsetPx = (int) (device_height * 0.45);
    offsetPx = view_height - offsetPx;
    return offsetPx;
}

int device_height = Utility.getDeviceHeight(ProfileSettingsNew.this);
offsetPx= Utility.getMinHeightOfAppbar( device_height,imgv_user_photo.getHeight());

setAppBarOffset(offsetPx);