利用DrawerLayout和触摸事件分发实现抽屉侧滑效果
作者:Allen-华 时间:2022-11-09 04:45:51
本文实例为大家分享了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>
来源:http://blog.csdn.net/carry_code/article/details/51699249
标签:DrawerLayout,抽屉侧滑
0
投稿
猜你喜欢
Java可变个数形参的方法实例代码
2023-01-15 18:35:56
c# DataDirectory的用法
2021-07-30 14:36:22
IDEA如何进行全局搜索图文教程
2022-10-14 13:39:45
Android中方法数超限问题与启动优化详解
2023-03-05 17:55:41
在类库或winform项目中打开另一个winform项目窗体的方法
2023-08-14 06:47:31
Android 性能优化系列之bitmap图片优化
2023-11-05 14:03:20
Java基础详解之内存泄漏
2022-04-14 03:45:46
java后端+前端使用WebSocket实现消息推送的详细流程
2022-04-24 12:34:53
Android中切换到主线程执行的方法
2023-08-19 07:02:28
一次排查@CacheEvict注解失效的经历及解决
2023-11-13 12:07:23
Android Studio ADB网络调试汇总
2023-07-13 22:10:07
Android中RecyclerView布局代替GridView实现类似支付宝的界面
2021-08-20 02:32:34
arthas jprofiler做复杂链路的调用分析
2022-01-15 12:01:25
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
Android简单实现 缓存数据
2022-11-04 11:30:54
android开机自动启动app的解决方法
2021-08-23 18:56:51
事务在c#中的使用
2021-11-27 23:30:29
浅析SpringBoot2.4 静态资源加载问题
2023-01-29 11:04:33
Java实现高校教务系统
2022-05-16 04:24:17
将JavaDoc注释生成API文档的操作
2023-06-16 18:24:06