完整的应用程序工作正常但是当涉及侧抽屉的过渡或我们可以说导航菜单它不顺利。它在打开和关闭时会造成延迟。 这是我的主要活动代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mHandler = new Handler();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navHeader = navigationView.getHeaderView(0);
activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);
setUpNavigationView();
if (savedInstanceState == null) {
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
}
}
private void loadHomeFragment() {
selectNavMenu();
setToolbarTitle();
if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
drawer.closeDrawers();
return;
}
Runnable mPendingRunnable = new Runnable() {
@Override
public void run() {
Fragment fragment = getHomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
fragmentTransaction.commitAllowingStateLoss();
}
};
if (mPendingRunnable != null) {
mHandler.post(mPendingRunnable);
}
drawer.closeDrawers();
invalidateOptionsMenu();
}
private Fragment getHomeFragment() {
switch (navItemIndex) {
case 0:
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
case 1:
HealthFragment healthFragment = new HealthFragment();
return healthFragment;
case 2:
PinFragment pinFragment = new PinFragment();
return pinFragment;
case 3:
SupportFragment supportFragment = new SupportFragment();
return supportFragment;
case 4:
FeedbackFragment feedbackFragment = new FeedbackFragment();
return feedbackFragment;
case 5:
FAQsFragment faQsFragment = new FAQsFragment();
return faQsFragment;
default:
return new HomeFragment();
}
}
private void setToolbarTitle() {
getSupportActionBar().setTitle(activityTitles[navItemIndex]);
}
private void selectNavMenu() {
navigationView.getMenu().getItem(navItemIndex).setChecked(true);
}
private void setUpNavigationView() {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_health:
navItemIndex = 1;
CURRENT_TAG = TAG_HEALTH;
break;
case R.id.nav_pin:
navItemIndex = 2;
CURRENT_TAG = TAG_PIN;
break;
case R.id.nav_support:
navItemIndex = 3;
CURRENT_TAG = TAG_SUPPORT;
break;
case R.id.nav_feedback:
navItemIndex = 4;
CURRENT_TAG = TAG_FEEDBACK;
break;
case R.id.nav_faqs:
navItemIndex = 5;
CURRENT_TAG = TAG_FAQS;
break;
case R.id.nav_chat:
startActivity(new Intent(MainActivity.this, LoginActivity.class));
drawer.closeDrawers();
return true;
case R.id.nav_rate:
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.sega.sonicdash");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=com.sega.sonicdash")));
}
break;
case R.id.nav_aboutus:
startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
drawer.closeDrawers();
return true;
case R.id.nav_share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
drawer.closeDrawers();
return true;
case R.id.nav_privacy_policy:
startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
drawer.closeDrawers();
return true;
default:
navItemIndex = 0;
}
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
menuItem.setChecked(true);
loadHomeFragment();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
/* My edits */
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawer.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawers();
return;
}
if (shouldLoadHomeFragOnBackPress) {
if (navItemIndex != 0) {
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
return;
}
}
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (navItemIndex == 0) {
getMenuInflater().inflate(R.menu.main, menu);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
我正在导航菜单中切换片段。如何使这些菜单的转换顺利进出。
答案 0 :(得分:0)
在动画期间,不要让应用程序过载,提前或动画之后都要努力工作。
例如:在用户选择所需的片段后,您首先关闭抽屉,并在动画结束后启动片段事务。 如果你知道在关闭之后,交易应该开始,可以加速结束动画。