Android可穿戴设备:2.0.5打破抽屉布局

时间:2017-09-19 13:10:04

标签: android android-layout android-studio android-fragments wear-os

我的Android应用中有一个可穿戴的抽屉布局。行为是窥视视图将显示窥视片段,当用户向上滑动时,窥视片段将淡入内容片段。

我最近尝试将我的android服装项目从可穿戴式2.0.3更新为可穿戴式2.0.5。需要将许多组件从 wearable.view 更改为 wear.widget

更新后,两个片段都会显示抽屉打开或关闭的天气。我尝试手动淡入淡出这些,但似乎没有办法顺利或轻松地做到这一点。先前的行为是否可能?我不得不重新安排一些事情来让它发挥作用。我应该使用不同的格式来获得我之前的行为吗?谢谢!

上一个抽屉布局:

<android.support.wearable.view.drawer.WearableActionDrawer
    android:id="@+id/bottom_action_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorPrimary"
    app:drawer_content="@+id/nowPlayingFragment">
    <!-- Peek View -->
    <fragment
        android:id="@+id/peekFragment"
        android:name="com.turndapage.navmusic.ui.fragment.PeekFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/nowPlayingFragment"
        android:name="com.turndapage.navmusic.ui.fragment.NowPlayingFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.wearable.view.drawer.WearableActionDrawer>

新抽屉布局:

<android.support.wear.widget.drawer.WearableActionDrawerView
    android:id="@+id/bottom_action_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorPrimary"
    app:drawer_content="@+id/nowPlayingFragment"
    app:peekView="@+id/peek_view">


    <fragment
        android:id="@+id/nowPlayingFragment"
        android:name="com.turndapage.navmusic.ui.fragment.NowPlayingFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- Peek View -->
    <fragment
        android:id="@+id/peekFragment"
        android:name="com.turndapage.navmusic.ui.fragment.PeekFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.wear.widget.drawer.WearableActionDrawerView>

1 个答案:

答案 0 :(得分:0)

从来没有找到我真正喜欢的解决方案,但这里有一个有用的解决方案。

wearableDrawerLayout.setDrawerStateCallback(new WearableDrawerLayout.DrawerStateCallback() {

        @Override
        public void onDrawerStateChanged(WearableDrawerLayout layout, int newState) {
            super.onDrawerStateChanged(layout, newState);
            if(nowPlayingUtil != null) {
                // If the drawer is settling into position and it was opened all the way.
                if (wearableActionDrawer.getDrawerState() == WearableDrawerView.STATE_SETTLING && nowPlayingUtil != null) {
                    // Transitioning from peek view
                    if (drawerState == "drawerPeeking")
                        nowPlayingUtil.fadeToOpen();
                    // Transitioning from open view
                    else if (drawerState == "drawerOpen")
                        nowPlayingUtil.fadeToPeek();
                    else if (drawerState == "drawerClosed")
                        nowPlayingUtil.fadeToPeek();
                }
                if (wearableActionDrawer.getDrawerState() == WearableDrawerView.STATE_IDLE) {
                    // update the previous State
                    if (wearableActionDrawer.isOpened()) {
                        drawerState = "drawerOpen";
                        nowPlayingUtil.fadeToOpen();
                    } else if (wearableActionDrawer.isPeeking()) {
                        drawerState = "drawerPeeking";
                        nowPlayingUtil.fadeToPeek();
                    } else if (wearableActionDrawer.isClosed()) {
                        drawerState = "drawerClosed";
                        nowPlayingUtil.fadeToPeek();
                    }
                }
            }
        }
    });

所以在我的例子中,我有两个函数,一个用于fadeToOpen,另一个用于fadeToPeek,它将为两个视图设置动画以使它们可见或不可见。我可以得到同样的效果。

仍然感兴趣,如果有内置的方法来做到这一点,但现在这样做。