android 两个导航抽屉动画
android two navigation drawer animation
我正在尝试在我的应用程序中制作两个导航抽屉。它基于 DrawerLayout Double Drawer (Left and Right Drawers simultaneously)。除了抽屉切换的动画之外,一切似乎都运行良好。我只想要左边的那个,它只对两个或 none 个都有效。非常感谢!
代码如下:
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 );
}
这是 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>
这里是右导航drawer.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>
最后是左侧导航抽屉
<?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>
您的 if
语句没有像您预期的那样工作,因为您包含的抽屉布局中的根 View
是 LinearLayout
,而您的 View
在您的代码中重新查找并分配给 mLeftDrawerView
和 mRightDrawerView
的是 ListView
。各种侦听器方法中的 drawerView
将是包含的抽屉布局的根 View
;即 LinearLayout
s。这意味着 drawerView == mLeftDrawerView
永远不会为真。
解决方法是给[=40=中的LinearLayout
赋值,在代码中找到那些View
,赋值给mLeftDrawerView
和mRightDrawerView
.
例如,您的左侧抽屉布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/leftdrawer"
...
>
<ListView
android:id="@+id/leftlistviewdrawer"
...
/>
</LinearLayout>
您可以对右侧的抽屉布局执行相同的操作。然后,在初始化中:
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 );
}
并且您还需要将 mLeftDrawerView
和 mRightDrawerView
的声明类型更改为 LinearLayout
。
我正在尝试在我的应用程序中制作两个导航抽屉。它基于 DrawerLayout Double Drawer (Left and Right Drawers simultaneously)。除了抽屉切换的动画之外,一切似乎都运行良好。我只想要左边的那个,它只对两个或 none 个都有效。非常感谢!
代码如下:
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 );
}
这是 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>
这里是右导航drawer.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>
最后是左侧导航抽屉
<?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>
您的 if
语句没有像您预期的那样工作,因为您包含的抽屉布局中的根 View
是 LinearLayout
,而您的 View
在您的代码中重新查找并分配给 mLeftDrawerView
和 mRightDrawerView
的是 ListView
。各种侦听器方法中的 drawerView
将是包含的抽屉布局的根 View
;即 LinearLayout
s。这意味着 drawerView == mLeftDrawerView
永远不会为真。
解决方法是给[=40=中的LinearLayout
赋值,在代码中找到那些View
,赋值给mLeftDrawerView
和mRightDrawerView
.
例如,您的左侧抽屉布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/leftdrawer"
...
>
<ListView
android:id="@+id/leftlistviewdrawer"
...
/>
</LinearLayout>
您可以对右侧的抽屉布局执行相同的操作。然后,在初始化中:
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 );
}
并且您还需要将 mLeftDrawerView
和 mRightDrawerView
的声明类型更改为 LinearLayout
。