打开时的抽屉项目动画

时间:2015-10-28 09:38:13

标签: android navigation-drawer android-recyclerview layout-animation

我使用DrawerLayout创建了一个抽屉,其中包含带有项目的RecyclerView。我还将一个layoutAnimation附加到RecyclerView,以便在打开抽屉时让物品从侧面进入。这是第一次使用peachy,但是当第二次打开抽屉时,一切都已经到位。我希望每次打开抽屉时都会运行layoutAnimation。

我到目前为止尝试过的是自定义ActionBarDrawerToggle(我还需要那个),并添加以下内容:

@Override
public void onDrawerOpened(final View drawerView) {
    super.onDrawerOpened(drawerView);
    final RecyclerView recyclerView =
            (RecyclerView) drawerView.findViewById(R.id.drawer_content);
    if (recyclerView != null) {
        recyclerView.startLayoutAnimation();
    }
}

它有点像,因为它会重新运行动画,但打开抽屉时所有项目都在那里,然后它们消失,然后动画开始。

任何人都有解决方法如何重置"每次抽屉关闭时抽屉物品都会查看?

不确定是否需要这些,但无论如何我都会包含它们

<--! layout_animation.xml -->
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/slide_from_right"
    android:delay="15%"
    android:animationOrder="normal"
    />

<--! slide_from_right.xml -->
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toXDelta="0"
    />

2 个答案:

答案 0 :(得分:1)

经过一些测试后我找到了解决方案,也许不是最漂亮的解决方案,但它有效。通过在抽屉关闭时隐藏内容,然后在开始动画之前再次使其可见,解决了我遇到的问题:

private boolean mFirstDrawerOpen = true;
private boolean mAnimationScheduled;

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
    super.onDrawerSlide(drawerView, slideOffset);

    // The framework handles the first animation
    if (mIsFirstDrawerOpen) {
        mIsFirstDrawerOpen = false;
        return;
    }

    final RecyclerView recyclerView =
            (RecyclerView) drawerView.findViewById(R.id.drawer_content);

    if (mAnimationScheduled && recyclerView != null) {
        recyclerView.setVisibility(View.VISIBLE);
        recyclerView.startLayoutAnimation();
        mAnimationScheduled = false;
    } else if (slideOffset == 0f) {
        // Handles the case when the drawer is not completly opened and then closed,
        // which does not trigger onDrawerClosed()
        mAnimationScheduled = true;
    }
}

@Override
public void onDrawerOpened(final View drawerView) {
    super.onDrawerOpened(drawerView);

    mAnimationScheduled = false;
}

@Override
public void onDrawerClosed(View drawerView) {
    super.onDrawerClosed(drawerView);

    mAnimationScheduled = true;        

    final RecyclerView recyclerView =
            (RecyclerView) drawerView.findViewById(R.id.drawer_content);
    if (recyclerView != null) {
        recyclerView.setVisibility(View.INVISIBLE);
    }
}

<强>更新 之前的答案没有处理抽屉被拖到中途然后关闭的情况,因为如果抽屉没有完全打开,则不会调用onDrawerClosed。为了解决这个问题,我将大部分代码从onDrawerOpen移到了onDrawerSlide()并对其进行了一些修改。

答案 1 :(得分:0)

我遇到了与@ patrick-iv相同的问题,并想知道其他人是如何解决它的。我想出了以下代码添加到onDrawerStateChanged监听器。

boolean drawerOpen = drawer.isDrawerOpen(GravityCompat.START);
_drawerTopMenu.setVisibility(drawerOpen ? View.VISIBLE : View.INVISIBLE);

if (newState == DrawerLayout.STATE_SETTLING && !drawerOpen)
    _drawerTopMenu.startLayoutAnimation();