我在这里有3个活动,我希望每个活动都继承我的导航抽屉。我已经浏览了许多其他资源,但我仍然对如何实现它感到困惑。
以下是我的BaseActivity,HomeActivity和ScheduleActivity
继承我的基础活动:
public class BaseActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
public DrawerLayout drawer;
ImageView navDrawerBtn;
HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expListView.setIndicatorBounds(402,465);
} else {
expListView.setIndicatorBoundsRelative(402,465);
}
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
;
prepareListData();
navDrawerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!drawer.isDrawerOpen(expListView)) {
drawer.openDrawer(expListView);
} else {
drawer.closeDrawer(expListView);
}
}
});
//listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (childPosition) {
case 0:
Intent a = new Intent(getApplicationContext(), MainActivity.class);
startActivity(a);
break;
case 1:
Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
startActivity(b);
break;
}
return false;
// TODO Auto-generated method stub
// Toast.makeText(
// getApplicationContext(),
// listDataHeader.get(groupPosition)
// + " : "
// + listDataChild.get(
// listDataHeader.get(groupPosition)).get(
// childPosition), Toast.LENGTH_SHORT)
// .show();
// return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("VRP Medical Bay");
//listDataHeader.add("");
//listDataHeader.add("");
// Adding child data
List<String> listUnderVRP = new ArrayList<String>();
listUnderVRP.add("eDataClinical");
listUnderVRP.add("Schedule");
listUnderVRP.add("Dictate");
listUnderVRP.add("View Messages");
listUnderVRP.add("Reports for Signature");
listUnderVRP.add("View Billing");
listUnderVRP.add("View State");
listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
//listDataChild.put(listDataHeader.get(1), nowShowing);
//listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
继承我的日程活动:
public class ScheduleActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
}
@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;
}
}
我的导航栏布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="73dp"
android:background="@color/actionbar" >
<ImageView
android:id="@+id/headerDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_drawer"
android:contentDescription="@string/desc" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/actionHeaderText"
android:layout_toRightOf="@+id/headerDrawer"
android:src="@drawable/e_icon"
android:contentDescription="@string/desc" />
<TextView
android:id="@+id/actionHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView1"
android:text="@string/actionbar_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold"
android:typeface="monospace" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="470dp"
android:layout_height="match_parent"
android:groupIndicator="@drawable/group_selector"
android:transcriptMode="alwaysScroll"
android:layout_gravity="start"
android:cacheColorHint="#00000000" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
和我的日程安排:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
答案 0 :(得分:3)
onCreate
的{{1}}中的不要拨打Schedule
而是执行此操作:
setContentView
使@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.schedule, null, false);
mDrawer.addView(contentView, 0);
}
中的mDrawer
受到保护。并且在MainActivity
中只保留抽屉标签和重力元素左(或右)。
答案 1 :(得分:0)
更好,更容易和更好推荐的方法是使用1活动&amp; 3个片段而不是3个活动。
3个活动意味着3个视图,这意味着将创建3个抽屉。
我建议1活动&amp; 3个片段(抽屉只会创建一次,主页面会不断被替换),因为这是API的建议:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
所以让你的导航抽屉像这样:
(使用默认操作栏)... http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
(没有默认操作栏或您自己的操作栏)... Using Navigation Drawer without TitleBar or ActionBar(我的回答)
答案 2 :(得分:0)
请在步骤
下面休息您应该将导航抽屉代码保留在BaseActivity中(超级 您需要的项目中所有活动的类 navigationDrawer)并设置navigationDrawer所需的布局 在BaseActivity的onCreate()中。
为所有需要的活动扩展BaseActivity navigationDrawer。
覆盖Activity中的onCreate方法,并且不要写入 super.onCreate()将创建所需的布局 navigationDrawer然后将您的活动特定布局添加到 创建的navigationDrawer布局。