您好,登錄后才能下訂單哦!
本文實例為大家分享了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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。