android自定義左側(cè)滑出菜單效果
這里給大家提供一個類似QQ聊天那種可以左側(cè)滑出菜單的自定義控件。希望對大家有幫助。參考了一些網(wǎng)友的做法,自己整理優(yōu)化了一下,用法非常簡單,就一個類,不需要自己寫任何的代碼,只要添加上布局就能實(shí)現(xiàn)側(cè)滑菜單效果,非常方便。不多說,一看就懂。
先來看看效果:

先看看實(shí)現(xiàn):
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進(jìn)行操作
? ? 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());
? ? ? ? }
? ? }
?
? ? //滑動關(guān)閉方法
? ? 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;//點(diǎn)擊關(guān)閉后不執(zhí)行其他click事件
? ? ? ? ? ? ? ? ? ? }else if(isOpen()&&ev.getX()<menuView.getLeft()){
? ? ? ? ? ? ? ? ? ? ? ? mDragHelper.smoothSlideViewTo(contentView, getPaddingLeft(), getPaddingTop());
? ? ? ? ? ? ? ? ? ? ? ? postInvalidate();
? ? ? ? ? ? ? ? ? ? ? ? return true;//點(diǎn)擊關(guān)閉后不執(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會導(dǎo)致關(guān)閉失效
? ? ? ? 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 {
?
? ? ? ? /**
? ? ? ? ?* 拖動中,可根據(jù)offset 進(jìn)行其他動畫
? ? ? ? ?* @param layout
? ? ? ? ?* @param offset 偏移量
? ? ? ? ?*/
? ? ? ? void onUpdate(SwipeDragLayout layout, float offset);
?
? ? ? ? /**
? ? ? ? ?* 展開完成
? ? ? ? ?* @param layout
? ? ? ? ?*/
? ? ? ? void onOpened(SwipeDragLayout layout);
?
? ? ? ? /**
? ? ? ? ?* 關(guān)閉完成
? ? ? ? ?* @param layout
? ? ? ? ?*/
? ? ? ? void onClosed(SwipeDragLayout layout);
?
? ? ? ? /**
? ? ? ? ?* 點(diǎn)擊內(nèi)容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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android左右滑出菜單實(shí)例分析
- Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡單方式
- Android實(shí)現(xiàn)頂部導(dǎo)航菜單左右滑動效果
- Android實(shí)現(xiàn)自定義滑動式抽屜菜單效果
- android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例
- Android側(cè)滑菜單之DrawerLayout用法詳解
- android RecyclerView側(cè)滑菜單,滑動刪除,長按拖拽,下拉刷新上拉加載
- Android滑動優(yōu)化高仿QQ6.0側(cè)滑菜單(滑動優(yōu)化)
- Android仿微信滑動彈出編輯、刪除菜單效果、增加下拉刷新功能
- Android利用滑動菜單框架實(shí)現(xiàn)滑動菜單效果
相關(guān)文章
Flutter實(shí)現(xiàn)webview與原生組件組合滑動的示例代碼
這篇文章主要介紹了Flutter實(shí)現(xiàn)webview與原生組件組合滑動的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實(shí)例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
Android-Service實(shí)現(xiàn)手機(jī)壁紙自動更換
這篇文章主要為大家詳細(xì)介紹了Android-Service實(shí)現(xiàn)手機(jī)壁紙自動更換,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Flutter使用Overlay與ColorFiltered新手引導(dǎo)實(shí)現(xiàn)示例
這篇文章主要介紹了Flutter使用Overlay與ColorFiltered新手引導(dǎo)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
快速解決Android7.0下沉浸式狀態(tài)欄變灰的問題
下面小編就為大家分享一篇快速解決Android7.0下沉浸式狀態(tài)欄變灰的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
ViewPager頂部導(dǎo)航欄聯(lián)動效果(標(biāo)題欄條目多)
這篇文章主要介紹了ViewPager頂部導(dǎo)航欄聯(lián)動效果,代碼簡單易懂,感興趣的朋友參考下吧2016-08-08

