我尝试在同一个活动上实现两个导航抽屉,我正在关注这篇文章Two Navigation Drawer on same Activity,但是当我尝试在ListView中捕获onItemClick时,它发生了什么。在此先感谢您的帮助。
以下是我的代码..
MainActivity.java
public class MainActivity extends ActionBarActivity
{
DrawerLayout mDrawerlayout;
ListView mDrawerList_Left,mDrawerList_Right;
ActionBarDrawerToggle mDrawerToggle;
ImageButton imgLeftMenu,imgRightMenu;
NavDrawerAdapter Left_Adapter;
NavDrawerAdapter Right_Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//===============Initialization of Variables=========================//
mDrawerlayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList_Left=(ListView)findViewById(R.id.drawer_list_left);
mDrawerList_Left.setOnItemClickListener(new DrawerItemClickListener());
mDrawerList_Right=(ListView)findViewById(R.id.drawer_list_right);
imgLeftMenu=(ImageButton)findViewById(R.id.imgLeftMenu);
imgRightMenu=(ImageButton)findViewById(R.id.imgRightMenu);
mDrawerlayout.setDrawerListener(mDrawerToggle);
//============== Define a Custom Header for Navigation drawer=================//
LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflator.inflate(R.layout.header, null);
imgLeftMenu=(ImageButton)v.findViewById(R.id.imgLeftMenu);
imgRightMenu=(ImageButton)v.findViewById(R.id.imgRightMenu);
getSupportActionBar().setHomeButtonEnabled(true);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1281A9")));
getSupportActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getSupportActionBar().setCustomView(v);
imgLeftMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mDrawerlayout.isDrawerOpen(mDrawerList_Right)){
mDrawerlayout.closeDrawer(mDrawerList_Right);
}
mDrawerlayout.openDrawer(mDrawerList_Left);
mDrawerList_Left.bringToFront();
mDrawerlayout.requestLayout();
}
});
imgRightMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mDrawerlayout.isDrawerOpen(mDrawerList_Left)){
mDrawerlayout.closeDrawer(mDrawerList_Left);
}
mDrawerlayout.openDrawer(mDrawerList_Right);
mDrawerList_Right.bringToFront();
mDrawerlayout.requestLayout();
}
});
RefreshListView();
}
// Filling the ArrayLists
public void RefreshListView() {
NavDrawerMenuItem[] menu = new NavDrawerMenuItem[]{
NavMenuLabel.create("First Label"),
NavMenuIconLabel.create("First IconLabel", R.drawable.ic_drawer)
};
Left_Adapter = new NavDrawerAdapter(this,R.layout.navdrawer_item,menu);
mDrawerList_Left.setAdapter(Left_Adapter);
Right_Adapter = new NavDrawerAdapter(this,R.layout.navdrawer_item,menu);
mDrawerList_Right.setAdapter(Right_Adapter);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 1:
Log.d("Item Click","1");
break;
case 2:
Log.d("Item Click","2");
break;
default:
}
}
}
activity_main.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/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/drawer_list_left"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="@android:color/transparent"
android:layout_marginLeft="-64dp"
android:background="#FFFFFF"/>
<ListView
android:id="@+id/drawer_list_right"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:divider="@android:color/transparent"
android:background="#FFFFFF"/>