我试图用2个片段(稍后我将添加)和4个活动做导航抽屉。在我的代码中,当我调用活动并关闭。它非常迟钝。我希望它非常顺利。请看我的代码。
ublic class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navigationView;
FragmentManager fragmentManager;
private String[] pageTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageTitle = new String[]{getString(R.string.frag_home),
getString(R.string.nav_introduction),};
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle(pageTitle[0]);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView)findViewById(R.id.nav_view);
// assert navigationView != null: Log.wtf("haha", "OMG");
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_introduction: {
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_Introduction()).commit();
}
break;
case R.id.nav_settings: {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
} break;
case R.id.nav_feedback: {
Intent intent = new Intent(MainActivity.this, FeedbackActivity.class);
startActivity(intent);
}
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
assert drawerLayout != null;
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
public void onClickHome(View view){
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
}
public void onClickFilter(View view){
Intent intent = new Intent(MainActivity.this, FIlterActivity.class);
startActivity(intent);
}
}
我的主要活动代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:titleTextColor="@android:color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">
</LinearLayout>
<TextView
android:text=""
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:gravity="right|center"
android:layout_weight="1"
android:onClick="onClickHome"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="3dp"
android:gravity="center"
android:src="@drawable/filter_icon"
android:onClick="onClickFilter"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
就像我使用Activity和Fragment一样,它非常迟钝。还有其他方法可以顺利吗?
解决方案:我使用了处理程序。但这并不是最好的。 case R.id.nav_settings:{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
}, 250);
} break;