简体   繁体   English

Android的两个导航抽屉动画

[英]android two navigation drawer animation

I'm trying to make two navigation drawers in my app. 我正在尝试在我的应用中制作两个导航抽屉。 It is based on DrawerLayout Double Drawer (Left and Right Drawers simultaneously) . 它基于DrawerLayout Double Drawer(同时包含左右抽屉) Everything seems to work fine apart from the animation of the drawer toggle. 除了抽屉开关的动画效果之外,其他所有功能似乎都可以正常工作。 I want it only for the left one and it only works for both or none of them. 我只想要左边的那个,它只对两个都起作用,或者对它们都不起作用。 Thanks a lot! 非常感谢!

Here's the code: 这是代码:

MainActivity (ProductsUI) MainActivity(ProductsUI)

protected RecyclerView mProductsRecyclerView;
protected SearchView mSearchView;
protected ListView mRightDrawerView, mLeftDrawerView;
protected DrawerLayout mDrawerLayout;
protected ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate( Bundle savedInstanceState )
{
    super.onCreate(savedInstanceState);

    // Especificamos el layout 'products_grid.xml'
    setContentView( R.layout.products_grid );

    initActionBar();
    initRecyclerView();
    initNavigationDrawers();
}
private void initActionBar()
{
    // Cargamos la action bar personalizada
    getSupportActionBar().setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
    getSupportActionBar().setCustomView(R.layout.action_bar);

    // Cargamos el boton del left drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}
private void initNavigationDrawers()
{
    mRightDrawerView = ( ListView )findViewById( R.id.rightlistviewdrawer );
    mLeftDrawerView  = ( ListView )findViewById( R.id.leftlistviewdrawer );
    mDrawerLayout    = ( DrawerLayout )findViewById( R.id.drawer_layout );

    mRightDrawerView.setAdapter( menuAdapter );
    mLeftDrawerView.setAdapter( menuAdapter );

    initDrawerToggle();

    mDrawerLayout.setDrawerListener( mDrawerToggle );
}
private void initDrawerToggle()
{
    // Inicializamos el navigation drawer y el control en la action bar
    mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer )
    {
        // Called when a drawer has settled in a completely closed state
        @Override
        public void onDrawerClosed( View drawerView )
        {
            if( drawerView.equals( mLeftDrawerView ) )
            {
                getSupportActionBar().setTitle( getTitle() );
                supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                mDrawerToggle.syncState();
            }
        }

        // Called when a drawer has settled in a completely open state
        @Override
        public void onDrawerOpened( View drawerView )
        {
            if( drawerView.equals( mLeftDrawerView ) )
            {
                getSupportActionBar().setTitle( getString( R.string.app_name ) );
                supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                mDrawerToggle.syncState();
            }
        }

        // Avoid normal indicator glyph behaviour. This is to avoid glyph movement when opening the right drawer
        @Override
        public void onDrawerSlide( View drawerView, float slideOffset )
        {
            if( drawerView == mLeftDrawerView ) // THIS DOES NOT WORK (neither with equals())
                super.onDrawerSlide( drawerView, slideOffset );
        }
    };
}
@Override
protected void onPostCreate( Bundle savedInstanceState )
{
    super.onPostCreate( savedInstanceState );
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged( Configuration newConfig )
{
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
    if ( mDrawerToggle.onOptionsItemSelected( item ) )
        return true;

    // Funcionamiento del right drawer
    if ( item.getItemId() == R.id.right_drawer ) {
        if ( mDrawerLayout.isDrawerOpen( Gravity.RIGHT ) )
            mDrawerLayout.closeDrawer( Gravity.RIGHT );

        else
            mDrawerLayout.openDrawer( Gravity.RIGHT );

        return true;
    }

    return super.onOptionsItemSelected( item );
}

And here's the products_grid.xml 这是products_grid.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ProductsUI"
        android:background="@color/backgroundColorDark">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/grid_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="20dp"
            android:scrollbars="none"/>

    </LinearLayout>

    <include layout="@layout/right_navigation_drawer"/>

    <include layout="@layout/left_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

Here's the right navigation drawer.xml 这是正确的导航抽屉.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layout_gravity="end">

    <ListView
        android:id="@+id/rightlistviewdrawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:dividerHeight="0dp"
        android:background="@color/backgroundColorDark"/>

</LinearLayout> 

And finally the left navigation drawer 最后是左侧导航抽屉

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layout_gravity="start">

    <ListView
        android:id="@+id/leftlistviewdrawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:dividerHeight="0dp"
        android:background="@color/backgroundColorDark"/>

</LinearLayout>

Your if statement is not working as you expect because the root View s in your included drawer layouts are LinearLayout s, whereas the View s you're finding and assigning to mLeftDrawerView and mRightDrawerView in your code are the ListView s. if语句不是您正常工作,因为根View在你的抽屉里包含布局LinearLayout S,而View是你正在寻找和分配给mLeftDrawerViewmRightDrawerView在你的代码是ListView秒。 The drawerView in the various listener methods will be the root View of the included drawer layouts; drawerView在各种监听方法将是根View所包括的抽屉布局; ie, the LinearLayout s. LinearLayout This means that drawerView == mLeftDrawerView will never be true. 这意味着drawerView == mLeftDrawerView将永远不会为真。

The solution is to assign IDs to the LinearLayout in the XML, find those View s in the code, and assign those to mLeftDrawerView and mRightDrawerView . 解决方案是将ID分配给XML中的LinearLayout ,在代码中找到那些View ,然后将其分配给mLeftDrawerViewmRightDrawerView

For example, your left drawer layout: 例如,您的左抽屉布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/leftdrawer"
    ...
    >

    <ListView
        android:id="@+id/leftlistviewdrawer"
        ...
        />

</LinearLayout>

You would do the same with the right drawer layout. 您将使用正确的抽屉布局执行相同的操作。 Then, in the initialization: 然后,在初始化中:

private void initNavigationDrawers() {
    mRightDrawerView = ( LinearLayout )findViewById( R.id.rightdrawer );
    mLeftDrawerView  = ( LinearLayout )findViewById( R.id.leftdrawer );
    mRightDrawerListView = ( ListView )findViewById( R.id.rightlistviewdrawer );
    mLeftDrawerListView  = ( ListView )findViewById( R.id.leftlistviewdrawer );

    mDrawerLayout = ( DrawerLayout )findViewById( R.id.drawer_layout );

    mRightDrawerListView.setAdapter( menuAdapter );
    mLeftDrawerListView.setAdapter( menuAdapter );

    initDrawerToggle();

    mDrawerLayout.setDrawerListener( mDrawerToggle );
}

And you would also need to change the declared type for mLeftDrawerView and mRightDrawerView to LinearLayout . 而且,您还需要将mLeftDrawerViewmRightDrawerView的声明类型更改为LinearLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM