我的应用程序包含一个包含左侧导航抽屉的活动。 此活动还会动态加载不同的片段。
现在重点是:我想在每个片段上附加一个不同的右侧抽屉(以实现一些过滤器),附加在片段加载上。
我尝试在navigationDrawer.setUp(int fragmentId, DrawerLayout drawerLayout)
方法中复制并重复使用MyFragment.onAttach(...)
代码,但我得到NullPointerException
mFragmentContainerView = getActivity().findViewById(R.id.filter_drawer);
(我认为是getActivity()。findViewById (...)找不到任何东西)。
以下是我在CustomerFragment中自动生成的NavigationDrawerFragment中复制的代码:
mFragmentContainerView = getActivity().findViewById(R.id.filter_drawer);
mDrawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
我在网上搜索了一个解决方案,但每个人都做了与我想要的相反:“通过导航抽屉管理片段”,我想要“片段上的自定义导航抽屉”。
我怎样才能做到这一点?
答案 0 :(得分:4)
请参阅此代码,我使用了intent而不是片段
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private LinearLayout mDrawer1;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
Fragment[] allFrgaments;
public int curFrag = -1;
FragmentTransaction ft;
FragmentManager fm;
ImageView ivtophomeslide;
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidingmenu);
activity = MainActivity.this;
allFrgaments = new Fragment[] { new HomeFragment(),
new AccountFragment(), new SavedLocationFragment(),
new BecomeDriverFragment(), new InviteFragment(),
new DiscountFragment(), new RecentTripsFragment() };
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawer1 = (LinearLayout) findViewById(R.id.drawer1);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons
.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons
.getResourceId(7, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setHomeButtonEnabled(true);
getActionBar().setBackgroundDrawable(
new ColorDrawable(getResources().getColor(R.color.light_blue)));
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.menu_m, R.string.app_name,
R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
public void switchFrag(int position, Activity act) {
try {
Log.e("tag", "clicked" + position);
Log.e("tag", "current :-" + curFrag);
if (curFrag != position) {
curFrag = position;
ft = act.getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, allFrgaments[position], ""
+ position);
ft.commit();
displayView(position);
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawer1);
setTitle(navMenuTitles[position]);
} else {
Log.e("test", "else part fragment ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void displayView(int position) {
switch (position) {
case 0:
switchFrag(0, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 1:
switchFrag(1, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 2:
switchFrag(2, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 3:
switchFrag(3, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 4:
switchFrag(4, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 5:
switchFrag(5, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 6:
switchFrag(6, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
case 7:
Intent intent = new Intent(this, MainActivityy.class);
startActivity(intent);
// switchFrag(7, activity);
mDrawerLayout.closeDrawer(mDrawer1);
break;
default:
break;
}
}
@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);
}
}
答案 1 :(得分:1)
您可以通过删除片段类来完成此操作,并在导航抽屉中关闭抽屉,您可以简单地添加显式意图来调用另一类导航抽屉