所以我正在使用导航抽屉,主要是我只是清除元素的视图,然后将它们添加回动态内容。一个屏幕将主要是非动态内容,与其他屏幕不同,所以我想创建两个xml布局,一个用于动态内容,一个用于静态。
问题是,当我切换到新视图时,导航抽屉没有显示出来。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString("area", "Home");
editor.commit();
setContentView(R.layout.main_screen);
setupDrawer(0);
}
private void itemSelected(String selection, int position){
DataClass dc = new DataClass(this);
dc.setSection(selection, this);
ArrayList<String> menuArray = dc.getMenuArray(selection);
String[] menuList = menuArray.toArray(new String[menuArray.size()]);
setContentView(R.layout.activity_main);
setupDrawer(1);
standardDisplay(selection, menuList, 1);
}
private void setupDrawer(int i){
mTitle = mDrawerTitle = getTitle();
mForumTitles = getResources().getStringArray(R.array.main_menu_array);
if (i == 0){
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
}else if(i == 1){
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mForumTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void standardDisplay(String section, String[] menuList, int position){
setContentView(R.layout.activity_main);
mDrawerList.setItemChecked(position, true);
setTitle(section);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, menuList));
mDrawerLayout.closeDrawer(mDrawerList);
setMainView(section, menuList[position]);
}
private void setMainView(String section, String selection){
mTableView = (LinearLayout) findViewById(R.id.scroll_layout);
mTableView.removeAllViews();
DataClass dc = new DataClass(this);
ArrayList<String> info = dc.getInfoArray(section, selection);
String[] instructions = info.get(0).split("\\|");
String[] values = info.get(1).split("\\|");
for(int i=0; i < instructions.length; i++){
if (instructions[i].equals("@image")){
ImageView newImage = new ImageView(this);
newImage.setBackgroundColor(Color.BLUE);
mTableView.addView(newImage);
}else{
TextView newText = new TextView(this);
newText.setText(instructions[i] + ": " + values[i]);
newText.setTextColor(Color.BLACK);
newText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
//newRow.addView(newText);
mTableView.addView(newText);
}
}
TextView newText = new TextView(this);
newText.setText(info.get(2));
newText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
newText.setTextColor(Color.BLACK);
mTableView.setPadding(60, 20, 60, 0);
mTableView.addView(newText);
}
main_screen.xml:
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/MainText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="@string/Dashboard"
android:textColor="#000000"
android:gravity="center_horizontal"
/>
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml中:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</FrameLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>