我试图将导航视图和页面查看器结合起来。我的工作正常,我遇到的问题是将抽屉拨动开关添加到操作栏(或工具栏)以便调出导航抽屉。我不在乎我是否可以将切换添加到默认视图寻呼机或通过实现我自己的栏(您可以看到已注释掉的部分)。实际上我一次在屏幕上都有两个酒吧......这不是我想要的。我只是在试验看看有什么可能。任何帮助将不胜感激,并提前感谢。
PagerActivity:
package valdes.navigationviewmenu;
import android.app.ActionBar;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class KioskPagerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private ViewPager viewPager;
private Question [] questions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kiosk_pager);
//toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//ActionBar bar = getActionBar();
//bar.setDisplayHomeAsUpEnabled(true);
//bar.setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
questions = QuestionManager.getQuestions();
viewPager = (ViewPager) findViewById(R.id.activity_question_pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
@Override
public Fragment getItem(int position) {
return QuestionFragment.newInstance(position);
}
@Override
public int getCount() {
return questions.length;
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent intent;
switch (id){
case R.id.nav_camera:
intent = new Intent(this, Main2Activity.class);
startActivity(intent);
return true;
case R.id.nav_gallery:
intent = new Intent(this, KioskPagerActivity.class);
startActivity(intent);
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
对应的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_question_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- custom app bar that can be used.
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
-->
<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/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
我实际上通过以下更改解决了这个问题:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kiosk_pager);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_kiosk);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
navigationView = (NavigationView)findViewById(R.id.nav_view_kiosk);
navigationView.setNavigationItemSelectedListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
但是现在切换器没有拉出抽屉,抽屉出现在操作栏后面......帮助。