Android侧滑导航栏的实例代码

Android侧滑导航栏的实例代码,第1张

概述今天学习的新内容是侧滑导航栏,我想大家肯定都比较熟悉了,因为这个效果在qq里面也有,最近一直跟室友们玩的游戏是快速让自己的头像的点赞量上千。当然我的效果跟qq是没有办法比的,因为那里面的功能是在是太强大了

今天学习的新内容是侧滑导航栏,我想大家肯定都比较熟悉了,因为这个效果在qq里面也有,最近一直跟室友们玩的游戏是快速让自己的头像的点赞量上千。当然我的效果跟qq是没有办法比的,因为那里面的功能是在是太强大了。下面我来展示一下我做的效果截图。

我做的界面有点丑,但是对比之前已经是有了很大的改观了。想做这样的效果的话可以ps几张比较好看的图片。

下面就是粘贴我代码的时间了。

activity_main.xml

<cn.edu.rjxy.activity.DragLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/dl" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="@drawable/bg1" androID:clickable="true" androID:orIEntation="vertical" > <!-- 左侧菜单页面 --> <include layout="@layout/leftmenu" /> <cn.edu.rjxy.activity.MyrelativeLayout  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:background="#eeeeee"  androID:orIEntation="vertical" >  <!-- headbanner -->  <include layout="@layout/mIDdlevIEw" /> </cn.edu.rjxy.activity.MyrelativeLayout></cn.edu.rjxy.activity.DragLayout>

leftmenu.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:paddingBottom="30dp" androID:paddingleft="30dp" androID:paddingtop="10dp" > <linearLayout  androID:ID="@+ID/menu_header"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:gravity="left|center"  androID:orIEntation="horizontal"  androID:padding="5dp" >  <ImageVIEw   androID:ID="@+ID/iv_headimage"   androID:layout_wIDth="55dp"   androID:layout_height="55dp"   androID:src="@drawable/ic_launcher" />  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:gravity="center|left"   androID:layout_marginleft="6dp"   androID:orIEntation="vertical" >   <TextVIEw    androID:ID="@+ID/tv_sname"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_gravity="center_vertical"    androID:text="员工名"    androID:textcolor="#ffffff"    androID:textSize="15sp" />   <TextVIEw    androID:ID="@+ID/tv_message"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_margintop="2dp"    androID:textcolor="#ffffff"    androID:text="蝴蝶为花醉,花却随风飞,花舞花落泪,花哭花瓣飞"    androID:textSize="12sp" />  </linearLayout> </linearLayout> <TextVIEw  androID:ID="@+ID/tv_setting"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_alignParentBottom="true"  androID:text="设置"  androID:drawablepadding="5dp"  androID:gravity="center"  androID:drawableleft="@drawable/ic_launcher"  androID:textcolor="#ffffff"  androID:textSize="15sp" /> <ListVIEw  androID:ID="@+ID/lv"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:layout_above="@ID/tv_setting"  androID:layout_below="@ID/menu_header"  androID:layout_marginBottom="30dp"  androID:layout_margintop="20dp"  androID:cachecolorHint="#00000000"  androID:divIDer="@null"  androID:scrollbars="none"  androID:textcolor="#ffffff" /></relativeLayout>

menuList_item_text.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:gravity="center"  androID:padding="10dp"  androID:orIEntation="horizontal" >  <ImageVIEw   androID:ID="@+ID/menu_imageVIEw1"   androID:layout_wIDth="40dp"   androID:layout_height="40dp"   androID:src="@drawable/ic_launcher" />  <TextVIEw   androID:ID="@+ID/menu_text"   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_marginleft="5dp"   androID:gravity="center_vertical"   androID:text="菜单1"   androID:textcolor="#ffffff"   androID:textSize="20sp" /> </linearLayout></linearLayout>

mIDdlevIEw.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="@androID:color/white" androID:orIEntation="vertical" > <relativeLayout  androID:layout_wIDth="fill_parent"  androID:layout_height="40dp"  androID:background="#00C5CD"  androID:orIEntation="horizontal" >  <Imagebutton   androID:ID="@+ID/menu_imgbtn"   androID:layout_wIDth="20dp"   androID:layout_height="15dp"   androID:layout_centerVertical="true"   androID:layout_marginleft="10dp"   androID:layout_margintop="1dp"   androID:background="@drawable/leftmenu_btn_selector" />  <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_centerInParent="true"   androID:text="@string/app_name"   androID:textcolor="@androID:color/white"   androID:textSize="20dp" /> </relativeLayout></linearLayout>

leftmenu_btn_selector.xml

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:drawable="@drawable/leftmenu_btn_press" androID:state_pressed="true"/> <item androID:drawable="@drawable/leftmenu_btn" androID:state_pressed="false"/></selector>

MyrelativeLayout

package cn.edu.rjxy.activity;import cn.edu.rjxy.activity.DragLayout.Status;import androID.content.Context;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.Widget.linearLayout;public class MyrelativeLayout extends linearLayout { private DragLayout dl; public MyrelativeLayout(Context context) {  super(context); } public MyrelativeLayout(Context context,AttributeSet attrs) {  super(context,attrs); } public voID setDragLayout(DragLayout dl) {  this.dl = dl; } @OverrIDe public boolean onIntercepttouchEvent(MotionEvent event) {  if (dl.getStatus() != Status.Close) {   return true;  }  return super.onIntercepttouchEvent(event); } @OverrIDe public boolean ontouchEvent(MotionEvent event) {  if (dl.getStatus() != Status.Close) {   if (event.getAction() == MotionEvent.ACTION_UP) {    dl.close();   }   return true;  }  return super.ontouchEvent(event); }}

DragLayout

package cn.edu.rjxy.activity;import com.nineoldandroIDs.vIEw.VIEwHelper;import androID.content.Context;import androID.graphics.color;import androID.graphics.PorterDuff.Mode;import androID.support.v4.vIEw.GestureDetectorCompat;import androID.support.v4.vIEw.VIEwCompat;import androID.support.v4.Widget.VIEwDragHelper;import androID.util.AttributeSet;import androID.vIEw.GestureDetector.SimpleOnGestureListener;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.FrameLayout;import androID.Widget.ImageVIEw;import androID.Widget.relativeLayout;public class DragLayout extends FrameLayout { private boolean isShowShadow = true; private GestureDetectorCompat gestureDetector; private VIEwDragHelper dragHelper; private DragListener dragListener; private int range; private int wIDth; private int height; private int mainleft; private Context context; private ImageVIEw iv_shadow; private relativeLayout vg_left; private MyrelativeLayout vg_main; private Status status = Status.Close; public DragLayout(Context context) {  this(context,null); } public DragLayout(Context context,AttributeSet attrs) {  this(context,attrs,0);  this.context = context; } public DragLayout(Context context,AttributeSet attrs,int defStyle) {  super(context,defStyle);  gestureDetector = new GestureDetectorCompat(context,new YScrollDetector());  dragHelper = VIEwDragHelper.create(this,dragHelperCallback); } class YScrollDetector extends SimpleOnGestureListener {  @OverrIDe  public boolean onScroll(MotionEvent e1,MotionEvent e2,float dx,float dy) {   return Math.abs(dy) <= Math.abs(dx);  } } private VIEwDragHelper.Callback dragHelperCallback = new VIEwDragHelper.Callback() {  @OverrIDe  public int clampVIEwpositionHorizontal(VIEw child,int left,int dx) {   if (mainleft + dx < 0) {    return 0;   } else if (mainleft + dx > range) {    return range;   } else {    return left;   }  }  @OverrIDe  public boolean tryCaptureVIEw(VIEw child,int pointerID) {   return true;  }  @OverrIDe  public int getVIEwHorizontalDragRange(VIEw child) {   return wIDth;  }  @OverrIDe  public voID onVIEwReleased(VIEw releasedChild,float xvel,float yvel) {   super.onVIEwReleased(releasedChild,xvel,yvel);   if (xvel > 0) {    open();   } else if (xvel < 0) {    close();   } else if (releasedChild == vg_main && mainleft > range * 0.3) {    open();   } else if (releasedChild == vg_left && mainleft > range * 0.7) {    open();   } else {    close();   }  }  @OverrIDe  public voID onVIEwpositionChanged(VIEw changedVIEw,int top,int dx,int dy) {   if (changedVIEw == vg_main) {    mainleft = left;   } else {    mainleft = mainleft + left;   }   if (mainleft < 0) {    mainleft = 0;   } else if (mainleft > range) {    mainleft = range;   }   if (isShowShadow) {    iv_shadow.layout(mainleft,mainleft + wIDth,height);   }   if (changedVIEw == vg_left) {    vg_left.layout(0,wIDth,height);    vg_main.layout(mainleft,height);   }   dispatchDragEvent(mainleft);  } }; public interface DragListener {  public voID onopen();  public voID onClose();  public voID onDrag(float percent); } public voID setDragListener(DragListener dragListener) {  this.dragListener = dragListener; } @OverrIDe protected voID onFinishInflate() {  super.onFinishInflate();  if (isShowShadow) {   iv_shadow = new ImageVIEw(context);   iv_shadow.setimageResource(R.drawable.shadow);   LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);   addVIEw(iv_shadow,1,lp);  }  vg_left = (relativeLayout) getChildAt(0);  vg_main = (MyrelativeLayout) getChildAt(isShowShadow ? 2 : 1);  vg_main.setDragLayout(this);  vg_left.setClickable(true);  vg_main.setClickable(true); } public VIEwGroup getVg_main() {  return vg_main; } public VIEwGroup getVg_left() {  return vg_left; } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) {  super.onSizeChanged(w,h,olDW,oldh);  wIDth = vg_left.getMeasureDWIDth();  height = vg_left.getMeasuredHeight();  range = (int) (wIDth * 0.6f); } @OverrIDe protected voID onLayout(boolean changed,int right,int bottom) {  vg_left.layout(0,height);  vg_main.layout(mainleft,height); } @OverrIDe public boolean onIntercepttouchEvent(MotionEvent ev) {  return dragHelper.shouldIntercepttouchEvent(ev)    && gestureDetector.ontouchEvent(ev); } @OverrIDe public boolean ontouchEvent(MotionEvent e) {  try {   dragHelper.processtouchEvent(e);  } catch (Exception ex) {   ex.printstacktrace();  }  return false; } private voID dispatchDragEvent(int mainleft) {  if (dragListener == null) {   return;  }  float percent = mainleft / (float) range;  animateVIEw(percent);  dragListener.onDrag(percent);  Status lastStatus = status;  if (lastStatus != getStatus() && status == Status.Close) {   dragListener.onClose();  } else if (lastStatus != getStatus() && status == Status.Open) {   dragListener.onopen();  } } private voID animateVIEw(float percent) {  float f1 = 1 - percent * 0.3f;  VIEwHelper.setScaleX(vg_main,f1);  VIEwHelper.setScaleY(vg_main,f1);  VIEwHelper.setTranslationX(vg_left,-vg_left.getWIDth() / 2.3f    + vg_left.getWIDth() / 2.3f * percent);  VIEwHelper.setScaleX(vg_left,0.5f + 0.5f * percent);  VIEwHelper.setScaleY(vg_left,0.5f + 0.5f * percent);  VIEwHelper.setAlpha(vg_left,percent);  if (isShowShadow) {   VIEwHelper.setScaleX(iv_shadow,f1 * 1.4f * (1 - percent * 0.12f));   VIEwHelper.setScaleY(iv_shadow,f1 * 1.85f * (1 - percent * 0.12f));  }  getBackground().setcolorFilter(    evaluate(percent,color.BLACK,color.transparent),Mode.SRC_OVER); } private Integer evaluate(float fraction,Object startValue,Integer endValue) {  int startInt = (Integer) startValue;  int startA = (startInt >> 24) & 0xff;  int startR = (startInt >> 16) & 0xff;  int startG = (startInt >> 8) & 0xff;  int startB = startInt & 0xff;  int endInt = (Integer) endValue;  int endA = (endInt >> 24) & 0xff;  int endR = (endInt >> 16) & 0xff;  int endG = (endInt >> 8) & 0xff;  int endB = endInt & 0xff;  return (int) ((startA + (int) (fraction * (endA - startA))) << 24)    | (int) ((startR + (int) (fraction * (endR - startR))) << 16)    | (int) ((startG + (int) (fraction * (endG - startG))) << 8)    | (int) ((startB + (int) (fraction * (endB - startB)))); } @OverrIDe public voID computeScroll() {  if (dragHelper.continueSettling(true)) {   VIEwCompat.postInvalIDateOnAnimation(this);  } } public enum Status {  Drag,Open,Close } public Status getStatus() {  if (mainleft == 0) {   status = Status.Close;  } else if (mainleft == range) {   status = Status.Open;  } else {   status = Status.Drag;  }  return status; } public voID open() {  open(true); } public voID open(boolean animate) {  if (animate) {   if (dragHelper.smoothSlIDeVIEwTo(vg_main,range,0)) {    VIEwCompat.postInvalIDateOnAnimation(this);   }  } else {   vg_main.layout(range,range * 2,height);   dispatchDragEvent(range);  } } public voID close() {  close(true); } public voID close(boolean animate) {  if (animate) {   if (dragHelper.smoothSlIDeVIEwTo(vg_main,0)) {    VIEwCompat.postInvalIDateOnAnimation(this);   }  } else {   vg_main.layout(0,height);   dispatchDragEvent(0);  } }}

MainActivity

package cn.edu.rjxy.activity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import cn.edu.rjxy.activity.DragLayout.DragListener;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.Imagebutton;import androID.Widget.linearLayout;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends Activity { /** 左边侧滑菜单 */ private DragLayout mDragLayout; private ListVIEw menuListVIEw;// 菜单列表 private Imagebutton menuSettingBtn;// 菜单呼出按钮 private linearLayout menu_header; private TextVIEw menu_setting; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  menu_setting=(TextVIEw) this.findVIEwByID(R.ID.tv_setting);  menu_header = (linearLayout) this.findVIEwByID(R.ID.menu_header);  /**   * 如果需要在别的Activity界面中也实现侧滑菜单效果,需要在布局中引入DragLayout(同本Activity方式),   * 然后在onCreate中声明使用; Activity界面部分,需要包裹在MyrelativeLayout中.   */  mDragLayout = (DragLayout) findVIEwByID(R.ID.dl);  mDragLayout.setDragListener(new DragListener() {// 动作监听     @OverrIDe     public voID onopen() {     }     @OverrIDe     public voID onClose() {     }     @OverrIDe     public voID onDrag(float percent) {     }    });  // 生成测试菜单选项数据  List<Map<String,Object>> data = getMenuData();  menuListVIEw = (ListVIEw) findVIEwByID(R.ID.lv);  menuListVIEw.setAdapter(new SimpleAdapter(this,data,R.layout.menuList_item_text,new String[] { "item","image" },new int[] { R.ID.menu_text,R.ID.menu_imageVIEw1 }));  // 添加监听,可点击呼出菜单  menuSettingBtn = (Imagebutton) findVIEwByID(R.ID.menu_imgbtn);  menuSettingBtn.setonClickListener(new OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    // Todo auto-generated method stub    mDragLayout.open();   }  });  initResIDeListener();// 自定义添加的东东 } private voID initResIDeListener() {  // Todo auto-generated method stub  // 点击个人中心  menu_header.setonClickListener(new OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    // Todo auto-generated method stub    Toast.makeText(MainActivity.this,"进入个人中心界面",Toast.LENGTH_LONG).show();   }  });  // 点击子菜单选项  menuListVIEw.setonItemClickListener(new OnItemClickListener() {   @OverrIDe   public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {    // Todo auto-generated method stub    switch (position) {    case 0:     Toast.makeText(MainActivity.this,"0",Toast.LENGTH_LONG).show();     break;    case 1:     Toast.makeText(MainActivity.this,"1",Toast.LENGTH_LONG).show();     break;    case 2:     Toast.makeText(MainActivity.this,"2",Toast.LENGTH_LONG).show();     break;    case 3:     Toast.makeText(MainActivity.this,"3",Toast.LENGTH_LONG).show();     break;    case 4:     Toast.makeText(MainActivity.this,"4",Toast.LENGTH_LONG).show();     break;    case 5:     Toast.makeText(MainActivity.this,"5",Toast.LENGTH_LONG).show();     break;    default:     break;    }   }  });  //进入设置界面  menu_setting.setonClickListener(new OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    // Todo auto-generated method stub    Toast.makeText(MainActivity.this,"进入设置界面",Toast.LENGTH_LONG).show();   }  }); } private List<Map<String,Object>> getMenuData() {  // Todo auto-generated method stub  List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();    Map<String,Object> item;  item = new HashMap<String,Object>();  item.put("item","需参加会议");  item.put("image",R.drawable.ic_launcher);  data.add(item);  item = new HashMap<String,"已参加会议");  item.put("image","个人信息完善");  item.put("image","密码修改");  item.put("image",R.drawable.ic_launcher);  data.add(item);  return data; }}

以上所述是小编给大家介绍的AndroID侧滑导航栏的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android侧滑导航栏的实例代码全部内容,希望文章能够帮你解决Android侧滑导航栏的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存