«

Android - DrawerLayout的使用

时间:2024-3-2 17:59     作者:韩俊     分类: Android


Android DrawerLayout的使用。

从屏幕的左右边缘开始拖动,能拖出一个新的视图。


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" >
    <!-- 指定drawerlayout为根视图 -->
    <!-- main view -->

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

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#ffffcc"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" >
    </ListView>
</android.support.v4.widget.DrawerLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

MainActivit.java

package com.rust.rustdrawerlayout;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener{

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ArrayList<String> menuLists;
    private ArrayAdapter<String> adapter;
    @SuppressWarnings("deprecation")
    private ActionBarDrawerToggle mDrawerToggle;
    private String mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = (String) getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        menuLists = new ArrayList<String>();
        for (int i = 0; i < 8; i++) {    //初始化
            menuLists.add("按钮示例0" + i);
        }
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,menuLists);
        mDrawerList.setAdapter(adapter);    //内容添加完毕

        mDrawerList.setOnItemClickListener(this);   //设置监听器

        mDrawerToggle = new ActionBarDrawerToggle(this, 
                mDrawerLayout, R.drawable.ic_drawer, 
                R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);//
                getActionBar().setTitle("Please selecte");
                invalidateOptionsMenu();
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    //  左上角的图标就是一个menu item
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //动态插入一个fragment到framelayout当中
        Fragment contentFragment = new ContentFragment();
        Bundle args = new Bundle(); 
        args.putString("text", menuLists.get(position));  
        contentFragment.setArguments(args);  //携带一个参数

        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, contentFragment).commit();
        //开启一个事务
        mDrawerLayout.closeDrawer(mDrawerList);//隐藏当前导航菜单
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//      if (mDrawerToggle.onOptionsItemSelected(item)) {
//          return true;
//      }
        switch (item.getItemId()) {
        case R.id.action_websearch:
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri uri = Uri.parse("http://www.baidu.com");
            intent.setData(uri);
            startActivity(intent);
            break;

        case android.R.id.home:
             if (mDrawerLayout.isDrawerVisible(GravityCompat.END)) {  
                 mDrawerLayout.closeDrawer(GravityCompat.END);//关闭抽屉  
             } else {  
                 mDrawerLayout.openDrawer(GravityCompat.END);//打开抽屉  
             }  
//           
//           if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {  
//               mDrawerLayout.closeDrawer(GravityCompat.START);//关闭抽屉  
//           } else {  
//               mDrawerLayout.openDrawer(GravityCompat.START);//打开抽屉  
//           }  
//           

             break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        //
        mDrawerToggle.syncState();
        super.onPostCreate(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

ContentFragment.java

package com.rust.rustdrawerlayout;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContentFragment extends Fragment{
    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_content, container,false);
        //接受传入的值
        textView = (TextView) view.findViewById(R.id.textView);

        String text = getArguments().getString("text");
        textView.setText(text);     
        return view;
    }
}


标签: android

热门推荐