删除片段和活动更改时的自定义工具栏视图

时间:2015-03-20 08:42:12

标签: android android-fragments navigation-drawer android-custom-view android-toolbar

Activity A和B. A包含fragments,可以通过点击导航抽屉上的项目来导航。 Activity B有自定义toolbar视图。活动B上也可以使用导航抽屉,因为活动B扩展了活动A.当我通过单击抽屉上的项目从B返回任何fragment activity A时,自定义视图仍然存在。我不希望自定义视图显示在活动A的fragments上。我该怎么做?

主要活动代码:

public class MainActivity extends ActionBarActivity {
protected static int position;
private static boolean isLaunch = true;
protected DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
protected Toolbar mToolbar,mToolbar2;
protected FrameLayout frameLayout;
protected View mCustomView;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private SharedPreferences runPrefs;
Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    frameLayout = (FrameLayout) findViewById(R.id.frame_container);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mTitle = mDrawerTitle = getTitle();
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);
    ArrayList<NavDrawerItem> navDrawerItems = new ArrayList<NavDrawerItem>();
    for (int i = 0; i < navMenuTitles.length; i++) {
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons
                .getResourceId(i, -1)));
    }
    //  Garbage collection
    navMenuIcons.recycle();
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    NavDrawerListAdapter adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    if (isLaunch) {
        isLaunch = false;
        displayView(0);
    }
  }


private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // display view for selected nav drawer item
        displayView(position);
        mDrawerList.setItemChecked(position, true);
    }
}

private void displayView(int position) {
    // update the main content by replacing fragments
    mDrawerLayout.closeDrawer(mDrawerList);
    MainActivity.position = position;
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new FragmentMain();
            break;
        case 1:
            fragment = new FragmentLogin();

            break;
        case 2:
            fragment = new FragmentEmergency();
            break;
        case 3:
            fragment = new FragmentFavourite();
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}
}

其他活动的代码(使用自定义工具栏视图)

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLayoutInflater().inflate(R.layout.activity_business_result_list, frameLayout);
    mDrawerList.setItemChecked(position, true);
    this.businessList = (ListView) findViewById(R.id.lvBusinesslist); // Initialize View Containing Fields of ListView
    this.footerView = ((LayoutInflater) this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
    this.businessList.addFooterView(footerView);
    this.adapter = new BusinessListDataAdapter(this, this.imgFetcher,
            this.layoutInflator, this.businesses); // initialize BusinessListDataAdapter
    this.businesses = null;
    Intent myIntent = getIntent();
    // gets the arguments from previously created intent
    this.metroTxt = myIntent.getStringExtra("key");
    this.metroLoc = myIntent.getStringExtra("loc");
    this.metroId = myIntent.getStringExtra("qt");

    showCustomView(metroTxt, metroLoc, metroId);
    getBusinesses(metroTxt, metroLoc, metroId); // Get Businesses List from server
}

void showCustomView(String metroTxt, String metroLoc, String metroId) {
    LayoutInflater mInflater = LayoutInflater.from(this);

    if (mCustomView == null)
        mCustomView = mInflater.inflate(R.layout.custom_action_bar, null);
    mToolbar.removeView(mCustomView);
    mToolbar.addView(mCustomView);
}

0 个答案:

没有答案