我在我的应用程序中使用Navigation Drawer
。
当用户点击抽屉中的任何菜单项时,它会打开一个新的Activity
(不是片段)。
现在,我正在使用slide_right_in/slide_left_out animation
作为活动之间的转换。
代码有效,但这些动画与导航抽屉的关闭动画相冲突,因为即使在抽屉完全关闭之前,当前活动也开始滑向左边和左边。下一个活动开始从右边滑入。
那么,有没有办法在抽屉完全关闭后启动动画?
谢谢
答案 0 :(得分:12)
您可以延迟打开Activity
。例如,以这种方式Activity
将在250ms后启动:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(<filter>);
startActivity(intent);
finish();
}
}, 250);
mDrawerLayout.closeDrawer(mDrawerList);
答案 1 :(得分:5)
我这样做的方式与Jan相似。
如果点击了某个项目,我会保存其ID并关闭抽屉:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
clickedItem = menuItem.getItemId();
drawerLayout.closeDrawers();
return true;
}
});
如果抽屉关闭,我会听取它并检查是否已点击某个项目。如果它我调用我的方法来处理导航点击。
drawerToggle = new ActionBarDrawerToggle(activity, drawerLayout, R.string.accessibility_open_nav, R.string.accessibility_open_nav)
{
@Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
if(clickedItem != 0)
{
handleNavigationClick();
}
}
};
drawerLayout.setDrawerListener(drawerToggle);
在这种情况下,我会对项目做出反应点击打开的意图(简化)。你需要将clickedItem重置为0.也就是说,如果你回到一个活动,打开和关闭抽屉,它仍然会有clickedItem编号,并会再次处理点击。
private void handleItemClick()
{
switch (clickedItem)
{
case R.id.item_1:
do_something_1;
break;
case R.id.item_2:
do_something_2;
break;
}
clickedItem = 0;
}
答案 2 :(得分:1)
不要关闭导航抽屉。它将随旧活动滑动。或者在startActivity
drawerLayout.closeDrawer(drawerList);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
drawerLayout.closeDrawer(drawerList);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//set animation here
startActivity(intent);
finish();
}
答案 3 :(得分:1)
每个答案都是如此复杂......这很容易。
只需添加一个drawerlistener并在onClosed()方法中执行一些操作:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
当您从导航栏中选择一个项目时,您将调用此方法关闭抽屉:
drawer.closeDrawer(GravityCompat.START);
在上面的方法调用后,只需添加以下行并执行任何操作:
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
startActivity(finalIntent);
// Or else do something here....
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
答案 4 :(得分:0)
nikis的答案并未涵盖所有案例。如果您的片段或活动包含地图或CameraView,则可能需要更多时间。简单的屏幕和屏幕之间的差异与地图太大,因此难以选择良好的延迟。
最好的方法是从onResume()中刚刚打开的Activity / Fragment向NavigationDrawer发送回调。然后在此回调中关闭抽屉。
答案 5 :(得分:0)
我将所有活动扩展到我希望通过DrawerActivity类创建导航抽屉的地方。因为我也有一点动画冲突所以我在导航抽屉完全关闭后开始我的活动。这是涵盖所有情况的实施:
public class DrawerActivity extends AppCompatActivity {
private String[] mMenuTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
private Boolean isItemClicked = false;
protected void onCreateDrawer() {
mTitle = getResources().getString(R.string.app_name);
mMenuTitles = new String[]{getString(R.string.drawer_item1), getString(R.string.drawer_item2), getString(R.string.drawer_item3)};
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(R.color.black_transparent_30));
// mDrawerLayout.setScrimColor(Color.TRANSPARENT);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mMenuTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
// R.mipmap.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
如果点击了项目,我会在检查位置开始活动。
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
if (isItemClicked) {
int position = mDrawerList.getCheckedItemPosition();
startMyActivity(position);
isItemClicked = false;
}
方法setCheckedItem在扩展活动中被覆盖,以便一致地显示已检查项目。
setCheckedItem(mDrawerList);
// getSupportActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// getSupportActionBar().setTitle(mTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
public void setCheckedItem(ListView mDrawerList) {
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setCheckedItem(mDrawerList);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void selectItem(int position) {
mDrawerList.setItemChecked(position, true);
// setTitle(mMenuTitles[position]);
// Toast.makeText(this, mTitle, Toast.LENGTH_SHORT).show();
// startMyActivity(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
public void startMyActivity(int position) {
//is overriden in extended activities for being able to send data to another activity
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
单击项目,因此我将帮助器属性设置为true。
public void onItemClick(AdapterView parent, View view, int position, long id) {
isItemClicked = true;
selectItem(position);
}
}
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}}
答案 6 :(得分:0)
我找到了一种简单的方法来摆脱你可能会尝试这个以及它的工作和测试。
OLD ANSWER:
int id = -1;
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
if (id == R.id.NAV_YOUR_ACTIVITY_ONE) {
Intent activityIntent = new Intent(getApplicationContext(), YOUR_ACTIVITY_ONE.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
}
else if (id == R.id.NAV_YOUR_ACTIVITY_TWO) {
Intent activityIntent = new Intent(getApplicationContext(), YOUR_ACTIVITY_TWO.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
}
//do not forget to set id again to -1 or else it will cause problem
id=-1;
}
@Override
public void onDrawerStateChanged(int newState) {
}
}
}
更新答案:
您也可以使用ActionBarDrawerToggle,例如观察下面的代码。
int id = -1;
//Use this function for click effect be performed on drawer item clicked
public void perfromDrawerNavigation(){
if (id == R.id.NAV_YOUR_ACTIVITY_ONE) {
Intent activityIntent = new Intent(getApplicationContext(), YOUR_ACTIVITY_ONE.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
}
else if (id == R.id.NAV_YOUR_ACTIVITY_TWO) {
Intent activityIntent = new Intent(getApplicationContext(), YOUR_ACTIVITY_TWO.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
}
//do not forget to set id again to -1 or else it will cause problem
id=-1;
}
//you can write this code in onCreate() also
public void initControl(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(
getResources().getString(R.string.activity));
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if(id != -1){
perfromDrawerNavigation();
}
}
};
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
id = item.getItemId();
drawer.closeDrawer(GravityCompat.START);
}