百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

基于Android 8.0的源码分析(View的绘制流程)

bigegpt 2024-08-21 12:00 2 浏览

1.使用的注意事项

  • 1.主内容视图一定要是DrawerLayout的第一个子视图
  • 2.主内容视图宽度和高度需要match_parent
  • 3.必须显示指定侧滑视图的android:layout_gravity属性 android:layout_gravity = "start"时,从左向右滑出菜单 android:layout_gravity = "end"时,从右向左滑出菜单 不推荐使用left和right!!!
  • 侧滑视图的宽度以dp为单位,不建议超过320dp(为了总能看到一些主内容视图)
  • 设置侧滑事件:mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener);
  • 要说一点:可以结合Actionbar使用当用户点击Actionbar上的应用图标,弹出侧滑菜单! 这里就要通过ActionBarDrawerToggle,它是DrawerLayout.DrawerListener的具体实现类, 我们可以重写ActionBarDrawerToggle的onDrawerOpened()和onDrawerClosed()以监听抽屉拉出 或隐藏事件!但是这里我们不讲,因为5.0后我们使用的是Toolbar!有兴趣的可以自行查阅相关 文档!

2.使用代码示例


示例1:单个侧滑菜单的实现

运行效果图

实现关键代码

首先是我们的主布局,注意:最外层要是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">

<FrameLayout

android:id="@+id/ly_content"

android:layout_width="match_parent"

android:layout_height="match_parent" />

<ListView

android:id="@+id/list_left_drawer"

android:layout_width="180dp"

android:layout_height="match_parent"

android:layout_gravity="start"

android:background="#080808"

android:choiceMode="singleChoice"

android:divider="#FFFFFF"

android:dividerHeight="1dp" />

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

接着ListView的布局代码和domain类:Item比较简单,就不给出了,直接上中间Fragment的 布局以及代码吧!另外Adapter直接复用我们之前写的那个可复用的MyAdapter!

fg_content.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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/tv_content"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:textSize="25sp" />

</RelativeLayout>

ContentFragment.java

/**

* Created by Jay on 2015/10/8 0008.

*/

public class ContentFragment extends Fragment {

private TextView tv_content;

@Override

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

View view = inflater.inflate(R.layout.fg_content, container, false);

tv_content = (TextView) view.findViewById(R.id.tv_content);

String text = getArguments().getString("text");

tv_content.setText(text);

return view;

}

}

最后是我们的Activity类

MainActivity.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

private DrawerLayout drawer_layout;

private ListView list_left_drawer;

private ArrayList<Item> menuLists;

private MyAdapter<Item> myAdapter = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

list_left_drawer = (ListView) findViewById(R.id.list_left_drawer);

menuLists = new ArrayList<Item>();

menuLists.add(new Item(R.mipmap.iv_menu_realtime,"实时信息"));

menuLists.add(new Item(R.mipmap.iv_menu_alert,"提醒通知"));

menuLists.add(new Item(R.mipmap.iv_menu_trace,"活动路线"));

menuLists.add(new Item(R.mipmap.iv_menu_settings,"相关设置"));

myAdapter = new MyAdapter<Item>(menuLists,R.layout.item_list) {

@Override

public void bindView(ViewHolder holder, Item obj) {

holder.setImageResource(R.id.img_icon,obj.getIconId());

holder.setText(R.id.txt_content, obj.getIconName());

}

};

list_left_drawer.setAdapter(myAdapter);

list_left_drawer.setOnItemClickListener(this);

}

@Override

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

ContentFragment contentFragment = new ContentFragment();

Bundle args = new Bundle();

args.putString("text", menuLists.get(position).getIconName());

contentFragment.setArguments(args);

FragmentManager fm = getSupportFragmentManager();

fm.beginTransaction().replace(R.id.ly_content,contentFragment).commit();

drawer_layout.closeDrawer(list_left_drawer);

}

}

代码很简单,就不多说了~


示例2.左右两个侧滑菜单的实现

嗯,不知道你有没有发现,从上面的DrawerLayout的布局,我们大概可以猜到,DrawerLayout 最多由三个部分组成,中间的内容部分,左边的侧滑菜单部分,右边的侧滑菜单部分组成! 下面我们来写一个带有两个侧滑菜单的示例!

运行效果图

代码实现

首先我们创建两个Fragment以及对应的布局,他们分别是左右侧滑菜单!

左边Fragment

布局:fg_left.xml,这里就用了一个图片而以,点击后弹出一个新的Activity; 当然你可以根据自己的需求进行扩展!

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:id="@+id/img_bg"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@mipmap/bg_menu_left"/>

</LinearLayout>

对应的LeftFragment.java

/**

* Created by Jay on 2015/10/9 0009.

*/

public class LeftFragment extends Fragment{

private DrawerLayout drawer_layout;

@Override

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

View view = inflater.inflate(R.layout.fg_left, container, false);

ImageView img_bg = (ImageView) view.findViewById(R.id.img_bg);

img_bg.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

getActivity().startActivity(new Intent(getActivity(),OtherActivity.class));

drawer_layout.closeDrawer(Gravity.START);

}

});

return view;

}

//暴露给Activity,用于传入DrawerLayout,因为点击后想关掉DrawerLayout

public void setDrawerLayout(DrawerLayout drawer_layout){

this.drawer_layout = drawer_layout;

}

}

右面的Fragment

布局就三个按钮,点击后替换中间部分的Fragment,布局fg_right.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:background="#2F9AF2"

android:gravity="center"

android:orientation="vertical">

<Button

android:id="@+id/btn_one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="菜单项一" />

<Button

android:id="@+id/btn_two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="菜单项二" />

<Button

android:id="@+id/btn_three"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="菜单项三" />

</LinearLayout>

然后对应的是RightFragment.java

/**

* Created by Jay on 2015/10/9 0009.

*/

public class RightFragment extends Fragment implements View.OnClickListener{

private DrawerLayout drawer_layout;

private FragmentManager fManager;

@Override

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

View view = inflater.inflate(R.layout.fg_right, container, false);

view.findViewById(R.id.btn_one).setOnClickListener(this);

view.findViewById(R.id.btn_two).setOnClickListener(this);

view.findViewById(R.id.btn_three).setOnClickListener(this);

fManager = getActivity().getSupportFragmentManager();

return view;

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.btn_one:

ContentFragment cFragment1 = new ContentFragment("1.点击了右侧菜单项一",R.color.blue);

fManager.beginTransaction().replace(R.id.fly_content,cFragment1).commit();

drawer_layout.closeDrawer(Gravity.END);

break;

case R.id.btn_two:

ContentFragment cFragment2 = new ContentFragment("2.点击了右侧菜单项二",R.color.red);

fManager.beginTransaction().replace(R.id.fly_content,cFragment2).commit();

drawer_layout.closeDrawer(Gravity.END);

break;

case R.id.btn_three:

ContentFragment cFragment3 = new ContentFragment("3.点击了右侧菜单项三",R.color.yellow);

fManager.beginTransaction().replace(R.id.fly_content,cFragment3).commit();

drawer_layout.closeDrawer(Gravity.END);

break;

}

}

public void setDrawerLayout(DrawerLayout drawer_layout){

this.drawer_layout = drawer_layout;

}

}

另外还有一个中间部分填充的ContentFragment,布局:fg_content.xml如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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/tv_content"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:textSize="25sp" />

</RelativeLayout>

ContentFragment.java

public class ContentFragment extends Fragment {

private TextView tv_content;

private String strContent;

private int bgColor;

public ContentFragment(String strContent,int bgColor) {

this.strContent = strContent;

this.bgColor = bgColor;

}

@Override

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

View view = inflater.inflate(R.layout.fg_content, container, false);

view.setBackgroundColor(getResources().getColor(bgColor));

tv_content = (TextView) view.findViewById(R.id.tv_content);

tv_content.setText(strContent);

return view;

}

}

编写好以后,就到我们的Activity的布局了以及Activity的代码了: 在此之前我们还需要些一个顶部条形栏的布局:

view_topbar.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#DCDEDB">

<Button

android:id="@+id/btn_right"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_centerVertical="true"

android:layout_alignParentRight="true"

android:background="@drawable/btn_selctor"/>

</RelativeLayout>

然后是activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<include

android:id="@+id/topbar"

layout="@layout/view_topbar"

android:layout_width="wrap_content"

android:layout_height="48dp" />

<FrameLayout

android:id="@+id/fly_content"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</LinearLayout>

<fragment

android:id="@+id/fg_left_menu"

android:name="jay.com.drawerlayoutdemo2.LeftFragment"

android:layout_width="300dp"

android:layout_height="match_parent"

android:layout_gravity="start"

android:tag="LEFT"

tools:layout="@layout/fg_left" />

<fragment

android:id="@+id/fg_right_menu"

android:name="jay.com.drawerlayoutdemo2.RightFragment"

android:layout_width="100dp"

android:layout_height="match_parent"

android:layout_gravity="end"

android:tag="RIGHT"

tools:layout="@layout/fg_right" />

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

最后是MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private DrawerLayout drawer_layout;

private FrameLayout fly_content;

private View topbar;

private Button btn_right;

private RightFragment fg_right_menu;

private LeftFragment fg_left_menu;

private FragmentManager fManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

fManager = getSupportFragmentManager();

fg_right_menu = (RightFragment) fManager.findFragmentById(R.id.fg_right_menu);

fg_left_menu = (LeftFragment) fManager.findFragmentById(R.id.fg_left_menu);

initViews();

}

private void initViews() {

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

fly_content = (FrameLayout) findViewById(R.id.fly_content);

topbar = findViewById(R.id.topbar);

btn_right = (Button) topbar.findViewById(R.id.btn_right);

btn_right.setOnClickListener(this);

//设置右面的侧滑菜单只能通过编程来打开

drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,

Gravity.END);

drawer_layout.setDrawerListener(new DrawerLayout.DrawerListener() {

@Override

public void onDrawerSlide(View view, float v) {

}

@Override

public void onDrawerOpened(View view) {

}

@Override

public void onDrawerClosed(View view) {

drawer_layout.setDrawerLockMode(

DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.END);

}

@Override

public void onDrawerStateChanged(int i) {

}

});

fg_right_menu.setDrawerLayout(drawer_layout);

fg_left_menu.setDrawerLayout(drawer_layout);

}

@Override

public void onClick(View v) {

drawer_layout.openDrawer(Gravity.RIGHT);

drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,

Gravity.END); //解除锁定

}

}


好的,至此就大功告成了~,呼呼,下面说下看代码时可能会有的疑惑:

  • 1. drawer_layout.openDrawer(Gravity.END);
  • 这句是设置打开的哪个菜单START代表左边,END代表右边
  • 2. drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.END); 锁定右面的侧滑菜单,不能通过手势关闭或者打开,只能通过代码打开!即调用openDrawer方法! 接着 drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.END); 解除锁定状态,即可以通过手势关闭侧滑菜单 最后在drawer关闭的时候调用: drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.END); 再次锁定右边的侧滑菜单!
  • 3. 布局代码中的Tag属性的作用? 答:这里没用到,在重写DrawerListener的onDrawerSlide方法时,我们可以通过他的第一个 参数drawerView,调用drawerView.getTag().equals("START")判断触发菜单事件的是哪个 菜单!然后可以进行对应的操作!

3.代码示例下载

DrawerLayoutDemo.zip

DrawerLayoutDemo2.zip


本节小结:

好的,本节给大家介绍了官方的侧滑控件DrawerLayout的基本用法,使用起来非常的方便! 当然这里仅仅是简单的使用演示,另外看到弘扬大神写过一篇: Android DrawerLayout 高仿QQ5.2双向侧滑菜单 有兴趣可以看看,如果看完本节的内容,相信你看起来不会怎么吃力~好的!

相关推荐

悠悠万事,吃饭为大(悠悠万事吃饭为大,什么意思)

新媒体编辑:杜岷赵蕾初审:程秀娟审核:汤小俊审签:周星...

高铁扒门事件升级版!婚宴上‘冲喜’老人团:我们抢的是社会资源

凌晨两点改方案时,突然收到婚庆团队发来的视频——胶东某酒店宴会厅,三个穿大红棉袄的中年妇女跟敢死队似的往前冲,眼瞅着就要扑到新娘的高额钻石项链上。要不是门口小伙及时阻拦,这婚礼造型团队熬了三个月的方案...

微服务架构实战:商家管理后台与sso设计,SSO客户端设计

SSO客户端设计下面通过模块merchant-security对SSO客户端安全认证部分的实现进行封装,以便各个接入SSO的客户端应用进行引用。安全认证的项目管理配置SSO客户端安全认证的项目管理使...

还在为 Spring Boot 配置类加载机制困惑?一文为你彻底解惑

在当今微服务架构盛行、项目复杂度不断攀升的开发环境下,SpringBoot作为Java后端开发的主流框架,无疑是我们手中的得力武器。然而,当我们在享受其自动配置带来的便捷时,是否曾被配置类加载...

Seata源码—6.Seata AT模式的数据源代理二

大纲1.Seata的Resource资源接口源码2.Seata数据源连接池代理的实现源码3.Client向Server发起注册RM的源码4.Client向Server注册RM时的交互源码5.数据源连接...

30分钟了解K8S(30分钟了解微积分)

微服务演进方向o面向分布式设计(Distribution):容器、微服务、API驱动的开发;o面向配置设计(Configuration):一个镜像,多个环境配置;o面向韧性设计(Resista...

SpringBoot条件化配置(@Conditional)全面解析与实战指南

一、条件化配置基础概念1.1什么是条件化配置条件化配置是Spring框架提供的一种基于特定条件来决定是否注册Bean或加载配置的机制。在SpringBoot中,这一机制通过@Conditional...

一招解决所有依赖冲突(克服依赖)

背景介绍最近遇到了这样一个问题,我们有一个jar包common-tool,作为基础工具包,被各个项目在引用。突然某一天发现日志很多报错。一看是NoSuchMethodError,意思是Dis...

你读过Mybatis的源码?说说它用到了几种设计模式

学习设计模式时,很多人都有类似的困扰——明明概念背得滚瓜烂熟,一到写代码就完全想不起来怎么用。就像学了一堆游泳技巧,却从没下过水实践,很难真正掌握。其实理解一个知识点,就像看立体模型,单角度观察总...

golang对接阿里云私有Bucket上传图片、授权访问图片

1、为什么要设置私有bucket公共读写:互联网上任何用户都可以对该Bucket内的文件进行访问,并且向该Bucket写入数据。这有可能造成您数据的外泄以及费用激增,若被人恶意写入违法信息还可...

spring中的资源的加载(spring加载原理)

最近在网上看到有人问@ContextConfiguration("classpath:/bean.xml")中除了classpath这种还有其他的写法么,看他的意思是想从本地文件...

Android资源使用(android资源文件)

Android资源管理机制在Android的开发中,需要使用到各式各样的资源,这些资源往往是一些静态资源,比如位图,颜色,布局定义,用户界面使用到的字符串,动画等。这些资源统统放在项目的res/独立子...

如何深度理解mybatis?(如何深度理解康乐服务质量管理的5个维度)

深度自定义mybatis回顾mybatis的操作的核心步骤编写核心类SqlSessionFacotryBuild进行解析配置文件深度分析解析SqlSessionFacotryBuild干的核心工作编写...

@Autowired与@Resource原理知识点详解

springIOCAOP的不多做赘述了,说下IOC:SpringIOC解决的是对象管理和对象依赖的问题,IOC容器可以理解为一个对象工厂,我们都把该对象交给工厂,工厂管理这些对象的创建以及依赖关系...

java的redis连接工具篇(java redis client)

在Java里,有不少用于连接Redis的工具,下面为你介绍一些主流的工具及其特点:JedisJedis是Redis官方推荐的Java连接工具,它提供了全面的Redis命令支持,且...