我们可以将导航菜单与活动一起使用而不是片段吗?

时间:2015-04-03 11:33:56

标签: android android-fragments

我想要完成导航菜单。

2 个答案:

答案 0 :(得分:1)

嗯,至于我,这是我开始学习Android时的第一个问题。但是,嘿,我会尝试解释为什么每个人都使用片段:

  1. 使用sidemenu可以更轻松地管理碎片。为什么?因为如果要将每个菜单项实现为Activity - 您需要实现该菜单部分"在每个Activity菜单中。菜单以Activity为界,Fragment只是其中的一部分 - 您可以将片段添加到Activity,就像在layout.xml中查看一样简单。

  2. 为什么你只需要活动?片段有getActivity()方法,它会返回父Activity。您可以在片段的onCreateView(...)方法上为片段实现自己的布局。通常,您可以在Activity中实现某种公共方法,然后调用((MyActivity)getActivity()).[your_method_name]()来实际调用您的Activity方法。

  3. 困难是学习的一部分,所以不要害怕学习新东西。如果有90%的人使用Fragments,那么就可以使用它们,只需要考虑一下;)

答案 1 :(得分:0)

BaseActivity.java

public class BaseActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer);
    // if (savedInstanceState == null) {
    // // on first time display view for first nav item
    // // displayView(0);
    // }
}

public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
    mTitle = mDrawerTitle = getTitle();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items
    if (navMenuIcons == null) {
        for (int i = 0; i < navMenuTitles.length; i++) {
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
        }
    } else {
        for (int i = 0; i < navMenuTitles.length; i++) {
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
                    navMenuIcons.getResourceId(i, -1)));
        }
    }

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    // getSupportActionBar().setIcon(R.drawable.ic_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, // nav menu toggle icon
            R.string.app_name, // nav drawer open - description for
            // accessibility
            R.string.app_name // nav drawer close - description for
    // accessibility
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            supportInvalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            supportInvalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

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);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    return super.onOptionsItemSelected(item);
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {

    switch (position) {
    case 0:
        Intent intent = new Intent(this, FirstActivity.class);
        startActivity(intent);
        finish();// finishes the current activity
        break;
    case 1:
        Intent intent1 = new Intent(this, SecondActivity.class);
        startActivity(intent1);
        finish();// finishes the current activity
        break;
    // case 2:
    // Intent intent2 = new Intent(this, third.class);
    // startActivity(intent2);
    // finish();
    // break;
    // case 3:
    // Intent intent3 = new Intent(this, fourth.class);
    // startActivity(intent3);
    // finish();
    // break;
    // case 4:
    // Intent intent4 = new Intent(this, fifth.class);
    // startActivity(intent4);
    // finish();
    // break;
    // case 5:
    // Intent intent5 = new Intent(this, sixth.class);
    // startActivity(intent5);
    // finish();
    // break;
    default:
        break;
    }

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}

产品..

public class NavDrawerItem {
private String title;
private int icon;

public NavDrawerItem() {
}

public NavDrawerItem(String title, int icon) {
    this.title = title;
    this.icon = icon;
}

public NavDrawerItem(String title) {
    this.title = title;
}

public String getTitle() {
    return this.title;
}

public int getIcon() {
    return this.icon;
}

public void setTitle(String title) {
    this.title = title;
}

public void setIcon(int icon) {
    this.icon = icon;
}

}

适配器

   public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
    return navDrawerItems.size();
}

@Override
public Object getItem(int position) {       
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);


    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());



    return convertView;
}

}

FirstActivity.java

public class FirstActivity extends BaseActivity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 


    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);// load icons from
                                                        // strings.xml

    set(navMenuTitles, navMenuIcons);
    }

}