利用DrawerLayout和触摸事件分发实现抽屉侧滑效果

利用DrawerLayout和触摸事件分发实现抽屉侧滑效果,第1张

概述本文实例为大家分享了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>

总结

以上是内存溢出为你收集整理的利用DrawerLayout和触摸事件分发实现抽屉侧滑效果全部内容,希望文章能够帮你解决利用DrawerLayout和触摸事件分发实现抽屉侧滑效果所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1144046.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存