DrawerLayout的简单使用

DrawerLayout的简单使用的主要函数:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
 
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}


MainActivity代码:


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, NavigationView.OnNavigationItemSelectedListener {
    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ArrayAdapter<String> madpter;
    private Fragment1 fragment1;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar toolbar;
    private String mTitle;
    public MainActivity() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);


        mPlanetTitles = new String[]{"item0", "item1", "item2", "item3", "item4"};
        madpter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanetTitles);
        mDrawerList.setAdapter(madpter);
        mDrawerList.setOnItemClickListener(this);


        setSupportActionBar(toolbar);
        mTitle = toolbar.getTitle().toString();


//        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
//                this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        //添加监听
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                toolbar.setTitle("请选择");
                invalidateOptionsMenu();
            }


            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                toolbar.setTitle(mTitle);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(toggle);
        //syncState()方法1
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             fragment1 = new Fragment1();
             Bundle agrs = new Bundle();
             agrs.putString("data","item"+i);
             fragment1.setArguments(agrs);
             getFragmentManager().beginTransaction().add(R.id.content_frame,fragment1).commit();
             mDrawerLayout.closeDrawer(mDrawerList);
    }


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


    //隐藏menu
    @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.search_action).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }


//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Pass the event to ActionBarDrawerToggle, if it returns
//        // true, then it has handled the app icon touch event
//        if (mDrawerToggle.onOptionsItemSelected(item)) {
//            return true;
//        }
//        switch(item.getItemId()){
//            case R.id.search_action:
//                Intent intent = new Intent();
//                intent.setAction("android.intent.action.VIEW");
//                Uri uri = Uri.parse("http://www.baidu.com");
//                intent.setData(uri);
//                startActivity(intent);
//                break;
//            default:
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        String str = null;
        switch (item.getItemId()){
            case R.id.search_action:
                str = "search";
                break;
            case R.id.nave_camera:
                str = "camera";
                break;
            case R.id.nav_gallery:
                str = "gallery";
                break;
            case R.id.nav_manage:
                str = "manage";
                break;
            case R.id.nav_slideshow:
                str = "slideshow";
                break;
            case R.id.nav_share:
                str = "share";
                break;
            case R.id.nav_send:
                str = "send";
                break;
            default:
                break;
        }
        fragment1 = new Fragment1();
        Bundle agrs = new Bundle();
        agrs.putString("data","item "+str);
        fragment1.setArguments(agrs);
        getFragmentManager().beginTransaction().add(R.id.content_frame,fragment1).commit();
        //点击关闭DrawerLayout
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }


    //syncState()方法2
//    @Override
//    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
//        super.onPostCreate(savedInstanceState, persistentState);
//        // Sync the toggle state after onRestoreInstanceState has occurred.
//        mDrawerToggle.syncState();
//    }
//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//        super.onConfigurationChanged(newConfig);
//        mDrawerToggle.onConfigurationChanged(newConfig);
//    }

}


fragment代码:
public class Fragment1 extends Fragment {
 
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;


    private OnFragmentInteractionListener mListener;


    public Fragment1() {
        
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment1, container, false);
        TextView textView =(TextView) view.findViewById(R.id.tv_content);
        textView.setText(getArguments().getString("data"));
        return view;
    }
}


main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


        <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dp"
            android:background="#fdfdfd"/>
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_gravity="start"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            app:menu="@menu/menu"/>


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

fragment.xml代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f7f6f6"
    tools:context="com.example.cuboo.drawerlayout.Fragment1">


    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />


</FrameLayout>


menu.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:title="Content_menu">
     <menu>


     <item android:id="@+id/search_action"
         android:icon="@drawable/search"
         android:title="Search"
         android:showAsAction="ifRoom|withText"
         />
     <group android:checkableBehavior="none">
          <item android:id="@+id/nave_camera"
               android:icon="@drawable/ic_menu_camera"
               android:title="Import"/>
          <item
              android:id="@+id/nav_gallery"
              android:icon="@drawable/ic_menu_gallery"
              android:title="Gallery" />
          <item
              android:id="@+id/nav_slideshow"
              android:icon="@drawable/ic_menu_slideshow"
              android:title="Slideshow" />
          <item
              android:id="@+id/nav_manage"
              android:icon="@drawable/ic_menu_manage"
              android:title="Tools" />
     </group>
     </menu>
     </item>


     <item android:title="Communicate">
          <menu>
               <item
                   android:id="@+id/nav_share"
                   android:icon="@drawable/ic_menu_share"
                   android:title="Share" />
               <item
                   android:id="@+id/nav_send"
                   android:icon="@drawable/ic_menu_send"
                   android:title="Send" />
          </menu>
     </item>
</menu>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值