appBar still taking space while visibility is gone

202 views Asked by At

I used ViewPager2 to implement tablayout but when i try to hide the app Bar in the first tab it still takes its space.

Don't know why it takes space while using the code below but if i simply use appBar.visibility = View.GONE outside this resisterOnPageChangeCallback it works fine

viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
        when (position) {
            0 -> {
                appBar.visibility = View.GONE
            }
            else -> {
                appBar.visibility = View.VISIBLE
            }
        }
    }
})

XML FILE

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabSelectedTextColor="@color/colorAccent"
            >
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />
        </com.google.android.material.tabs.TabLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

enter image description here

Any suggestion will be appreciated

0

There are 0 answers