尝试这样,它会工作,但我的导航抽屉类将在同一个活动页面本身。
public class Alerts extends ActionBarActivity
implements OnItemClickListener
{
//for Navigation Bar
DrawerLayout drawerLayout;
ListView listView;
String[] drawerlist;
ActionBarDrawerToggle drawerListener;
private MyAdapter4 myAdapter4;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alerts);
//navigation initialize
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.drawerList);
drawerlist = getResources().getStringArray(R.array.drawerlist);
myAdapter4 = new MyAdapter4(this);
listView.setAdapter(myAdapter4);
//for navigation click
listView.setOnItemClickListener(new DrawerItemClickListener());
//Navigation Bar starts
drawerListener = new ActionBarDrawerToggle
(this, drawerLayout, R.drawable.ic_drawer,
R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/
}
//for Action Bar Back Button
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
// Creating the Navigation Bar
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(drawerListener.onOptionsItemSelected(item))
{
return super.onOptionsItemSelected(item);
}
switch(item.getItemId()) {
case R.id.Add:
Intent addIntent = new Intent(this, Add.class);
startActivity(addIntent);
break;
case R.id.help:
Intent helpIntent = new Intent(this, Help.class);
startActivity(helpIntent);
break;
} return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testmenu, menu);
return super.onCreateOptionsMenu(menu);
}
class MyAdapter4 extends BaseAdapter
{
private Context context;
String[] list1;
int[] images = {R.drawable.ic_action_image1,
R.drawable.ic_action_image2,
R.drawable.ic_action_image2,
};
public MyAdapter4(Context context){
this.context = context;
list1 = context.getResources().getStringArray(R.array.drawerlist);
}
@Override
public int getCount() {
return list1.length;
}
@Override
public Object getItem(int position) {
return list1[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.easy_nav1, parent, false);
}
else
{
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);
titleTextView.setText(list1[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
// navigation drawer click listener
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
// Fragment fragment = null;
drawerLayout.closeDrawer(listView);
switch (position) {
case 0:
Intent addIntent1 = new Intent(this, YourActivtyPage1.class);
startActivity(addIntent1);
break;
case 1:
Intent addIntent2 = new Intent(this, YourActivtyPage2.class);
startActivity(addIntent2);
break;
}
}
}
您的活动 xml 文件
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Action Bar -->
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
// Add your xml what you need
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<ListView
android:id="@+id/drawerList"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#fff"
android:divider="#bdc3c7"
android:dividerHeight="0.5dp"
android:entries="@array/drawerlist" >
</ListView>
</android.support.v4.widget.DrawerLayout>
编辑:1
在字符串中添加您的导航抽屉项目
字符串.xml
<string-array name="drawerlist">
<item>navlist1</item>
<item>navlist2</item>
</string-array>
<string name="drawer_open">Open Navigation Drawer</string>
<string name="drawer_close">Close Navigation Drawer</string>