Android 与TableLayout和CoordinatorLayout一起使用时,Google地图视图中的偏移量

Android 与TableLayout和CoordinatorLayout一起使用时,Google地图视图中的偏移量,android,android-mapview,android-coordinatorlayout,android-tablayout,Android,Android Mapview,Android Coordinatorlayout,Android Tablayout,我有一个TabLayout和一个带有滚动行为的ViewPager。 在ViewPager中,有两种布局:- (RecyclerView+ProgressBar) 谷歌地图视图 1) 当选项卡位于RecyclerView时,当滚动Recycler视图时,TableAyout将滚动/隐藏。这部分工作正常 2) 但是,当选项卡位于GoogleMap视图时,整个视图向下移动。 缩放按钮和谷歌版权信息不可见。(见下图)。当滚动行为被注释掉时,地图将正确显示,但recyclerView不再隐藏TableLa

我有一个TabLayout和一个带有滚动行为的ViewPager。 在ViewPager中,有两种布局:-

  • (RecyclerView+ProgressBar)
  • 谷歌地图视图
  • 1) 当选项卡位于RecyclerView时,当滚动Recycler视图时,TableAyout将滚动/隐藏。这部分工作正常

    2) 但是,当选项卡位于GoogleMap视图时,整个视图向下移动。 缩放按钮和谷歌版权信息不可见。(见下图)。当滚动行为被注释掉时,地图将正确显示,但recyclerView不再隐藏TableLayout

    是否有一种方法可以正确显示地图,同时允许在显示recyclerview时隐藏TableLayout

    以下是coordinatorLayout的xml

    <android.support.design.widget.CoordinatorLayout 
        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.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:orientation="vertical">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                style="@style/TabLayoutTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="4dp"
                android:theme="@style/TabLayoutTheme"
                app:layout_scrollFlags="scroll|enterAlways"
                app:tabSelectedTextColor="@color/primary_text" />
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    从XML中删除滚动视图行为,并在选择“MapFragment以外”时动态添加它。更新更改后,您的XML应如下所示

    您的Xml文件

    <android.support.design.widget.CoordinatorLayout
    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_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:orientation="vertical">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            style="@style/TabLayoutTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="4dp"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </android.support.design.widget.CoordinatorLayout>
    
    关闭/打开滚动的代码

    public void turnOffTabLayoutScrolling() {
    
        //turn off scrolling
    
        AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mTabLayout.getLayoutParams();
        toolbarLayoutParams.setScrollFlags(0);
    
        CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        appBarLayoutParams.setBehavior(null);
        appBarLayout.setLayoutParams(appBarLayoutParams);
    }
    
    public void turnOnTabLayoutScrolling() {
    
        //turn on scrolling
        AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mTabLayout.getLayoutParams();
        toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        mTabLayout.setLayoutParams(toolbarLayoutParams);
    
        CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
        appBarLayout.setLayoutParams(appBarLayoutParams);
    }
    
    如果您遇到问题,请遵循下面的指导线,在一个选项卡上链接完整的表格布局示例和地图


    这是根据瓦利德·萨瓦尔的答案改编的,我最终使用了这个答案

    有时,该应用程序需要几秒钟来正确地重新绘制地图,但它符合我的目的

    XML:

    • 我没有改变ViewPager中的布局行为
    • 我删除了TabLayout中的布局滚动标志
    xml文件:

    <android.support.design.widget.CoordinatorLayout 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.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:orientation="vertical">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                style="@style/TabLayoutTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="4dp"
                android:theme="@style/TabLayoutTheme"
                app:tabSelectedTextColor="@color/primary_text" />
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    看看我的回答这个方法对我不起作用,但是Angel的方法起作用了(使用支持库23.3.0)
    <android.support.design.widget.CoordinatorLayout 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.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:orientation="vertical">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                style="@style/TabLayoutTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="4dp"
                android:theme="@style/TabLayoutTheme"
                app:tabSelectedTextColor="@color/primary_text" />
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    private void turnTabLayoutScrolling(boolean onScrolling) {
            AppBarLayout.LayoutParams toolbarLayoutParams
                    = (AppBarLayout.LayoutParams) tabLayout.getLayoutParams(); 
    
            if (onScrolling) {
                toolbarLayoutParams.setScrollFlags(
                          AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                        | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    
            }else{
                toolbarLayoutParams.setScrollFlags(0);
                appbarLayout.setExpanded(true);
            }
    
            tabLayout.setLayoutParams(toolbarLayoutParams);
        }