Android - Is Navigation Drawer from right hand sid

2018-12-31 20:10发布

问题:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

According to this doc, it doesn\'t say if it is possible to implement drawer from right hand side. Is it even possible? :(

回答1:

Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html



回答2:

The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:

<android.support.v4.widget.DrawerLayout
    android:id=\"@+id/drawer_layout\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\" >

    <FrameLayout
        android:id=\"@+id/content\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\"
        android:baselineAligned=\"false\" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id=\"@+id/left_drawer\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\"
        android:layout_gravity=\"left\"
        android:choiceMode=\"singleChoice\" />

    <!-- Right drawer -->

    <ListView
        android:id=\"@+id/right_drawer\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\"
        android:layout_gravity=\"right\"
        android:choiceMode=\"singleChoice\" />
</android.support.v4.widget.DrawerLayout>


回答3:

My App crashed with \"No drawer view found with gravity LEFT\" error.

So added this to the onOptionsItemSelected:

if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }


回答4:

To add to https://stackoverflow.com/a/21781710/437039 solution.

If you\'re using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

Next. In class NavigationDrawerFragment, you have to create 3 methods:

Method 1

public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

Method 2

public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

Method 3

public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}

Only now, the right-side drawer will function.



回答5:

You can use NavigationView from Material design. For ex :

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<android.support.v4.widget.DrawerLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    xmlns:app=\"http://schemas.android.com/apk/res-auto\"
    xmlns:tools=\"http://schemas.android.com/tools\"
    android:id=\"@+id/drawer_layout\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    android:fitsSystemWindows=\"true\"
    tools:openDrawer=\"start\">

    <include
        layout=\"@layout/app_bar_main\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\" />

    <android.support.design.widget.NavigationView
        android:id=\"@+id/nav_view\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"match_parent\"
        android:layout_gravity=\"end\"
        android:fitsSystemWindows=\"true\"
        app:headerLayout=\"@layout/nav_header_main\"
        app:menu=\"@menu/activity_main_drawer\" />

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

For complete tutorial please refer http://v4all123.blogspot.in/2016/03/simple-example-of-navigation-view-on.html



回答6:

I know this is an old question but for those who are still looking for the answer :

Yes, it is possible. Please check my answer on the link below :

https://stackoverflow.com/a/19358114/1572408



回答7:

Then Use these codes @amal i think this ll help you. XML:

<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id=\"@+id/frame_container\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\" >

    <RelativeLayout
        android:layout_width=\"match_parent\"
        android:layout_height=\"48dp\"
        android:background=\"@drawable/counter_bg\" >

        <ImageView
            android:id=\"@+id/iconl\"
            android:layout_width=\"25dp\"
            android:layout_height=\"wrap_content\"
            android:layout_gravity=\"left\"
            android:layout_marginLeft=\"12dp\"
            android:layout_marginRight=\"12dp\"
            android:src=\"@drawable/ic_launcher\" />

        <ImageView
            android:id=\"@+id/iconr\"
            android:layout_width=\"25dp\"
            android:layout_height=\"wrap_content\"
            android:layout_alignParentRight=\"true\"
            android:layout_alignParentTop=\"true\"
            android:layout_marginRight=\"17dp\"
            android:src=\"@drawable/ic_launcher\" />
    </RelativeLayout>
</FrameLayout>

<!-- Listview to display slider menu -->

<ListView
    android:id=\"@+id/list_slidermenu\"
    android:layout_width=\"240dp\"
    android:layout_height=\"match_parent\"
    android:layout_gravity=\"start\"
    android:background=\"@color/list_background\"
    android:choiceMode=\"singleChoice\"
    android:divider=\"@color/list_divider\"
    android:dividerHeight=\"1dp\"
    android:listSelector=\"@drawable/list_selector\" />

<ListView
    android:id=\"@+id/list_slidermenu2\"
    android:layout_width=\"240dp\"
    android:layout_height=\"match_parent\"
    android:layout_gravity=\"end\"
    android:background=\"@color/list_background\"
    android:choiceMode=\"singleChoice\"
    android:divider=\"@color/list_divider\"
    android:dividerHeight=\"1dp\"
    android:listSelector=\"@drawable/list_selector\" />

//set the required images

Activity code :

ImageView iconl,iconr;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;

ImageView iconl,iconr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iconl = (ImageView)findViewById(R.id.iconl);
    iconr = (ImageView)findViewById(R.id.iconr);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
    iconl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.START);
            mDrawerLayout.closeDrawer(Gravity.END);
        }
    });
    iconr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.END);
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    });
}
}

and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;


    default:
        break;
    }

    if (fragment != null) 
    {
        FragmentManager fragmentManager = getFragmentManager();
        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);

        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e(\"MainActivity\", \"Error in creating fragment\");
    }
}


回答8:

To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.