利用DrawerLayout和触摸事件分发实现抽屉侧滑效果
程序员文章站
2023-12-13 22:34:16
本文实例为大家分享了drawerlayout和触摸事件分发实现抽屉侧滑效果的具体代码,供大家参考,具体内容如下
效果展示
还是看代码实在,直接上菜了。...
本文实例为大家分享了drawerlayout和触摸事件分发实现抽屉侧滑效果的具体代码,供大家参考,具体内容如下
效果展示
还是看代码实在,直接上菜了。
1.mainactivity的代码:
public class mainactivity extends appcompatactivity implements mydrawelayout.getpositioncallback { private list<imageview> imagelist; private viewpager viewpager; private myadapter adapter; private mydrawelayout mydrawelayout; private int currentposition; @override protected void oncreate (bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager = (viewpager) findviewbyid(r.id.viewpager); mydrawelayout = (mydrawelayout) findviewbyid(r.id.mydrawelayout); initdata(); adapter = new myadapter(this, imagelist); mydrawelayout.setcallback(this); viewpager.setadapter(adapter); viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { currentposition=position; } @override public void onpagescrollstatechanged(int state) { } }); } private void initdata() { imagelist = new arraylist<imageview>(); imageview imageview = new imageview(this); imageview.setimageresource(r.mipmap.ic_launcher); imageview.setscaletype(imageview.scaletype.fit_xy); imagelist.add(imageview); imageview = new imageview(this); imageview.setimageresource(r.mipmap.ic_launcher); imageview.setscaletype(imageview.scaletype.fit_xy); imagelist.add(imageview); imageview = new imageview(this); imageview.setimageresource(r.mipmap.ic_launcher); imageview.setscaletype(imageview.scaletype.fit_xy); imagelist.add(imageview); imageview = new imageview(this); imageview.setimageresource(r.mipmap.ic_launcher); imageview.setscaletype(imageview.scaletype.fit_xy); imagelist.add(imageview); } @override public int position() { return currentposition;//todo 通过接口回调把当前位置传到mydrawelayout中 } }
2.mydrawelayout类中的代码:
public class mydrawelayout extends drawerlayout { public mydrawelayout(context context) { super(context); } public mydrawelayout(context context, attributeset attrs) { super(context, attrs); } public mydrawelayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } //todo 事件拦截 @override public boolean onintercepttouchevent(motionevent ev) { //todo 获得当前位置,进行判断 if(callback.position()==0){ return super.onintercepttouchevent(ev); }else { return false; } } public interface getpositioncallback{ int position(); } private getpositioncallback callback; public void setcallback(getpositioncallback callback){ this.callback = callback; } }
3.适配器的代码;
public class myadapter extends pageradapter { private final list<imageview> imagelist; private final context contex; public myadapter(context context, list<imageview> imagelist) { this.contex=context; this.imagelist = imagelist; } @override public int getcount() { return imagelist.size(); } @override public boolean isviewfromobject(view view, object object) { return view==object; } @override public object instantiateitem(viewgroup container, int position) { imageview imageview = imagelist.get(position); container.addview(imageview); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { //super.destroyitem(container, position, object);这行代码记得删除,不然滑到viewpager的时候会闪退哦 container.removeview(imagelist.get(position)); } }
4.xml布局:
<?xml version="1.0" encoding="utf-8"?> <com.example.a43_drawelayoutandviewpager.mydrawelayout android:id="@+id/mydrawelayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.viewpager android:layout_width="match_parent" android:layout_height="250dp" android:id="@+id/viewpager"/> </relativelayout> <imageview android:background="@mipmap/ic_launcher" android:layout_width="300dp" android:layout_gravity = "start" android:layout_height="match_parent" android:layout_below="@+id/viewpager" /> </com.example.a43_drawelayoutandviewpager.mydrawelayout>