在android上实现NavDrawer

时间:2014-01-06 08:25:26

标签: c# android xamarin mvvmcross

我即将使用Mvvmcross和Xamarin(在c#中)在Android上实现NavDrawer,但我有点担心我的HomeView模型如下所示:https://github.com/jamesmontemagno/Xam.NavDrawer/blob/master/Mvx/MvxSample/ViewModels/HomeViewModel.cs

我如何在MS平台上使用这样的视图模型,在那里我可能会有一个panarama或者与布局数据非常不同的东西。

我是否应该为不支持NavDrawer类型体验的平台提供单独的主视图模型?

2 个答案:

答案 0 :(得分:2)

api级别的导航栏实现> 7

NavDrawerProject / RES /值/ strings.xml中

<resources>
    <string-array name="pages_array">
        <item>Home</item>
        <item>Trending</item>
        <item>Profile</item>
    </string-array>
<resources>

NavDrawerProject / RES /布局/ 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" >

<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="@color/drawer_bg"
    android:paddingTop="?attr/actionBarSize"/>

</android.support.v4.widget.DrawerLayout>

NavDrawerProject / SRC / MainActivity.java

public class MainActivity extends ActionBarActivity {
// navigation drawer
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPageTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setting up navigation drawer
    mTitle = mDrawerTitle = getTitle();
    mPageTitles = getResources().getStringArray(R.array.pages_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPageTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().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 */
    ) {
        // TODO find out if stray {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }
        // TODO find out if stray
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content
    // view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
}

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment;
    switch (position) {
    case 0:
        fragment = new HomePageFragment();
        break;
    case 1:
        fragment = new TrendingPageFragment();
        break;
    case 2:
        fragment = new ProfilePageFragment();
        break;
    default:
        fragment = new HomePageFragment();
        break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content_main, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPageTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

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

答案 1 :(得分:-2)

要在旧Android上使用导航抽屉,您应该查看支持库。 http://components.xamarin.com/view/xamandroidsupportv7appcompat