当前位置: 移动技术网 > 移动技术>移动开发>Android > Android DrawerLayout带有侧滑功能的布局类(1)

Android DrawerLayout带有侧滑功能的布局类(1)

2019年07月24日  | 移动技术网移动技术  | 我要评论

drawerlayout顾名思义就是一个管理布局的。使用方式可以与其它的布局类类似。
drawerlayout带有滑动的功能。只要按照drawerlayout的规定布局方式写完布局,就能有侧滑的效果。
直接将drawerlayout作为根布局,然后其内部 

  第一个view为内容区域

  第二个view为左侧菜单   

       第三个view为右侧侧滑菜单 

当前第三个是可选的。
使用的包如下: 
import android.support.v4.widget.drawerlayout; 

使用这些包的时候有时有的会报错。这时候确保android.support.v4是不是最新的版本。
 可以更新一下support包,文件存放在sdk/extres/support中。
 然后可以通过eclipse>project right click>android tools>add support library…
 或者可以直接把文件复制到project中libs文件夹中。 

<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/content_frame"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
 <listview
  android:id="@+id/left_drawer"
  android:layout_width="240dp"
  android:layout_height="match_parent"
  android:layout_gravity="start"
  android:choicemode="singlechoice"
  android:divider="@android:color/transparent"
  android:dividerheight="0dp"
  android:background="#111"/>
</android.support.v4.widget.drawerlayout>

drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right.
 (or start/end on platform versions that support layout direction.)
 也就是说
  android:layout_gravity="start" 相当于左侧的menu向右滑动即显示菜单,left/start(right/end)
那么从布局文件中可知:
 framelayout是内容区, listview是左侧菜单。
我们需做一个fragment来装载内容: 

public class pagefragment extends fragment {
  public final static string item_position_number = "item_position_num";
  public pagefragment(){}
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container,
    bundle savedinstancestate) {
   view convertview = inflater.inflate(r.layout.page_fragment_layout, null);
   textview tv = (textview) convertview.findviewbyid(r.id.textview);
   int num = getarguments().getint(item_position_number);
   //从res/array中获取list数据
   string[] dynastylist = getresources().getstringarray(r.array.list_item);
   tv.settext(dynastylist[num]);
   return convertview;
  }
 }

代码中可以看出当我们在左侧菜单中选择selectitem时会把对应的值显示到内容区。
代码中的page_fragment_layout.xml仅是framelayout内加一个textview所以就不贴代码了。
接下来我们需要把listview进行填充数据。 

private listview menulist;
private string[] mmenutitles;
private string[] historytitles;
private string[] musictitles;
private string[] movietitles;
private string[] listtitles;
     // 历史栏
  historytitles = getresources().getstringarray(r.array.history);
  // 音乐栏
  musictitles = getresources().getstringarray(r.array.music);
  // 电影栏
  movietitles = getresources().getstringarray(r.array.movie);
  // 标题数组
  mmenutitles = getresources().getstringarray(r.array.title);
  // 每一項的標題
  listtitles = getresources().getstringarray(r.array.list_item);

  drawlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);
  menulist = (listview) findviewbyid(r.id.left_menu);

  // 设置菜单阴影效果
  // drawlayout.setdrawershadow(r.drawable.drawer_shadow,
  // gravitycompat.start);
  list<item> list = new arraylist<item>();
  // 菜单加入历史标题和历史项
  headeritem historyheader = new headeritem(mmenutitles[0]);
  list.add(historyheader);
  for (int i = 0; i < historytitles.length; i++) {
   eventitem historyitem = new eventitem(historytitles[i]);
   list.add(historyitem);
  }

  // 菜单加入音乐标题和音乐项
  headeritem musicheader = new headeritem(mmenutitles[1]);
  list.add(musicheader);
  for (int i = 0; i < musictitles.length; i++) {
   eventitem musicitem = new eventitem(musictitles[i]);
   list.add(musicitem);
  }

  // 菜单加入电影标题和电影项
  headeritem movieheader = new headeritem(mmenutitles[2]);
  list.add(movieheader);
  for (int i = 0; i < movietitles.length; i++) {
   eventitem movieitem = new eventitem(movietitles[i]);
   list.add(movieitem);
  }

  mylistadapter adapter = new mylistadapter(this, list);
  menulist.setadapter(adapter);

这个数据填充有点麻烦。自定义listadapter然后进行适配。
 数据在res/values/arrays.xml中 

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="history">
  <item >三国</item>
  <item >楚汉</item>
  <item >春秋</item>
  <item >战国</item>
 </string-array>
  <string-array name="music">
  <item >爵士</item>
  <item >古典</item>
  <item >现代</item>
  <item >民谣</item>
 </string-array>
  <string-array name="movie">
  <item >悬疑</item>
  <item >爱情</item>
  <item >历史</item>
  <item >恐怖</item>
 </string-array>
 <string-array name="title">
  <item >历史</item>
  <item >音樂</item>
  <item >电影</item>
 </string-array>
 <string-array name="list_item">
  <item >歷史</item>
  <item >三国</item>
  <item >楚汉</item>
  <item >春秋</item>
  <item >战国</item>
  <item >音樂</item>
  <item >爵士</item>
  <item >古典</item>
  <item >现代</item>
  <item >民谣</item>
  <item >電影</item>
  <item >悬疑</item>
  <item >爱情</item>
  <item >历史</item>
  <item >恐怖</item>
 </string-array>
</resources>

然后就是listview的监听: 

  private void initlistener() {
  // 菜单单击事件监听器
  menulist.setonitemclicklistener(new draweritemclicklistener());
 }

 /* the click listner for listview in the navigation drawer */
 private class draweritemclicklistener implements
   listview.onitemclicklistener {
  @override
  public void onitemclick(adapterview<?> parent, view view, int position,
    long id) {
   log.i("light", "position:" + position);
   selectitem(position);
  }
 }

 private void selectitem(int position) {
  // update the main content by replacing fragments
  pagefragment fragment = new pagefragment();
  // 将当前选择的项传递到fragment
  bundle args = new bundle();
  args.putint(pagefragment.item_position_number, position);
  fragment.setarguments(args);

  fragmenttransaction ft = mainactivity.this.getsupportfragmentmanager()
    .begintransaction();
  ft.replace(r.id.content_frame, fragment).commit();

  drawlayout.closedrawer(menulist);
  // update selected item and title, then close the drawer
  menulist.setitemchecked(position, true);
  // 注意这里改变的是actionbar的标题
  getactionbar().settitle(listtitles[position]);
 }

我们关心的是当某一个item被点击时会发生什么即代码: 

private void selectitem(int position) {
  // update the main content by replacing fragments
  pagefragment fragment = new pagefragment();
  // 将当前选择的项传递到fragment
  bundle args = new bundle();
  args.putint(pagefragment.item_position_number, position);
  fragment.setarguments(args);

  fragmenttransaction ft = mainactivity.this.getsupportfragmentmanager()
    .begintransaction();
  ft.replace(r.id.content_frame, fragment).commit();

  drawlayout.closedrawer(menulist);
  // update selected item and title, then close the drawer
  menulist.setitemchecked(position, true);
  // 注意这里改变的是actionbar的标题
  getactionbar().settitle(listtitles[position]);
 }

从代码中可以看出 
1. 首先我们先通过new pagefragment();获取内容区。 
2. 通过bundle把数据打包起来然后注入fragment.setarguments(args);中这样fragment就能获取到此数据。 
    在fragment类中通过getarguments().getint(item_position_number);可以获取传递的值。 
3. 然后通过ft.replace(r.id.content_frame, fragment).commit();把内容替换成之前定义的pagefragment  
4. 关闭菜单通过drawlayout.closedrawer(menulist); 整个代码中我们仅用drawlayout这一个函数 
5. 同时把actionbar的标题改为selecteditem对应的值。 
*这时有人会问我们怎么没有listview与drawerlayout进行绑定的操作。我们在之前也说过drawerlayout中的第二个开始即是菜单view,内部已经绑定好了。 
就这些内容可以实现左右侧滑动菜单效果了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网