Android 可见性消失时appBar仍占用空间
我使用ViewPager2实现了表格布局,但当我试图在第一个选项卡中隐藏应用程序栏时,它仍然占用了它的空间 我不知道为什么在使用下面的代码时会占用空间,但是如果我只是简单地使用appBar.visibility=View.GONE超出此页面更改回调,它就可以正常工作Android 可见性消失时appBar仍占用空间,android,android-viewpager2,Android,Android Viewpager2,我使用ViewPager2实现了表格布局,但当我试图在第一个选项卡中隐藏应用程序栏时,它仍然占用了它的空间 我不知道为什么在使用下面的代码时会占用空间,但是如果我只是简单地使用appBar.visibility=View.GONE超出此页面更改回调,它就可以正常工作 viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
when (position) {
0 -> {
appBar.visibility = View.GONE
}
else -> {
appBar.visibility = View.VISIBLE
}
}
}
})
XML文件
<?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>
如果您有任何建议,我们将不胜感激。您是否尝试过在真实的设备上运行它?是的,我尝试过在真实的设备上运行。我认为这是因为viewpager一次至少加载3页。。因此,它也可能在第0个position语句之后触发else语句。。请尝试在第3页或第4页中查看。可见,而不是简单地使用else。已尝试但无法工作