본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] NavigationDrawer Tutorial

by 돼지왕 왕돼지 2013. 11. 28.
반응형


 android, NavigationDrawer Tutorial

 


[android] NavigationDrawer Tutorial


 



Layout


You should use DrawerLayout to enable your app edge activated for open drawer automatically.

Otherwise you have to implement drawer open touch event by yourself.


You must keep in mind that FrameLayout that is responsible for drawer has to be come last.

Because android draws xml declared layout from the bottom so that the drawer will be come most upper part.




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="#666"

        android:dividerHeight="1dp"

        android:background="#333"

        android:paddingLeft="15sp"

        android:paddingRight="15sp"

        />

 

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







Java


One thing very important is ActionBarDrawerToggle class.

That class is responsible for the setting and manipulation of the Drawer.


// refer : http://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

public class MainActivity extends Activity {

 

    private DrawerLayout mDrawerLayout;

    private ActionBarDrawerToggle mAactionBarDrawerToggle;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mDrawerLayout = (DrawerLayout) findViewById( R.id.drawer_layout );

        mAactionBarDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, R.drawable.ic_drawer, 0, 0 );

        mDrawerLayout.setDrawerListener( mAactionBarDrawerToggle );

mDrawerLayout.setDrawerShadow( R.drawable.drawer_shadow, GravityCompat.START );


        getActionBar().setDisplayHomeAsUpEnabled( true ); 

    }

 

    @Override

    protected void onPostCreate(Bundle savedInstanceState) {

        super.onPostCreate(savedInstanceState);

        mAactionBarDrawerToggle.syncState();

    }

 

    @Override

    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);

        mAactionBarDrawerToggle.onConfigurationChanged(newConfig);

    }

 

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        if ( mAactionBarDrawerToggle.onOptionsItemSelected( item ) )

            return true;


        return super.onOptionsItemSelected(item);

    }

}




반응형

댓글