簡體   English   中英

帶有ViewPager和底部按鈕的折疊工具欄

[英]Collapsing toolbar with viewpager and bottom button

我的活動帶有折疊的工具欄和視圖尋呼機。 在其中一個頁面上,有一個Webview,下面有一個按鈕:

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button"
    android:layout_alignParentTop="true">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="false" />
</android.support.v4.widget.NestedScrollView>

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

</RelativeLayout>

編輯:和其他XML

<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinator"
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.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    app:elevation="0dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <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">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/accent" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0px" />

</LinearLayout>

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

該按鈕應始終可見。 問題是,當不折疊工具欄時,它會被推出屏幕,並且僅在折疊工具欄時可見

我已經進行了很多搜索,但是找不到解決方案。 另外,我還查看了快餐欄的xml,因為它始終顯示在正確的位置,但是我看不到任何特殊的地方。 您對如何解決此問題有任何想法,或者使用ViewPager不可能嗎?

edit2:在此答案中,問題描述得非常好,但是建議的解決方案直接將視圖添加到協調器視圖中是行不通的,因為該按鈕僅屬於頁面中的一部分,並且基於是否顯示正確的頁面似乎真的很糟糕:) https://stackoverflow.com/a/32959820/1625744

所以我找到了一個看起來不錯的可行解決方案。 我擴展了AppBarLayout.ScrollingViewBehavior並添加了此方法:

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                                      View dependency) {
    final CoordinatorLayout.Behavior behavior =
            ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
    if (behavior instanceof AppBarLayout.Behavior) {
        if (childBottom == 0) {
            childBottom = child.getBottom();
            int offset = childBottom - parent.getHeight();
            ViewGroup.LayoutParams params = child.getLayoutParams();
            initialHeigth = child.getHeight() - offset;
            params.height = initialHeigth;
            child.setLayoutParams(params);
        } else {
            int offset = dependency.getHeight() - dependency.getBottom();
            ViewGroup.LayoutParams params = child.getLayoutParams();
            params.height = initialHeigth + offset;
            child.setLayoutParams(params);
        }
    }
    return super.onDependentViewChanged(parent, child, dependency);
}

不知道這是否是一個好的解決方案,因為布局一直會重置大小。 同樣,在超級方法中,偏移量的計算方式也更加復雜,但是所有這些東西都在私有方法和變量中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM