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

本文实例为大家分享了DrawerLayout和触摸事件分发实现抽屉侧滑效果的具体代码,供大家参考,具体内容如下

效果展示

e31452807faf7a5cbc1f46c698980671.gif

还是看代码实在,直接上菜了。

1.MainActivity的代码:

public class MainActivity extends AppCompatActivity implements MyDraweLayout.GetPositionCallback {

private List 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 = 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 imageList;

private final Context contex;

public MyAdapter(Context context, List 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布局:

android:id="@+id/mydrawelayout"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="250dp"

android:id="@+id/viewpager"/>

android:background="@mipmap/ic_launcher"

android:layout_width="300dp"

android:layout_gravity = "start"

android:layout_height="match_parent"

android:layout_below="@+id/viewpager"

/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于Android侧滑菜单,抽屉效果可以通过使用Android提供的DrawerLayout实现。以下是一些基本的步骤: 1. 在您的布局文件中,使用DrawerLayout作为根布局,并将主要内容和侧滑菜单分别放置在该布局中的两个子布局中。 ```xml <androidx.drawerlayout.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"> <!-- 主要内容 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main content here --> </LinearLayout> <!-- 侧滑菜单 --> <LinearLayout android:id="@+id/drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start"> <!-- Your drawer content here --> </LinearLayout> </androidx.drawerlayout.widget.DrawerLayout> ``` 2. 在Java代码中,找到DrawerLayout并设置监听器以响应侧滑手势。 ```java DrawerLayout drawerLayout = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer); drawerLayout.addDrawerListener(toggle); toggle.syncState(); ``` 3. 在您的manifest文件中,确保您的活动具有适当的主题,并且已启用ActionBar。 ```xml <activity ... android:theme="@style/AppTheme.NoActionBar"> ... </activity> ``` 这样,您就可以在您的应用程序中实现一个简单的侧滑菜单抽屉效果了。您可以在侧滑菜单中放置您的导航菜单或其他相关内容。希望这能帮到您!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值