Skip to content

Commit

Permalink
feat: 调整示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaocydx committed Dec 29, 2023
1 parent 526556d commit 6c2746a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.updatePadding
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
Expand All @@ -23,7 +23,7 @@ import com.xiaocydx.insets.systembar.SystemBar
* 在[ViewPager2]场景下使用[SystemBar]:
* 1. [FragmentStateAdapter]构建的[Fragment]不要实现[SystemBar]。
* 2. [ViewPager2]所在的[FragmentActivity]或[Fragment]实现[SystemBar]。
* 3. 选中[Fragment]或者滚动停止时,应用`ViewPager2.currentItem`对应的配置
* 3. 选中[Fragment]或滚动停止时,设置`ViewPager2.currentItem`对应的window属性
*
* @author xcc
* @date 2023/12/29
Expand All @@ -32,6 +32,7 @@ class SystemBarVp2Activity : AppCompatActivity(), SystemBar, SystemBar.Host {
private val controller = systemBarController {
// PageFragment自行处理状态栏Insets
statusBarEdgeToEdge = EdgeToEdge.Enabled
navigationBarColor = 0xFF5E79B5.toInt()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -46,8 +47,7 @@ class SystemBarVp2Activity : AppCompatActivity(), SystemBar, SystemBar.Host {
viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
val page = pageAdapter.pages[position]
controller.navigationBarColor = page.navigationBarColor
controller.isAppearanceLightNavigationBar = page.isAppearanceLightNavigationBar
controller.isAppearanceLightStatusBar = page.isAppearanceLightStatusBar
}
})
}.root
Expand All @@ -66,8 +66,8 @@ class SystemBarVp2Activity : AppCompatActivity(), SystemBar, SystemBar.Host {
}

enum class Page(
val navigationBarColor: Int,
val isAppearanceLightNavigationBar: Boolean
val statusBarColor: Int,
val isAppearanceLightStatusBar: Boolean
) {
A(0xFFBABBC4.toInt(), true),
B(0xFF496291.toInt(), false),
Expand All @@ -81,10 +81,12 @@ class PageFragment : BaseFragment() {
val page = Page.values()[ordinal]
root.setBackgroundColor(0xFFAABBC4.toInt())
root.onClick { requireActivity().addFragment<NotPageFragment>() }
root.doOnApplyWindowInsets { view, insets, initialState ->
view.updatePadding(top = initialState.paddings.top + insets.statusBarHeight)
}
tvCenter.text = "PageFragment${page.name}\n\n点击添加NotPageFragment"
statusBar.setBackgroundColor(page.statusBarColor)
statusBar.doOnApplyWindowInsets { view, insets, initialState ->
val height = initialState.params.height + insets.statusBarHeight
if (height != view.layoutParams.height) view.updateLayoutParams { this.height = height }
}
}

companion object {
Expand Down
71 changes: 44 additions & 27 deletions app/src/main/res/layout/layout_base.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/tvTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:gravity="center"
android:text="top"
android:textSize="18sp" />
<View
android:id="@+id/statusBar"
android:layout_width="match_parent"
android:layout_height="0dp" />

<TextView
android:id="@+id/tvCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="center"
android:textSize="20sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<TextView
android:id="@+id/tvBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:gravity="center"
android:text="bottom"
android:textSize="18sp" />
</FrameLayout>
<TextView
android:id="@+id/tvTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:gravity="center"
android:text="top"
android:textSize="18sp" />

<TextView
android:id="@+id/tvCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="center"
android:textSize="20sp" />

<TextView
android:id="@+id/tvBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:gravity="center"
android:text="bottom"
android:textSize="18sp" />
</FrameLayout>

<View
android:id="@+id/navigationBar"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

0 comments on commit 6c2746a

Please sign in to comment.