布局:fg_left_drawer

<?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"> <ListView
android:id="@+id/list_left_listview"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#d6d6d6"
android:divider="#FFFFFF"
android:dividerHeight="1dp" /> </LinearLayout>

创建LeftDrawFragment

public class LeftDrawFragment  extends Fragment{

    private ListView listView;
private ArrayAdapter<String> myAdapter = null;
private String []data = {"1","2","3","4"}; private DrawerLayout drawerLayout; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_left_drawer, container, false);
// tv_content = (TextView) view.findViewById(R.id.tv_content);
// String text = getArguments().getString("text");
// tv_content.setText(text); listView = (ListView)view.findViewById(R.id.list_left_listview);
myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data);
listView.setAdapter(myAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
drawerLayout.closeDrawer(Gravity.LEFT); Toast.makeText(getActivity(),"点击了"+data[i],Toast.LENGTH_SHORT).show();
}
}); return view;
} //暴露给Activity,用于传入DrawerLayout,因为点击后想关掉DrawerLayout
public void setDrawerLayout(DrawerLayout drawer_layout){
this.drawerLayout = drawer_layout;
} }

第三步,default_nav_head.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="64dp"
android:background="#12b7f5"> <LinearLayout
android:layout_width="85dp"
android:layout_height="match_parent"
android:id="@+id/default_nav_left_layout">
<ImageView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_marginLeft="5dp"
/> </LinearLayout> <TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="档案"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
/> <RelativeLayout
android:layout_width="85dp"
android:layout_height="match_parent"
android:id="@+id/default_nav_right_layout"
> <ImageView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
/> </RelativeLayout> </LinearLayout>

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"
> <GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/home_content_gridview"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_margin="5dp"> </GridView> </RelativeLayout>

创建ContentFragment

public class ContentFragment  extends Fragment{

    private GridView gridView;
private MainAdapter adapter; @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); gridView = (GridView) view.findViewById(R.id.home_content_gridview);
adapter = new MainAdapter(getActivity());
gridView.setAdapter(adapter); return view;
} }

activity_xml

<?xml version="1.0" encoding="utf-8"?>
<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"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <include layout="@layout/default_nav_head"/> <FrameLayout
android:id="@+id/ly_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </LinearLayout> <fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="LEFT"
android:layout_gravity="left"
android:name="com.example.mac.mainapplication.Activity.LeftDrawFragment"
android:id="@+id/left_drawer_fragment"/> </android.support.v4.widget.DrawerLayout>

mainactivity

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private DrawerLayout drawerLayout;
private LeftDrawFragment leftDrawFragment;
private FragmentManager fManager; private LinearLayout leftLayout;
private RelativeLayout rightLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); fManager = getSupportFragmentManager();
leftDrawFragment = (LeftDrawFragment)fManager.findFragmentById(R.id.left_drawer_fragment); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
leftDrawFragment.setDrawerLayout(drawerLayout); ContentFragment contentFragment = new ContentFragment();
Bundle args = new Bundle();
args.putString("text","");
contentFragment.setArguments(args); FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.ly_content,contentFragment).commit(); leftLayout = (LinearLayout)findViewById(R.id.default_nav_left_layout);
leftLayout.setOnClickListener(this); rightLayout = (RelativeLayout)findViewById(R.id.default_nav_right_layout);
rightLayout.setVisibility(View.INVISIBLE); } @Override
public void onClick(View view) { switch (view.getId()){
case R.id.default_nav_left_layout:
drawerLayout.openDrawer(Gravity.LEFT);
break;
default:
break;
} }
}

数据参考:http://blog.csdn.net/coder_pig/article/details/49000185

DrawLayout使用侧滑抽屉的更多相关文章

  1. Android使用ToolBar+DrawerLayout+NavigationView实现侧滑抽屉效果

    学会使用DrawerLayout 学会使用NavigationView 学会使用ToolBar+DrawerLayout+NavigationView实现侧滑抽屉效果 学会实现Toolbar在顶部以及 ...

  2. 仿QQ5.0侧滑菜单

    一.概述 侧滑菜单现在已经非常流行了,目前大概有这么几种:最普通的侧滑,抽屉侧滑,QQ侧滑 注:本文来自慕课网 二.最普通的侧滑 先上图 代码如下: public class MainActivity ...

  3. Android开源代码分享

    一.AppMsg实现自定义Toast. github下载地址 二.CircleImageView实现带边框圆形头像.                               github下载地址 ...

  4. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

  5. 用Flutter开发的跨平台项目,完美运行在Android和IOS上,Material简洁风格,包括启动页、引导页、注册、登录、首页、体系、公众号、导航、项目,还有漂亮的妹子图库,运行极度流畅,结构清晰,代码规范,值得拥有

    Flutter学习资源汇总持续更新中...... Flutter官方网站 Flutter中文网 wendux的Flutter实战 Flutter官方exampleflutter_gallery 阿里巴 ...

  6. React Native——react-navigation的使用

    在 React Native 中,官方已经推荐使用 react-navigation 来实现各个界面的跳转和不同板块的切换. react-navigation 主要包括三个组件: StackNavig ...

  7. HBuilder mui 手机app开发 Android手机app开发 ios手机app开发

    经过一段时间的学习,做公司项目,对mui框架有了更加深入完整的了解,其实刚开始接触HBuilder中的mui框架只是简单的了解,并没有深入的研究,后来由于工作的需求,不得不深入研究,并运用的项目中去. ...

  8. Android Jetpack Compose 引入示例工程

    引入 Jetpack Compose 示例工程 去GitHub上找到Compose的示例工程 https://github.com/android/compose-samples ,clone到本地 ...

  9. iOS:抽屉侧滑动画两种形式(1、UIView侧滑 2、ViewController侧滑)

    前言: 在iOS中抽屉动画是很常用的一种技术,使用它有很炫的体验效果,为app增添特色,形式就两种,一个是UIView的侧滑,另一个就是ViewController的侧滑. 实现方式: 抽屉侧滑动画有 ...

随机推荐

  1. jquery获取html元素的绝对位置和相对位置的方法

    绝对位置坐标: 代码如下: $("#elem").offset().top$("#elem").offset().left 相对父元素的位置坐标: 代码如下: ...

  2. 51地图标注接口(EZMarker API)

    功能 在很多时候,您需要您的用户标出一个位置,比如:一个房地产网站,用户在登记新楼盘的时候,就需要在地图上标出这个楼盘的位置,这个时候就可以用到本接口. 地图标注接口(EZMarker API)是我要 ...

  3. linux mint 19解决 输入法问题

    安装搜狗后出现 You're currently running Fcitx with GUI, but fcitx-configtool couldn't be found, the package ...

  4. json_encode用法

    1.从数据库中查询出来的数据,放在数组中 $query=mysql_query($SQL); while($row = mysql_fetch_array($query)){         $xda ...

  5. [转]ASP.NET MVC 5 - 给电影表和模型添加新字段

    在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...

  6. 南京IT企业环境之最深心得体会

    我是南京做嵌入式的. 之前搞过一年的PC平台Linux内核开发,Linux内核态的仅仅要不是非常复杂的BUG还是能修复的.一年的Linux用户态软件开发. 然后近期搞了两年ARM嵌入式开发. 做的CM ...

  7. 【RF库Collections测试】Copy Dictionary

    Name: Copy DictionarySource:Collections <test library>Arguments:[ dictionary ]Returns a copy o ...

  8. git 提交代码出现git Permission to Xx denied to Xx 错误

    http://blog.csdn.net/chen_xi_hao/article/details/71172279

  9. coreseek/sphinx CentOS6.4下安装

    一.在CentOS6.4下安装coreseek之前需要预先安装以下软件 1.打开终端 输入 su 获取管理员权限 2.输入命令 yum install make gcc g++ gcc-c++ lib ...

  10. php学习五:数组操作

    前言:由于之前接触过js,所以在学习php的时候,里面继承了js的许多方法,所以数组里面的许多操作和js很相似,但是却比js里面多的多,个人感觉php是集成数组方法最多的一个 ,今天学习的有二十余个, ...