android自定義左側滑出菜單效果
這里給大家提供一個類似QQ聊天那種可以左側滑出菜單的自定義控件。希望對大家有幫助。參考了一些網友的做法,自己整理優(yōu)化了一下,用法非常簡單,就一個類,不需要自己寫任何的代碼,只要添加上布局就能實現側滑菜單效果,非常方便。不多說,一看就懂。
先來看看效果:
先看看實現:
package com.kokjuis.travel.customView; ? import android.content.Context; import android.content.res.TypedArray; import android.graphics.Point; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; ? import com.kokjuis.travel.R; ? /** ?* <com.kokjuis.travel.customView.SwipeDragLayout ?android:id="@+id/swip_layout" ?android:layout_width="match_parent" ?android:layout_height="70dp" ?android:clickable="true" ?app:ios="true" ?app:click_to_close="true" ?> ? ?<LinearLayout ?android:id="@+id/id_back" ?android:layout_width="match_parent" ?android:layout_height="wrap_content" ?android:gravity="right"> ? ?<Button ?android:id="@+id/btn_delete" ?android:layout_width="70dp" ?android:layout_height="70dp" ?android:background="#00ffff" ?android:text="刪除" /> ? ?<Button ?android:id="@+id/btn_move" ?android:layout_marginLeft="1dp" ?android:layout_width="70dp" ?android:layout_height="70dp" ?android:background="#00ffff" ?android:text="移動" /> ? ?</LinearLayout> ? ?<RelativeLayout ?android:id="@+id/id_front" ?android:layout_width="match_parent" ?android:layout_height="wrap_content" ?android:background="@color/white"> ?.........你的布局 ? ?</RelativeLayout> ? ?</com.kokjuis.travel.customView.SwipeDragLayout> ?* ?* ?* ?* 用法就是直接用這個控件把需要的布局包起來 ?* ?可滑動的layout extends FrameLayout ?*/ public class SwipeDragLayout extends FrameLayout { ? ? ? private static SwipeDragLayout mCacheView; ? ? private View contentView;//列表的view ? ? private View menuView;//滑出菜單的view ? ? private ViewDragHelper mDragHelper; ? ? private Point originPos = new Point(); ? ? private boolean isOpen, ios, clickToClose; ? ? private float offset; ? ? private float needOffset = 0.2f; ? ? private SwipeListener mListener; ? ? ? public SwipeDragLayout(Context context) { ? ? ? ? this(context, null); ? ? } ? ? ? public SwipeDragLayout(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? ? public SwipeDragLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? ? TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SwipeDragLayout); ? ? ? ? needOffset = array.getFloat(R.styleable.SwipeDragLayout_need_offset, 0.2f); ? ? ? ? //是否有回彈效果 ? ? ? ? ios = array.getBoolean(R.styleable.SwipeDragLayout_ios, false); ? ? ? ? clickToClose = array.getBoolean(R.styleable.SwipeDragLayout_click_to_close, false); ? ? ? ? init(); ? ? ? ? array.recycle(); ? ? } ? ? ? public static SwipeDragLayout getmCacheView() { ? ? ? ? return mCacheView; ? ? } ? ? ? //初始化dragHelper,對拖動的view進行操作 ? ? private void init() { ? ? ? ? mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean tryCaptureView(View child, int pointerId) { ? ? ? ? ? ? ? ? return child == contentView; ? ? ? ? ? ? } ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onViewReleased(View releasedChild, float xvel, float yvel) { ? ? ? ? ? ? ? ? if (releasedChild == contentView) { ? ? ? ? ? ? ? ? ? ? if (isOpen()) { ? ? ? ? ? ? ? ? ? ? ? ? if (offset != 1 && offset > (1 - needOffset)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? open(); ? ? ? ? ? ? ? ? ? ? ? ? } else if (offset == 1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (clickToClose) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? close(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? if (offset != 0 && offset < needOffset) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? close(); ? ? ? ? ? ? ? ? ? ? ? ? } else if (offset == 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? getParent().requestDisallowInterceptTouchEvent(false); ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? open(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? Log.d("Released and isOpen", "" + isOpen); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (mListener != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mListener.onOpened(SwipeDragLayout.this); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? invalidate(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public int clampViewPositionHorizontal(View child, int left, int dx) { ? ? ? ? ? ? ? ? //滑動距離,如果啟動效果,則可滑動3/2倍菜單寬度的距離 ? ? ? ? ? ? ? ? final int leftBound = getPaddingLeft() - (ios ? menuView.getWidth() * 3 / 2 : menuView.getWidth()); ? ? ? ? ? ? ? ? final int rightBound = getWidth() - child.getWidth(); ? ? ? ? ? ? ? ? final int newLeft = Math.min(Math.max(left, leftBound), rightBound); ? ? ? ? ? ? ? ? return newLeft; ? ? ? ? ? ? } ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public int getViewHorizontalDragRange(View child) { ? ? ? ? ? ? ? ? return contentView == child ? menuView.getWidth() : 0; ? ? ? ? ? ? } ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { ? ? ? ? ? ? ? ? final int childWidth = menuView.getWidth(); ? ? ? ? ? ? ? ? offset = -(float) (left - getPaddingLeft()) / childWidth; ? ? ? ? ? ? ? ? //offset can callback here ? ? ? ? ? ? ? ? if (mListener!=null){ ? ? ? ? ? ? ? ? ? ? mListener.onUpdate(SwipeDragLayout.this,offset); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? } ? ? ? public void setClickToClose(boolean clickToClose) { ? ? ? ? this.clickToClose = clickToClose; ? ? } ? ? ? public void setIos(boolean ios) { ? ? ? ? this.ios = ios; ? ? } ? ? ? public boolean isOpen() { ? ? ? ? return isOpen; ? ? } ? ? ? public void open() { ? ? ? ? mCacheView = SwipeDragLayout.this; ? ? ? ? mDragHelper.settleCapturedViewAt(originPos.x - menuView.getWidth(), originPos.y); ? ? ? ? isOpen = true; ? ? } ? ? ? public void smoothOpen(boolean smooth) { ? ? ? ? mCacheView = SwipeDragLayout.this; ? ? ? ? if (smooth) { ? ? ? ? ? ? mDragHelper.smoothSlideViewTo(contentView, originPos.x - menuView.getWidth(), originPos.y); ? ? ? ? } else { ? ? ? ? ? ? contentView.layout(originPos.x - menuView.getWidth(), originPos.y, menuView.getLeft(), menuView.getBottom()); ? ? ? ? } ? ? } ? ? ? //滑動關閉方法 ? ? private void smoothClose(boolean smooth) { ? ? ? ? if (smooth) { ? ? ? ? ? ? mDragHelper.smoothSlideViewTo(contentView, getPaddingLeft(), getPaddingTop()); ? ? ? ? ? ? postInvalidate(); ? ? ? ? } else { ? ? ? ? ? ? contentView.layout(originPos.x, originPos.y, menuView.getRight(), menuView.getBottom()); ? ? ? ? } ? ? ? ? isOpen = false; ? ? ? ? mCacheView = null; ? ? ? } ? ? public void close() { ? ? ? ? mDragHelper.settleCapturedViewAt(originPos.x, originPos.y); ? ? ? ? isOpen = false; ? ? ? ? mCacheView = null; ? ? ? ? if (mListener != null) { ? ? ? ? ? ? mListener.onClosed(SwipeDragLayout.this); ? ? ? ? } ? ? } ? ?? ? ? @Override ? ? public boolean onInterceptTouchEvent(MotionEvent ev) { ? ? ? ? switch (ev.getAction()) { ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? if (mCacheView != null) { ? ? ? ? ? ? ? ? ? ? if (mCacheView != this) { ? ? ? ? ? ? ? ? ? ? ? ? mCacheView.smoothClose(true); ? ? ? ? ? ? ? ? ? ? ? ? return true;//點擊關閉后不執(zhí)行其他click事件 ? ? ? ? ? ? ? ? ? ? }else if(isOpen()&&ev.getX()<menuView.getLeft()){ ? ? ? ? ? ? ? ? ? ? ? ? mDragHelper.smoothSlideViewTo(contentView, getPaddingLeft(), getPaddingTop()); ? ? ? ? ? ? ? ? ? ? ? ? postInvalidate(); ? ? ? ? ? ? ? ? ? ? ? ? return true;//點擊關閉后不執(zhí)行其他click事件 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ?getParent().requestDisallowInterceptTouchEvent(true); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? } ? ? ? ? return mDragHelper.shouldInterceptTouchEvent(ev); ? ? ? ? } ? ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? mDragHelper.processTouchEvent(event); ? ? ? ? return true; ? ? } ? ? ? ? @Override ? ? protected void onLayout(boolean changed, int left, int top, int right, int bottom) { ? ? ? ? super.onLayout(changed, left, top, right, bottom); ? ? ? ? ? originPos.x = contentView.getLeft(); ? ? ? ? originPos.y = contentView.getTop(); ? ? } ? ? ? @Override ? ? public void computeScroll() { ? ? ? ? if (mDragHelper.continueSettling(true)) { ? ? ? ? ? ? invalidate(); ? ? ? ? } ? ? } ? ? ? @Override ? ? protected void onFinishInflate() { ? ? ? ? super.onFinishInflate(); ? ? ? ? contentView = getChildAt(1); ? ? ? ? menuView = getChildAt(0); ? ? ? ? FrameLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ? ? ? ? params.gravity = Gravity.RIGHT; ? ? ? ? menuView.setLayoutParams(params); ? ? ? ? //重寫OnClickListener會導致關閉失效 ? ? ? ? if (contentView!=null){ ? ? ? ? ? ? contentView.setOnClickListener(new OnClickListener() { ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? ? ? if (clickToClose&&isOpen()){ ? ? ? ? ? ? ? ? ? ? ? ? smoothClose(true); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (mListener!=null){ ? ? ? ? ? ? ? ? ? ? ? ? mListener.onClick(SwipeDragLayout.this); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? } ? ? } ? ? ? @Override ? ? protected void onDetachedFromWindow() { ? ? ? ? if (mCacheView == this) { ? ? ? ? ? ? mCacheView.smoothClose(false); ? ? ? ? ? ? mCacheView = null; ? ? ? ? } ? ? ? ? super.onDetachedFromWindow(); ? ? ? } ? ? ? ? @Override ? ? public void setOnTouchListener(OnTouchListener l) { ? ? ? ? super.setOnTouchListener(l); ? ? } ? ? ? public void addListener(SwipeListener listener) { ? ? ? ? mListener = listener; ? ? } ? ? ? //滑動監(jiān)聽 ? ? public interface SwipeListener { ? ? ? ? ? /** ? ? ? ? ?* 拖動中,可根據offset 進行其他動畫 ? ? ? ? ?* @param layout ? ? ? ? ?* @param offset 偏移量 ? ? ? ? ?*/ ? ? ? ? void onUpdate(SwipeDragLayout layout, float offset); ? ? ? ? ? /** ? ? ? ? ?* 展開完成 ? ? ? ? ?* @param layout ? ? ? ? ?*/ ? ? ? ? void onOpened(SwipeDragLayout layout); ? ? ? ? ? /** ? ? ? ? ?* 關閉完成 ? ? ? ? ?* @param layout ? ? ? ? ?*/ ? ? ? ? void onClosed(SwipeDragLayout layout); ? ? ? ? ? /** ? ? ? ? ?* 點擊內容layout {@link #onFinishInflate()} ? ? ? ? ?* @param layout ? ? ? ? ?*/ ? ? ? ? void onClick(SwipeDragLayout layout); ? ? } ? }
必要的一些配置,添加到attrs.xml里:
<declare-styleable name="SwipeDragLayout"> ? ? ? ? <attr name="need_offset" format="float" /> ? ? ? ? <attr name="ios" format="boolean" /> ? ? ? ? <attr name="click_to_close" format="boolean" /> </declare-styleable>
看看用法,非常簡單,直接放在你想要的item布局里面就可以了:
?<com.kokjuis.travel.customView.SwipeDragLayout ? ? ? ? android:id="@+id/swip_layout" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="70dp" ? ? ? ? android:clickable="true" ? ? ? ? app:ios="true" ? ? ? ? app:click_to_close="true" ? ? ? ? > ? ? ? <LinearLayout ? ? ? ? android:id="@+id/id_back" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="right"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/btn_delete" ? ? ? ? ? ? android:layout_width="70dp" ? ? ? ? ? ? android:layout_height="70dp" ? ? ? ? ? ? android:background="#00ffff" ? ? ? ? ? ? android:text="刪除" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/btn_move" ? ? ? ? ? ? android:layout_marginLeft="1dp" ? ? ? ? ? ? android:layout_width="70dp" ? ? ? ? ? ? android:layout_height="70dp" ? ? ? ? ? ? android:background="#00ffff" ? ? ? ? ? ? android:text="移動" /> ? ? ? </LinearLayout> ? ? ? ? <RelativeLayout ? ? ? ? android:id="@+id/id_front" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:background="@color/white"> ? ? ? ? ? //你自己的布局..... ? ? ? ? ? </RelativeLayout> ? </com.kokjuis.travel.customView.SwipeDragLayout>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Flutter實現webview與原生組件組合滑動的示例代碼
這篇文章主要介紹了Flutter實現webview與原生組件組合滑動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結合實例形式分析了AudioManager調節(jié)音量的常用方法及相關使用技巧,需要的朋友可以參考下2016-08-08Flutter使用Overlay與ColorFiltered新手引導實現示例
這篇文章主要介紹了Flutter使用Overlay與ColorFiltered新手引導實現示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10快速解決Android7.0下沉浸式狀態(tài)欄變灰的問題
下面小編就為大家分享一篇快速解決Android7.0下沉浸式狀態(tài)欄變灰的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01ViewPager頂部導航欄聯(lián)動效果(標題欄條目多)
這篇文章主要介紹了ViewPager頂部導航欄聯(lián)動效果,代碼簡單易懂,感興趣的朋友參考下吧2016-08-08