南尘:爱编程,爱安卓,每天进步一点点。

drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。

项目已同步至github:https://github.com/nanchen2251/DrawerLayoutDemo

drawerLayout的使用很方便,使用drawerLayout的要点如下:

1.drawerLayout其实是一个布局控件,跟LinearLayout等控件是一种东西,但是drawerLayout带有滑动的功能。只要按照drawerLayout的规定布局方式写完布局,就能有侧滑的效果。

有两点要注意:1)主内容区的布局代码要放在侧滑菜单布局的前面,这可以帮助DrawerLayout判断谁是侧滑菜单,谁是主内容区;2)侧滑菜单的部分的布局(这里是ListView)可以设置layout_gravity属性,他表示侧滑菜单是在左边还是右边。

先上一个运行图:

2.drawerLayout左侧菜单(或者右侧)的展开与隐藏可以被DrawerLayout.DrawerListener的实现监听到,这样你就可以在菜单展开与隐藏发生的时刻做一些希望做的事情。

3.何为侧边菜单。侧边菜单其实只是一个普通的View,一般里面装的是ListView,看起来就像菜单,他完全可以是一个button,textView等等。虽然称为菜单,但跟Activity的菜单形式是两码事,Activity的菜单只需要在资源文件中定义好,就能按照固定的形式显示出来。而drawerLayout的侧边菜单显示成什么样完全是取决于你自己,同样点击事件也完全由你自己去写。

4.如何点击某个按钮的时候能够展开或者隐藏侧边菜单。

可以用DrawerLayout.closeDrawer和DrawerLayout.openDrawer来隐藏与展开

drawerLayout与Fragment是什么关系?我们看到很多使用drawerLayout的代码中都同时使用了Fragment,这会造成误解,以为使用drawerLayout必须用到Fragment,其实这是错误的,使用Fragment是因为在侧滑菜单被点击的时候,主内容区如果内容比较复杂,用Fragment去填充会更容易,如果你的主内容区只是一个简单的字符串,只想在不同菜单点击的时候更新一下字符串的内容,我觉得没必要用Fragment。不过Fragment真的是很有用的东西,大多数情况下我们还是优先考虑Fragment和DrawerLayout搭配使用的。

6.说到Fragment,就想到了当我们在使用FrameLayout装载的时候,总会不断地来回切换Fragment,一直都是用replace()方法来替换Fragment:然后总感觉切换的时候有些卡顿,这样就会导致Fragment无限重绘。

每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。就想,如何让多个Fragment彼此切换时不重新实例化?翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。这样就能做到多个Fragment切换不重新实例化。

下面是一些代码:包含了fragment事务处理刷新界面的问题。

package com.example.nanchen.drawerlayoutdemo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentTransaction;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.FrameLayout;

import android.widget.ListView;

import com.example.nanchen.drawerlayoutdemo.adapter.MenuListAdapter;

import com.example.nanchen.drawerlayoutdemo.fragment.HotNewsFragment;

import com.example.nanchen.drawerlayoutdemo.fragment.LateNewsFragment;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private ListView lv;

private FrameLayout fl;

private List list;

private Fragment fragment_hot_news,fragment_late_news;

private MenuListAdapter adapter;

private DrawerLayout dl;

private FragmentTransaction ft;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

fl = (FrameLayout) findViewById(R.id.main_content_frame);

lv = (ListView) findViewById(R.id.main_left_drawer_lv);

dl = (DrawerLayout) findViewById(R.id.main_dl);

initList();

adapter = new MenuListAdapter(this,list);

lv.setAdapter(adapter);

//创建Fragment管理事务

ft = getSupportFragmentManager().beginTransaction();

if (fragment_hot_news == null){

fragment_hot_news = new HotNewsFragment();

ft.add(R.id.main_content_frame,fragment_hot_news);

ft.commit();

}

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

dl.closeDrawer(lv);

FragmentTransaction tran = getSupportFragmentManager().beginTransaction();

hideFragment(tran);//隐藏已经add的fragment

switch (position){

case 1:

if (fragment_hot_news == null){

fragment_hot_news = new HotNewsFragment();

tran.add(R.id.main_content_frame,fragment_hot_news);

}else{

tran.show(fragment_hot_news);

}

break;

case 2:

if (fragment_late_news == null){

fragment_late_news = new LateNewsFragment();

tran.add(R.id.main_content_frame,fragment_late_news);

}else{

tran.show(fragment_late_news);

}

break;

}

tran.commit();

}

/**

* 隐藏已经初始化的Fragment

*/

private void hideFragment(FragmentTransaction tran) {

if (fragment_hot_news != null){

tran.hide(fragment_hot_news);

}

if (fragment_late_news != null){

tran.hide(fragment_late_news);

}

}

});

}

private void initList() {

list = new ArrayList<>();

list.add("新闻");

list.add("热门新闻");

list.add("最新新闻");

list.add("推荐新闻");

list.add("博客");

list.add("所有博客");

list.add("48小时阅读排行");

list.add("10天内推荐排行");

list.add("收藏");

list.add("书签");

list.add("离线浏览");

list.add("工具");

list.add("搜索");

list.add("设置");

list.add("退出");

}

}

1

package com.example.nanchen.drawerlayoutdemo.fragment;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import com.example.nanchen.drawerlayoutdemo.R;

/**

* Created by nanchen on 2016/6/24.

*/

public class HotNewsFragment extends Fragment {

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_hot_news,null);

}

}

2

package com.example.nanchen.drawerlayoutdemo.fragment;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import com.example.nanchen.drawerlayoutdemo.R;

/**

* Created by nanchen on 2016/6/24.

*/

public class LateNewsFragment extends Fragment {

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_late_news,null);

}

}

下面是一些资源文件xml

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:id="@+id/main_dl"

tools:context="com.example.nanchen.drawerlayoutdemo.MainActivity">

android:id="@+id/main_content_frame"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/main_left_drawer_lv"

android:layout_width="230dp"

android:layout_height="match_parent"

android:layout_gravity="left"

android:divider="#afafaf"

android:background="#4b4a4a"

android:dividerHeight="1dp"

>

3

android:layout_width="match_parent"

android:background="#ddb0b0"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="这是热门新闻板块"

android:gravity="center"/>

4

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="这里都是最新的新闻"

android:gravity="center"/>

5

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ff9a9999"

android:padding="5dp">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="aa"

android:textColor="@color/tv_color_white"

android:id="@+id/menu_item1_tv"

android:gravity="center"/>

6

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:padding="5dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@mipmap/ic_launcher"

android:scaleType="fitXY"

android:id="@+id/menu_item2_iv"

android:padding="5dp"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="aa"

android:textColor="@color/tv_color_white"

android:id="@+id/menu_item2_tv"

android:layout_marginLeft="20dp"

android:gravity="center_vertical"/>

android drawer,android 官方DrawerLayout的介绍和使用相关推荐

  1. android 官方DrawerLayout的介绍和使用

    南尘:爱编程,爱安卓,每天进步一点点. drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出 ...

  2. Android的Google官方设计指南(上)

    Android 设计规范 本文章是我公司一个大牛之前的公司同事翻译的Android的Google官方设计指导,经过我整理而成,分享给大家,欢迎转载,但是请保留出处和翻译作者.本指导内容详实.规范,无论 ...

  3. 【凯子哥带你夯实应用层】Android的Google官方设计指南(上)

    Android 设计规范 时间 2015.3.2 版本 V1.0 翻译 杨鹏 整理 赵凯强 本文章是我公司一个大牛之前的公司同事翻译的Android的Google官方设计指导,经过我整理而成,分享给大 ...

  4. Android NDK开发之 NEON基础介绍

    原文:http://blog.csdn.net/app_12062011/article/details/50434259 Android NDK开发之 NEON基础介绍 这是官方介绍: http:/ ...

  5. android新特性:DrawerLayout与NavigationView配合使用

    首先看一下效果吧 Android Design Support Library中增加了 NavigationView与DrawerLayout这个控件.代替SlidingMenu项目中的使用! 在这里 ...

  6. android ui框架详解,多图详解 “Android UI”设计官方教程(二)

    编者注:本文为Android的官方开发者博客发了一份幻灯片的翻译文档的第二部分,专门介绍了一些Android UI设计的小贴士,我们在介绍这个幻灯片的第一部分<多图详解 "Androi ...

  7. [免费专栏] Android安全之APK逆向入门介绍

    也许每个人出生的时候都以为这世界都是为他一个人而存在的,当他发现自己错的时候,他便开始长大 少走了弯路,也就错过了风景,无论如何,感谢经历 Android安全付费专栏长期更新,本篇最新内容请前往: [ ...

  8. GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    本文转载于:https://github.com/Freelander/Android_Data/blob/master/Android-Librarys-Top-100.md 本项目主要对目前 Gi ...

  9. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

最新文章

  1. 【计算机网络】数据链路层 : CSMA/CD 协议 ( 载波监听多点接入 / 碰撞检测 协议 | 单程端到端传播时延 | 截断二进制指数规避算法 | 计算示例 | 最小帧长问题 )★
  2. Nginx实用指南V1 (连载之四:流行CMS博客rewrite写法)
  3. Linux禁止普通用户su至root
  4. 蛙蛙推荐:几种典型的生产环境调试场景
  5. 华为开源深度学习框架MindSpore背后的商业野心
  6. 笔记本内置扬声器三强PK
  7. cf1523C. Compression and Expansion
  8. 数据结构之字典序全排列
  9. LINQ to CSV,一种类型安全,动态的高性能方法
  10. JavaScript-传值(引用类型,基本类型)
  11. centos6.9下设置nginx服务开机自动启动
  12. Atitit mybatis业务流程配置化管理总结 目录 1. Mybatis 1 2. 流程模型常见的bpm模式 1 2.1. 活动task 流程,getway流程控制(分支跳转 循环等) 1 3
  13. 【三维路径规划】基于matlab蚁群算法无人机三维路径规划【含Matlab源码 1278期】
  14. POJ 3134 - Power Calculus (IDDFS)
  15. dos2unix 安装和使用
  16. Firefox 中文语言包安装方法
  17. IP和子网掩码和网关的关系
  18. 浏览器刷新+缓存原理
  19. 12306火车票助手
  20. kaggle点赞最多的 泰坦尼克号数据竞赛模型融合方法(附代码)

热门文章

  1. 插入优盘有提示音,但不显示
  2. Rollup(1): 安装 Rollup 以及 Rollup 和 Webpack 的区别
  3. 服务器磁盘阵列亮红灯报警故障处理
  4. 汤姆猫无法访问html,汤姆猫开始,但HTML不加载
  5. 微信小程序实现简单的画板
  6. android 哪个服务提示 存储设备空间不足,请释放一些空间,手机出现空间不足,至少需要449.93MB手机存储空间怎么办...
  7. 给编程初学者的一些建议
  8. MongoDB 组合多个条件查询(and、and、in、gte、gte、lte)
  9. Android应用生死轮回的那些事儿(2) - PackageManager
  10. checkra1n越狱错误79_用你的安卓手机越狱iPhoneX及以下设备