WearableDrawerLayout.DrawerStateCallback的onDrawerClosed未被调用(但onDrawerOpened被调用)

时间:2019-09-29 03:40:58

标签: android android-fragments wear-os drawerlayout

我正在为Wear OS构建一个独立的应用程序。我的问题是,从未调用过WearableDrawerLayout.DrawerStateCallback的{​​{1}}。因此,当用户切换抽屉中的某些设置然后关闭抽屉时,更改不会反映在活动片段(onDrawerClosed())上。 (此外,当我滑动关闭MainFragment时,屏幕上的活动片段不会调用onResume(),因此无法解决此问题。)

以下是我为WearableDrawerView设计的布局。 MainActivitymain_content都是片段。出于我的问题,我们分别称它们为drawer_contentMainFragment

DrawerFragment

在MainActivity中,我实现<?xml version="1.0" encoding="utf-8"?> <androidx.wear.widget.drawer.WearableDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/main_content" /> <androidx.wear.widget.drawer.WearableDrawerView android:id="@+id/bottom_action_drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom" android:background="@color/overlay_background" app:drawerContent="@+id/drawer_content" app:peekView="@+id/peek_view"> <FrameLayout android:id="@+id/drawer_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/peek_view" android:layout_width="wrap_content" android:layout_height="@dimen/navigation_peek_height" android:layout_gravity="center" android:gravity="center_vertical" android:orientation="vertical" android:paddingTop="@dimen/action_drawer_peek_top_padding"> <ImageView android:layout_width="@dimen/peek_view_icon_size" android:layout_height="@dimen/peek_view_icon_size" android:layout_marginTop="@dimen/action_drawer_expand_icon_top_margin" android:alpha="0.7" android:src="@drawable/ic_expand_less_white_22" /> </LinearLayout> </androidx.wear.widget.drawer.WearableDrawerView> </androidx.wear.widget.drawer.WearableDrawerLayout> 并以编程方式设置DrawerCallback.DrawerCallbackListener的自定义实现,这是一个名为DrawerStateCallback的类。

DrawerCallback
class MainActivity : FragmentActivity(), DrawerCallback.DrawerCallbackListener {

    override fun onCreate(savedInstanceState: Bundle?) {

        // some initialization stuff
        ...

        mWearableDrawerLayout = findViewById(R.id.drawer_layout)
        mWearableDrawerLayout.setDrawerStateCallback(DrawerCallback(this))
        mWearableActionDrawer =
            findViewById<WearableDrawerView>(R.id.bottom_action_drawer).apply {
                // Peeks action drawer on the bottom.
                controller.peekDrawer()
            }
    }
}

虽然在打开抽屉时调用class DrawerCallback(private val listener: DrawerCallbackListener) : WearableDrawerLayout.DrawerStateCallback() { override fun onDrawerStateChanged(layout: WearableDrawerLayout?, newState: Int) { super.onDrawerStateChanged(layout, newState) Log.d("onDrawerStateChanged") } override fun onDrawerClosed(layout: WearableDrawerLayout?, drawerView: WearableDrawerView?) { super.onDrawerClosed(layout, drawerView) Log.d("onDrawerClosed") // TODO: WHY IS THIS LINE NEVER GETTING HIT?? listener.onDrawerClosed() } override fun onDrawerOpened(layout: WearableDrawerLayout?, drawerView: WearableDrawerView?) { super.onDrawerOpened(layout, drawerView) Log.d("onDrawerOpened") listener.onDrawerOpened() } interface DrawerCallbackListener { fun onDrawerClosed() fun onDrawerOpened() } } ,但在关闭抽屉时不调用onDrawerOpened()onDrawerClosed()也被调用,但是onDrawerStateChanged()整数仅代表其当前的运动状态(空闲,拖动或稳定)。

如何获取newState来识别MainFragment对某些设置值(例如DrawerFragment)所做的更改并相应地更新其UI?设置值,例如colorScheme,是通过colorScheme在片段之间存储/共享的。

1 个答案:

答案 0 :(得分:0)

这不是最好的解决方案,但是我可以通过创建MainFragment中的设置来反映对设置的更改,并在数据中检测到更改时使用片段事务替换它。

之所以起作用,是因为片段初始化是指视图从设置中拉出以确定其外观的时候。尽管如果任一片段中都有需要保留的值,此解决方法都将不起作用。