简体   繁体   English

带有ViewPager和底部按钮的折叠工具栏

[英]Collapsing toolbar with viewpager and bottom button

I have an activity with a collapsing toolbar and a view pager. 我的活动带有折叠的工具栏和视图寻呼机。 On one of those pages, there is a webview with a button below it: 在其中一个页面上,有一个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>

edit: and the other XML 编辑:和其他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>

The button should always be visible. 该按钮应始终可见。 The problem is, that when the toolbar is not collapsed, it gets pushed out of the screen, and gets only visible, when you collapse the toolbar 问题是,当不折叠工具栏时,它会被推出屏幕,并且仅在折叠工具栏时可见

I've been searching a lot but can't find a solution. 我已经进行了很多搜索,但是找不到解决方案。 Also I had a look into the xml of the snackbar, as it is always shown on the correct position, but I couldn't see anything special. 另外,我还查看了快餐栏的xml,因为它始终显示在正确的位置,但是我看不到任何特殊的地方。 You have any idea on how to fix this, or is it not possible with a ViewPager? 您对如何解决此问题有任何想法,或者使用ViewPager不可能吗?

edit2: in this answer, the problem is described pretty good, however the suggested fix, to add the view to the coordinator view directly won't work, as the button only belongs to on of the pages and setting visibility off the button based on whether the correct page is shown seems like REALLY bad style :) https://stackoverflow.com/a/32959820/1625744 edit2:在此答案中,问题描述得非常好,但是建议的解决方案直接将视图添加到协调器视图中是行不通的,因为该按钮仅属于页面中的一部分,并且基于是否显示正确的页面似乎真的很糟糕:) https://stackoverflow.com/a/32959820/1625744

so I found a working solutions that doesn't seem so bad. 所以我找到了一个看起来不错的可行解决方案。 I extended AppBarLayout.ScrollingViewBehavior and added this method: 我扩展了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);
}

not sure if this is a good solution, as the layout gets size reset all the time. 不知道这是否是一个好的解决方案,因为布局一直会重置大小。 Also the offset is calculated way more complicated in the super method, but all that stuff is in private methods and variables 同样,在超级方法中,偏移量的计算方式也更加复杂,但是所有这些东西都在私有方法和变量中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM