您好我尝试使用本教程在Android中制作NavigationDrawer,但我不想改变抽屉打开的方式。教程是这样的: http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
在本教程中,工具栏中的默认主页按钮打开抽屉我想用菜单项打开它。
这是我的主要代码:
public class First_page extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
Button go_layout;
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.page_logo_2);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
go_layout = (Button)findViewById(R.id.go_to_page);
go_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(First_page.this, Full_page.class);
startActivity(intent);
}
});
}
@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.right_btn) {
Toast.makeText(First_page.this, "Right_btn clicked", Toast.LENGTH_LONG).show();
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
}
}
答案 0 :(得分:1)
以下是我处理NavigationDrawer打开和关闭的方法。只需调用handleNavDrawer(),您可以在其中打开或关闭它。
处理打开和关闭抽屉
public void handleNavDrawer() {
if (mNavigationDrawer.isDrawerOpen(GravityCompat.START)) {
mNavigationDrawer.closeDrawer(GravityCompat.START);
} else {
mNavigationDrawer.openDrawer(GravityCompat.START);
}
}
工作示例
public class MainActivity extends AppCompatActivity {
private DrawerLayout mNavigationDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracker);
mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
handleNavDrawer();
return true;
}
return super.onOptionsItemSelected(item);
}
public void handleNavDrawer() {
if (mNavigationDrawer.isDrawerOpen(GravityCompat.START)) {
mNavigationDrawer.closeDrawer(GravityCompat.START);
} else {
mNavigationDrawer.openDrawer(GravityCompat.START);
}
}
}
使用您的示例 更新
public class First_page extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
Button go_layout;
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
private DrawerLayout mNavigationDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.page_logo_2);
go_layout = (Button)findViewById(R.id.go_to_page);
go_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(First_page.this, Full_page.class);
startActivity(intent);
}
});
mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, mNavigationDrawer, toolbar);
drawerFragment.setDrawerListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.right_btn) {
Toast.makeText(First_page.this, "Right_btn clicked", Toast.LENGTH_LONG).show();
handleNavDrawer();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
// Close the Nav Drawer
// handleNavDrawer();
}
public void handleNavDrawer() {
if (mNavigationDrawer.isDrawerOpen(GravityCompat.START)) {
mNavigationDrawer.closeDrawer(GravityCompat.START);
} else {
mNavigationDrawer.openDrawer(GravityCompat.START);
}
}
}
答案 1 :(得分:1)
我的方法是名为“btnClickSideBar”的onClick。你可以随意调用它。 当您点击它时,它将检查抽屉是打开还是关闭。如果它是打开我们的方法它将关闭,如果它是关闭,抽屉将打开。
public void btnClickSideBar(View view) {
if (drawerLayout.isDrawerOpen(drawerContainer)) {
drawerLayout.closeDrawers();
} else {
drawerLayout.openDrawer(drawerContainer);
}
}