我想在导航抽屉里面使用片段类。
我该怎么办?
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">
<!-- Displaying Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- for Drawer -->
<FrameLayout
android:id="@+id/frame_container_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.DrawerLayout>
MainActivity .xml:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
//private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// NavigationDrawer title "Nasdaq" in this example
private CharSequence mDrawerTitle;
// App title "Navigation Drawer" in this example
private CharSequence mTitle;
// slider menu items details
private String[] navMenuTitles;
private TypedArray navMenuIcons;
//private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
Fragment fragment = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// getting items of slider from array
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// getting Navigation drawer icons from res
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Recycle array
navMenuIcons.recycle();
// Enable action bar icon_luncher as toggle Home Button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); //Setting, Refresh and Rate App
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// displayView(0);
}
fragment = new Fragment5Microsoft();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container_two, fragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// title/icon
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//called when invalidateOptionsMenu() invoke
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if Navigation drawer is opened, hide the action items
//boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
我想将Fragment5Microsoft类添加到Navigation Drawer。有可能吗?
fragment = new Fragment5Microsoft();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container_two, fragment).commit();
我的目标:
假设我的导航抽屉有一个按钮,我想点击此按钮并更改导航抽屉的内容。
答案 0 :(得分:0)
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<fragment
android:id="@+id/section_fragment"
android:name="example.ui.SectionFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"
/>