工具栏和导航就可以了就可以、工具栏

由网友(叼花硬汉)分享简介:该项目的工具栏的活动。活动在动态变化的片段。根据不同的片段上应该改变内容的工具栏。至于第二个片段应该出现在工具条nazat箭头返回到previous片段The project has the activity of the toolbar. Activity in dynamically changing frag...

该项目的工具栏的活动。活动在动态变化的片段。根据不同的片段上应该改变内容的工具栏。 至于第二个片段应该出现在工具条nazat箭头返回到previous片段

The project has the activity of the toolbar. Activity in dynamically changing fragments. Depending on the fragment ought to change content toolbar. Turning to the second fragment ought to appear in the toolbar nazat arrow which returns to the previous fragment.

public class StartPageActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar mToolbar;
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_page_activity);
    setTitle("ForgetFul");
    getFragment(new MainFragment());
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mNavigationView = (NavigationView) findViewById(R.id.main_drawer);
    mNavigationView.setNavigationItemSelectedListener(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_widget);
    drawerToggle
            = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
    mDrawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@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) {
        Log.i("Activity", "Setting");
        return true;
    }
    if (android.R.id.home == id) {
        Log.i("One", "Dude");
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}...

在从一开始就显示MainFragment活动。 在第二个活动,我用下面的code:

In the active from the start displayed MainFragment. In the second activity, I use the following code:

((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

向上箭头出现,但是当我点击箭头,所有的时间打开NavigationView。 如何解决呢?     附:对不起,英文:(

"Up" Arrow appeared, but when I click on arrow, all time open the NavigationView. how to fix it? P.S. Sorry for English :(

推荐答案

试试这个。 首先,删除实现NavigationView.OnNavigationItemSelectedListener ,也注释掉overidden方法和变量。

Try this. First, remove implements NavigationView.OnNavigationItemSelectedListener , and also comment out the overidden methods and variables.

然后在此改变活动的onBack pressed()方法是这样的:

Then overide the Activity's onBackPressed() method like this:

    @Override
public void onBackPressed() {
    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager.getBackStackEntryCount() != 0) {
        fragmentManager.popBackStack();
    } else {
        super.onBackPressed();
    }
}

这将确保在previous片段越来越显示在pressing返回键。

This will make sure that the previous fragments are getting displayed on pressing the Back key.

然后,只需调用它onOptionsItemSelected()方法:

Then, simply call it in onOptionsItemSelected() method:

    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }
阅读全文

相关推荐

最新文章