菜单之抽屉菜单(NavigationDrawer)

来源:互联网 发布:淘宝网司法房产拍卖 编辑:程序博客网 时间:2024/05/22 03:30

抽屉菜单只要用到v4包中的DrawerLayout类,ActionBarDrawerToggle类

  • 1.在布局文件中设置抽屉导航的布局
<?xml version="1.0" encoding="utf-8"?><!--将DrawerLayout作为activity布局的容器--><android.support.v4.widget.DrawerLayout    android:id="@+id/drawerlayout"    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"    tools:context="com.example.wendy.navigationdrawer.MainActivity">       <!--FrameLayout作为内容的承载容器-->        <FrameLayout            android:id="@+id/contentView"            android:layout_width="match_parent"            android:layout_height="match_parent">        </FrameLayout>        <!--listView为左侧抽屉导航的导航窗体-->        <ListView            android:id="@+id/drawerView"            android:layout_width="240dp"            android:background="@color/colorBlack"            android:listSelector="@drawable/list_selector"            android:layout_height="match_parent"            android:layout_gravity="start"//必须要设置这一项才能让ListView成为一个抽屉菜单            android:choiceMode="singleChoice"//listViewitem的选择模式            android:divider="@drawable/shape"//分割listview中各item            android:dividerHeight="1dp">        </ListView></android.support.v4.widget.DrawerLayout>
  • 2.在代码中实例化布局中的Drawerlayout和listview
public class MainActivity extends AppCompatActivity {    private DrawerLayout drawerLayout;    private ListView listView;    private ActionBarDrawerToggle mActionBarToggle;    private static String[] planeTitle = null;    private ActionBar actionBar;    private String mTitle;    private String mDrawerTitle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        if(savedInstanceState==null){            selectItem(0);        }    }    //初始化所有的控件    private void init() {        actionBar = getSupportActionBar();        actionBar.setDisplayHomeAsUpEnabled(true);        actionBar.setLogo(R.drawable.color);        actionBar.setDisplayShowHomeEnabled(true);        actionBar.setHomeButtonEnabled(true);        mTitle = mDrawerTitle = actionBar.getTitle() + "";        planeTitle = getResources().getStringArray(R.array.plane_name);        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);        listView = (ListView) findViewById(R.id.listDrawer);        listView.setBackgroundColor(Color.BLACK);        ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item, planeTitle);        listView.setAdapter(arrayAdapter);        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                selectItem(position);            }        });        mActionBarToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer) {            /**             * {@link DrawerLayout.DrawerListener} callback method. If you do not use your             * ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call             * through to this method from your own listener object.             *             * @param drawerView Drawer view that is now open             */            @Override            public void onDrawerOpened(View drawerView) {                actionBar.setTitle(mDrawerTitle);                invalidateOptionsMenu();            }            /**             * {@link DrawerLayout.DrawerListener} callback method. If you do not use your             * ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call             * through to this method from your own listener object.             *             * @param drawerView Drawer view that is now closed             */            @Override            public void onDrawerClosed(View drawerView) {                actionBar.setTitle(mTitle);                invalidateOptionsMenu();            }        };        drawerLayout.setDrawerListener(mActionBarToggle);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.option_menu, menu);        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        if (mActionBarToggle.onOptionsItemSelected(item)) {            return true;        }        switch (item.getItemId()) {            case R.id.app_bar_search:                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);                intent.putExtra(SearchManager.QUERY, mTitle);                if (intent.resolveActivity(getPackageManager()) != null)                    startActivity(intent);                else {                    Toast.makeText(this, "没有浏览器", Toast.LENGTH_SHORT).show();                }                return true;            default:                return super.onOptionsItemSelected(item);        }    }    @Override    public boolean onPrepareOptionsMenu(Menu menu) {        boolean drawerOpend = drawerLayout.isDrawerOpen(listView);        menu.findItem(R.id.app_bar_search).setVisible(!drawerOpend);        return super.onPrepareOptionsMenu(menu);    }    //当选择了ListView的item时调用此方法加载对应的fragment    private void selectItem(int position) {        mTitle = planeTitle[position];        actionBar.setTitle(planeTitle[position]);        PlaneFragment fragment = new PlaneFragment();        Bundle bundle = new Bundle();        bundle.putInt(PlaneFragment.PLANE_NUMBER, position);        fragment.setArguments(bundle);        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();        drawerLayout.closeDrawer(listView);    }    public static class PlaneFragment extends Fragment {        public static final String PLANE_NUMBER = "plane number";        public PlaneFragment() {        }        @Nullable        @Override        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {            Bundle bundle = getArguments();            int position = bundle.getInt(PLANE_NUMBER);            String name = planeTitle[position].toLowerCase();            int id = getResources().getIdentifier(name, "drawable", getActivity().getPackageName());            View rootView = inflater.inflate(R.layout.fragment, container, false);            ImageView image = (ImageView) rootView.findViewById(R.id.image);            image.setImageResource(id);            return rootView;        }    }}
0 0
原创粉丝点击