├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── ljp │ │ └── swipemenulayout │ │ ├── ItemAdapter.java │ │ ├── ItemAdapter2.java │ │ ├── MainActivity.java │ │ └── ToastUtil.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_main.xml │ ├── layout_item.xml │ └── layout_item2.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── attrs.xml │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gif └── gif1.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── swipemenu ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── cn │ └── ljp │ └── swipemenu │ ├── SwipeMenuLayout.java │ └── SwipeMenuStateListener.java └── res └── values └── attrs.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android侧滑菜单-SwipeMenuLayout 2 | 3 | SwipeMenuLayout是一个零耦合的侧滑菜单,使用方式及其简单!只需要正常编写xml布局文件即可。 4 | 5 | ![](https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/master/gif/gif1.gif) 6 | 7 | 8 | ## 目前功能如下 9 | - 支持启用或禁用侧滑菜单 10 | - 支持菜单在条目的左边或者右边 11 | - 支持滑动阻塞或非阻塞 12 | - 支持点击了menu后是否自动关闭menu 13 | - 支持menu打开和关闭的回调监听 14 | - 可快速打开和关闭menu 15 | 16 | ## 简单用例 17 | - 只需正常编写xml文件即可 18 | - SwipeMenuLayout中第一个view为item布局,后面的为menu布局 19 | - 关于布局的宽高问题,特殊情况简单说明一下 20 | 1. item的布局宽度始终会以match_parent测量 21 | 2. SwipeMenuLayout如果宽为warp_content的话,以父view的宽度为主,基本也是match_parent 22 | 3. SwipeMenuLayout高度为wrap_content的话,分两种情况,第一种是下面的view高度都是warp_content,那高度就是warp_content;如果其中的view高度有值的话,以数值最大的那个为SwipeMenuLayout的高度;如果SwipeMenuLayout的高度有准确值,例如60dp,下面的view高度即便超过60dp依旧也为60dp; 23 | 24 | ---------- 25 | 26 | //第一步 项目根路径build中添加 27 | allprojects { 28 | repositories { 29 | ... 30 | maven { url 'https://jitpack.io' } 31 | } 32 | } 33 | //第二步 moudler中依赖 34 | dependencies { 35 | implementation 'com.github.ljphawk:SwipeMenuLayout:1.05' 36 | } 37 | 38 | 39 | 40 | 51 | 52 | 53 | 57 | 58 | 63 | 64 | 65 | 71 | 72 | 78 | 79 | 80 | 81 | 注:点击事件和长按事件请设置给SwipeMenuLayout 82 | ## 属性说明 83 | 84 | **代码示例** 85 | 86 | 有set方法就有会对应的get方法,get方法我就不贴了 87 | set方法支持链式调用 88 | 89 | SwipeMenuLayout swipeMenuLayout = findViewById(R.id.swipe_menu_layout); 90 | //是否启用侧滑菜单 默认是启用的 91 | swipeMenuLayout.setEnableSwipe(true); 92 | //设置菜单是否在item的左边,在左边的话是向右滑动,反之左滑(默认在item右边) 93 | swipeMenuLayout.setEnableLeftMenu(false); 94 | /* 95 | 是否开启阻塞效果 默认开启。 96 | 举个例子 比如你把item1的侧滑菜单划出来了,你继续滑动item2的, 97 | 这是默认是开启阻塞效果的,在你滑动item2的时候 会先关闭item1的菜单, 98 | 需要再次滑动item2才可以(qq是这样子的) 99 | 如果关闭这个效果,你在滑动item2的同时会同时关闭item1 100 | */ 101 | swipeMenuLayout.setOpenChoke(true); 102 | /* 103 | 是否开启点击菜单后自动关闭菜单,默认false. 104 | 思来想去决定还是把这个交给开发者决定应该在什么合适的时候来关闭 105 | */ 106 | swipeMenuLayout.setClickMenuAndClose(false); 107 | //动画方式展开菜单 默认300ms 108 | swipeMenuLayout.expandMenuAnim(); 109 | //动画方式关闭菜单 默认300ms 110 | swipeMenuLayout.closeMenuAnim(); 111 | //快速打开菜单 0s 112 | swipeMenuLayout.quickExpandMenu(); 113 | //快速关闭菜单 0s 114 | swipeMenuLayout.quickCloseMenu(); 115 | //获取当前菜单是否展开 116 | swipeMenuLayout.isExpandMenu(); 117 | //菜单打开关闭的监听。 true打开了 false关闭了 118 | swipeMenuLayout.setSwipeMenuStateListener(new SwipeMenuStateListener()); 119 | 120 | **xml代码设置** 121 | 122 | 123 | 134 | 135 | 139 | 140 | 145 | 146 | 147 | 153 | 154 | 160 | 161 | 162 | 163 | **属性表格 Attributes** 164 | 165 | name | format | default | description 166 | -|-|-|- 167 | isEnableSwipe | boolean | true |是否启用侧滑 168 | isEnableLeftMenu | boolean | false |菜单是否放置左边 169 | isClickMenuAndClose | boolean | false |点击菜单后是否自动关闭 170 | isOpenChoke | boolean | true |是否开启阻塞 171 | 172 | 173 | **Method** 174 | 175 | name | format | description 176 | -|-|- 177 | setEnableSwipe | SwipeMenuLayout | 是否启用侧滑 178 | setEnableLeftMenu | SwipeMenuLayout | 菜单是否放置左边 179 | setClickMenuAndClose | SwipeMenuLayout | 点击菜单后是否自动关闭 180 | setOpenChoke | SwipeMenuLayout | 是否开启阻塞 181 | expandMenuAnim | | 动画方式展开菜单 182 | closeMenuAnim | | 动画方式关闭菜单 183 | quickExpandMenu | | 快速打开菜单 184 | quickCloseMenu | | 快速关闭菜单 185 | isExpandMenu | | 获取当前菜单是否展开 186 | setSwipeMenuStateListener | SwipeMenuStateListener | 菜单打开关闭的监听 -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.ljp.swipemenulayout" 7 | minSdkVersion 19 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:design:27.1.1' 24 | implementation project(':swipemenu') 25 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46' 26 | } 27 | 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/ljp/swipemenulayout/ItemAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ljp.swipemenulayout; 2 | 3 | 4 | import android.content.Context; 5 | import android.support.annotation.NonNull; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | import cn.ljp.swipemenu.SwipeMenuLayout; 18 | 19 | /* 20 | *@创建者 L_jp 21 | *@创建时间 2019/6/9 13:31. 22 | *@描述 23 | * 24 | *@更新者 $Author$ 25 | *@更新时间 $Date$ 26 | *@更新描述 27 | */ 28 | public class ItemAdapter extends RecyclerView.Adapter { 29 | 30 | private static final String TAG = "ItemAdapter"; 31 | private Context mContext; 32 | private List mShowItems = new ArrayList<>(); 33 | 34 | public ItemAdapter(Context context) { 35 | mContext = context; 36 | for (int i = 0; i < 50; i++) { 37 | mShowItems.add("item = " + i); 38 | } 39 | } 40 | 41 | @NonNull 42 | @Override 43 | public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 44 | View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false); 45 | return new ViewHolder(view); 46 | } 47 | 48 | @Override 49 | public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) { 50 | final String item = mShowItems.get(i); 51 | viewHolder.mSwipe.setEnableLeftMenu(i % 4 == 0); 52 | String text = item + ",菜单在" + (i % 4 == 0 ? "左; " : "右; "); 53 | if (i % 3 == 0) { 54 | viewHolder.mSwipe.setOpenChoke(false); 55 | text += "我是无阻塞的; "; 56 | } else { 57 | viewHolder.mSwipe.setOpenChoke(true); 58 | text += "我是有阻塞的; "; 59 | } 60 | if (i % 5 == 0) { 61 | viewHolder.mSwipe.setClickMenuAndClose(true); 62 | text += "点击我可以展开菜单"; 63 | } else { 64 | viewHolder.mSwipe.setClickMenuAndClose(false); 65 | } 66 | viewHolder.mTv1.setText(text); 67 | 68 | viewHolder.mSwipe.setOnClickListener(new View.OnClickListener() { 69 | // viewHolder.mLl_item.setOnClickListener(new View.OnClickListener() { 70 | @Override 71 | public void onClick(View v) { 72 | if (i % 5 == 0) { 73 | viewHolder.mSwipe.expandMenuAnim(); 74 | } else { 75 | viewHolder.mSwipe.closeMenuAnim(); 76 | ToastUtil.showToast(mContext, "点击了条目" + i); 77 | Log.d(TAG, "onClick: 点击了条目" + i); 78 | } 79 | } 80 | }); 81 | viewHolder.mSwipe.setOnLongClickListener(new View.OnLongClickListener() { 82 | @Override 83 | public boolean onLongClick(View v) { 84 | Toast.makeText(mContext, "我长按了" + item, Toast.LENGTH_SHORT).show(); 85 | Log.d(TAG, "onLongClick: 我长按了" + item); 86 | return true; 87 | } 88 | }); 89 | viewHolder.mTv2.setOnClickListener(new View.OnClickListener() { 90 | @Override 91 | public void onClick(View v) { 92 | ToastUtil.showToast(mContext, "点击了菜单->取消关注"); 93 | Log.d(TAG, "onClick: 点击了菜单->取消关注"); 94 | } 95 | }); 96 | viewHolder.mTv3.setOnClickListener(new View.OnClickListener() { 97 | @Override 98 | public void onClick(View v) { 99 | ToastUtil.showToast(mContext, "点击了菜单->删除"); 100 | Log.d(TAG, "onClick: 点击了菜单->删除"); 101 | mShowItems.remove(i); 102 | //用这个 主要是解决了之前有个删除后刷新,其他条目菜单也会做个菜单动画bug 103 | notifyDataSetChanged(); 104 | //或者 notifyItemChanged(i); 也行 105 | } 106 | }); 107 | } 108 | 109 | @Override 110 | public int getItemCount() { 111 | return mShowItems.size(); 112 | } 113 | 114 | class ViewHolder extends RecyclerView.ViewHolder { 115 | 116 | private final View mLl_item; 117 | private final TextView mTv1; 118 | private final View mTv2; 119 | private final View mTv3; 120 | private final SwipeMenuLayout mSwipe; 121 | 122 | public ViewHolder(@NonNull View view) { 123 | super(view); 124 | mLl_item = view.findViewById(R.id.ll_item); 125 | mTv1 = view.findViewById(R.id.tv_content); 126 | mTv2 = view.findViewById(R.id.tv_menu1); 127 | mTv3 = view.findViewById(R.id.tv_menu2); 128 | mSwipe = view.findViewById(R.id.swipe_menu_layout); 129 | } 130 | } 131 | } 132 | 133 | -------------------------------------------------------------------------------- /app/src/main/java/com/ljp/swipemenulayout/ItemAdapter2.java: -------------------------------------------------------------------------------- 1 | package com.ljp.swipemenulayout; 2 | 3 | 4 | import android.widget.TextView; 5 | 6 | import com.chad.library.adapter.base.BaseQuickAdapter; 7 | import com.chad.library.adapter.base.BaseViewHolder; 8 | 9 | import java.util.List; 10 | 11 | import cn.ljp.swipemenu.SwipeMenuLayout; 12 | 13 | /* 14 | *@创建者 L_jp 15 | *@创建时间 2019/7/21 13:36. 16 | *@描述 17 | * 18 | *@更新者 $Author$ 19 | *@更新时间 $Date$ 20 | *@更新描述 21 | */ 22 | public class ItemAdapter2 extends BaseQuickAdapter { 23 | 24 | public ItemAdapter2(List mShowItems) { 25 | super(R.layout.layout_item, mShowItems); 26 | } 27 | 28 | @Override 29 | protected void convert(BaseViewHolder helper, String item) { 30 | SwipeMenuLayout mSwipe = helper.getView(R.id.swipe_menu_layout); 31 | int position = helper.getLayoutPosition(); 32 | mSwipe.setEnableLeftMenu(position % 4 == 0); 33 | String text = item + ",菜单在" + (position % 4 == 0 ? "左; " : "右; "); 34 | if (position % 3 == 0) { 35 | mSwipe.setOpenChoke(false); 36 | text += "我是无阻塞的; "; 37 | } else { 38 | mSwipe.setOpenChoke(true); 39 | text += "我是有阻塞的; "; 40 | } 41 | if (position % 5 == 0) { 42 | mSwipe.setClickMenuAndClose(true); 43 | text += "点击我可以展开菜单"; 44 | } else { 45 | mSwipe.setClickMenuAndClose(false); 46 | } 47 | ((TextView) helper.getView(R.id.tv_content)).setText(text); 48 | helper.addOnClickListener(R.id.tv_menu1).addOnClickListener(R.id.tv_menu2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/ljp/swipemenulayout/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ljp.swipemenulayout; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.Log; 8 | import android.view.View; 9 | 10 | import com.chad.library.adapter.base.BaseQuickAdapter; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | 17 | private int index = 0; 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_main); 23 | 24 | 25 | RecyclerView recyclerView = findViewById(R.id.recyclerView); 26 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 27 | 28 | List mShowItems = new ArrayList<>(); 29 | for (int i = 0; i < 50; i++) { 30 | mShowItems.add("item = " + i); 31 | } 32 | 33 | // recyclerView.setAdapter(new ItemAdapter(this)); 34 | 35 | ItemAdapter2 itemAdapter2 = new ItemAdapter2(mShowItems); 36 | recyclerView.setAdapter(itemAdapter2); 37 | 38 | itemAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { 39 | @Override 40 | public void onItemClick(BaseQuickAdapter adapter, View view, int position) { 41 | Log.d("======", "点击了item : " + (++index)); 42 | } 43 | }); 44 | itemAdapter2.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { 45 | @Override 46 | public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { 47 | if (view.getId() == R.id.tv_menu1) { 48 | Log.d("======", "点击菜单1 " + (++index)); 49 | } else { 50 | Log.d("======", "点击菜单2 " + (++index)); 51 | } 52 | } 53 | }); 54 | itemAdapter2.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() { 55 | @Override 56 | public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) { 57 | Log.d("======", "长按了" + (++index)); 58 | return true; 59 | } 60 | }); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/ljp/swipemenulayout/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package com.ljp.swipemenulayout; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | public class ToastUtil { 7 | 8 | private static Toast sToast; 9 | 10 | public static void showToast(final Context context, final String msg) { 11 | 12 | if (sToast == null) { 13 | initToast(context, msg); 14 | } 15 | //判断当前代码是否是主线程 16 | sToast.setText(msg); 17 | sToast.show(); 18 | } 19 | 20 | private static void initToast(Context context, String msg) { 21 | sToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT); 22 | sToast.setText(msg); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 19 | 20 | 25 | 26 | 27 | 34 | 35 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_item2.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SwipeMenuLayout 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.0' 11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | maven { url "https://jitpack.io" } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /gif/gif1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/gif/gif1.gif -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljphawk/SwipeMenuLayout/f1edce5e26096041d19a64f278f653791d7b229a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':swipemenu' 2 | -------------------------------------------------------------------------------- /swipemenu/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | *.iml 3 | -------------------------------------------------------------------------------- /swipemenu/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven'//this 3 | group='com.github.liuhawk' 4 | 5 | android { 6 | compileSdkVersion 28 7 | 8 | 9 | 10 | defaultConfig { 11 | minSdkVersion 19 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | } 29 | 30 | // 指定编码 31 | tasks.withType(JavaCompile) { 32 | options.encoding = "UTF-8" 33 | } 34 | 35 | // 打包源码 36 | task sourcesJar(type: Jar) { 37 | from android.sourceSets.main.java.srcDirs 38 | classifier = 'sources' 39 | } 40 | 41 | task javadoc(type: Javadoc) { 42 | failOnError false 43 | source = android.sourceSets.main.java.sourceFiles 44 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 45 | classpath += configurations.compile 46 | } 47 | 48 | // 制作文档(Javadoc) 49 | task javadocJar(type: Jar, dependsOn: javadoc) { 50 | classifier = 'javadoc' 51 | from javadoc.destinationDir 52 | } 53 | 54 | artifacts { 55 | archives sourcesJar 56 | archives javadocJar 57 | } -------------------------------------------------------------------------------- /swipemenu/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /swipemenu/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /swipemenu/src/main/java/cn/ljp/swipemenu/SwipeMenuLayout.java: -------------------------------------------------------------------------------- 1 | package cn.ljp.swipemenu; 2 | 3 | 4 | import android.animation.Animator; 5 | import android.animation.AnimatorListenerAdapter; 6 | import android.animation.ValueAnimator; 7 | import android.content.Context; 8 | import android.content.res.TypedArray; 9 | import android.util.AttributeSet; 10 | import android.util.Log; 11 | import android.view.MotionEvent; 12 | import android.view.VelocityTracker; 13 | import android.view.View; 14 | import android.view.ViewConfiguration; 15 | import android.view.ViewGroup; 16 | import android.view.animation.AccelerateInterpolator; 17 | import android.view.animation.OvershootInterpolator; 18 | 19 | /* 20 | *@创建者 L_jp 21 | *@创建时间 2019/6/8 11:15. 22 | *@描述 23 | * 24 | * 使用方式: 25 | * 当前这个SwipeMenuLayout为parentView, 26 | * childView中第一个view为itemView,后面相继的view为menuView; 27 | * itemView的宽度不管是AT_MOST还是EXACTLY,都会指定为SwipeMenuLayout的宽度; 28 | * 如果SwipeMenuLayout的宽度为AT_MOST,会以它父view的宽度来测量 29 | * 30 | * 禁止侧滑功能 isEnableSwipe设置false 31 | * 默认左滑打开菜单,想要右滑打开菜单的话 isEnableLeftMenu设置true 32 | * 33 | *@更新者 $Author$ 34 | *@更新时间 $Date$ 35 | *@更新描述 36 | */ 37 | public class SwipeMenuLayout extends ViewGroup { 38 | private static final String TAG = "SwipeMenuLayout"; 39 | private final Context mContext; 40 | private int mScaledTouchSlop; 41 | private int mScaledMaximumFlingVelocity; 42 | //内容view 43 | private View mContentView; 44 | //菜单内容的宽度,也是最大的宽度距离 45 | private int mMenuWidth; 46 | private float mLastRawX = 0; 47 | private float mFirstRawX = 0; 48 | private static SwipeMenuLayout mCacheView; 49 | private int mPointerId; 50 | //滑动速度 51 | private VelocityTracker mVelocityTracker; 52 | //多点触摸判断的变量 53 | private boolean isFingerTouch = false; 54 | //展开 关闭的动画 55 | private ValueAnimator mExpandAnim, mCloseAnim; 56 | //动画时间 57 | private int animDuration = 300; 58 | //阻塞拦截的一个控制变量 59 | private boolean chokeIntercept = false; 60 | /** 61 | * 是否开启阻塞效果 默认开启 62 | * 举个例子 比如你把item1的侧滑菜单划出来了,你继续滑动item2的, 63 | * 这是默认是开启阻塞效果的,在你滑动item2的时候 会先关闭item1的菜单, 64 | * 需要再次滑动item2才可以(qq是这样子的) 65 | * 如果关闭这个效果,你在滑动item2的同时会同时关闭item1 66 | */ 67 | private boolean isOpenChoke = true; 68 | //是否启用侧滑 默认启用 默认左滑动 而且放置右侧 69 | private boolean isEnableSwipe = true; 70 | //是否启用右滑出现菜单 启用后是menu放置左侧 71 | private boolean isEnableLeftMenu = false; 72 | //是否开启点击菜单内容后自动关闭菜单 默认false 73 | private boolean isClickMenuAndClose = false; 74 | private SwipeMenuStateListener mSwipeMenuStateListener; 75 | 76 | public SwipeMenuLayout(Context context) { 77 | this(context, null); 78 | } 79 | 80 | public SwipeMenuLayout(Context context, AttributeSet attrs) { 81 | this(context, attrs, 0); 82 | } 83 | 84 | public SwipeMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) { 85 | super(context, attrs, defStyleAttr); 86 | this.mContext = context; 87 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SwipeMenuLayout, defStyleAttr, 0); 88 | isEnableSwipe = ta.getBoolean(R.styleable.SwipeMenuLayout_isEnableSwipe, true); 89 | isEnableLeftMenu = ta.getBoolean(R.styleable.SwipeMenuLayout_isEnableLeftMenu, false); 90 | isOpenChoke = ta.getBoolean(R.styleable.SwipeMenuLayout_isOpenChoke, true); 91 | isClickMenuAndClose = ta.getBoolean(R.styleable.SwipeMenuLayout_isClickMenuAndClose, false); 92 | ta.recycle(); 93 | 94 | init(); 95 | } 96 | 97 | private void init() { 98 | //获取滑动的最小值,大于这个值就认为他是滑动 默认是8 99 | mScaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop(); 100 | // 获得允许执行fling (抛)的最大速度值 (惯性速度) 101 | mScaledMaximumFlingVelocity = ViewConfiguration.get(mContext).getScaledMaximumFlingVelocity(); 102 | setClickable(true); 103 | 104 | } 105 | 106 | @Override 107 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 108 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 109 | //获取测量模式 110 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); 111 | //内容view的宽度 112 | int contentWidth = 0; 113 | int contentMaxHeight = 0; 114 | mMenuWidth = 0; 115 | int childCount = getChildCount(); 116 | for (int i = 0; i < childCount; i++) { 117 | View childAt = getChildAt(i); 118 | if (childAt.getVisibility() == View.GONE) { 119 | continue; 120 | } 121 | LayoutParams layoutParams = childAt.getLayoutParams(); 122 | if (i == 0) { 123 | //让itemView的宽度为parentView的宽度 124 | layoutParams.width = getMeasuredWidth(); 125 | mContentView = childAt; 126 | } 127 | //测量子view的宽高 128 | measureChild(childAt, widthMeasureSpec, heightMeasureSpec); 129 | //如果parentView测量模式不是精准的 130 | if (heightMode != MeasureSpec.EXACTLY) { 131 | contentMaxHeight = Math.max(contentMaxHeight, childAt.getMeasuredHeight()); 132 | } 133 | //child测量结束后才能获取宽高 134 | if (i == 0) { 135 | contentWidth = childAt.getMeasuredWidth(); 136 | } else { 137 | mMenuWidth += childAt.getMeasuredWidth(); 138 | } 139 | } 140 | //取最大值 重新测量 141 | int height = Math.max(getMeasuredHeight(), contentMaxHeight); 142 | setMeasuredDimension(contentWidth, height); 143 | } 144 | 145 | @Override 146 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 147 | int childCount = getChildCount(); 148 | int pLeft = getPaddingLeft(); 149 | int pTop = getPaddingTop(); 150 | int left = 0; 151 | int right = 0; 152 | 153 | for (int i = 0; i < childCount; i++) { 154 | View childAt = getChildAt(i); 155 | if (childAt.getVisibility() == View.GONE) { 156 | continue; 157 | } 158 | if (i == 0) { 159 | childAt.layout(pLeft, pTop, pLeft + childAt.getMeasuredWidth(), pTop + childAt.getMeasuredHeight()); 160 | left += pLeft + childAt.getMeasuredWidth(); 161 | } else { 162 | //放置左侧 163 | if (isEnableLeftMenu) { 164 | childAt.layout(right - childAt.getMeasuredWidth(), pTop, right, pTop + childAt.getMeasuredHeight()); 165 | right -= childAt.getMeasuredWidth(); 166 | } else { 167 | //放置右侧 168 | childAt.layout(left, pTop, left + childAt.getMeasuredWidth(), pTop + childAt.getMeasuredHeight()); 169 | left += childAt.getMeasuredWidth(); 170 | } 171 | } 172 | } 173 | } 174 | 175 | 176 | @Override 177 | public boolean dispatchTouchEvent(MotionEvent ev) { 178 | switch (ev.getAction()) { 179 | case MotionEvent.ACTION_DOWN: 180 | mFirstRawX = ev.getRawX(); 181 | getParent().requestDisallowInterceptTouchEvent(false); 182 | //关闭上一个打开的SwipeMenuLayout 183 | chokeIntercept = false; 184 | if (null != mCacheView) { 185 | if (mCacheView != this) { 186 | mCacheView.closeMenuAnim(); 187 | chokeIntercept = isOpenChoke; 188 | } 189 | //屏蔽父类的事件,只要有一个侧滑菜单处于打开状态, 就不给外层布局上下滑动了 190 | getParent().requestDisallowInterceptTouchEvent(true); 191 | } 192 | break; 193 | case MotionEvent.ACTION_UP: 194 | case MotionEvent.ACTION_CANCEL: 195 | //多指触摸状态改变 196 | isFingerTouch = false; 197 | //如果已经侧滑出菜单,菜单范围内的点击事件不拦截 198 | if (Math.abs(getScrollX()) == Math.abs(mMenuWidth)) { 199 | //菜单范围的判断 200 | if ((isEnableLeftMenu && ev.getX() < mMenuWidth) 201 | || (!isEnableLeftMenu && ev.getX() > getMeasuredWidth() - mMenuWidth)) { 202 | //点击菜单关闭侧滑 203 | if (isClickMenuAndClose) { 204 | closeMenuAnim(); 205 | } 206 | break; 207 | } 208 | //否则点击了item, 直接动画关闭 209 | closeMenuAnim(); 210 | return true; 211 | } 212 | break; 213 | } 214 | return super.dispatchTouchEvent(ev); 215 | } 216 | 217 | @Override 218 | public boolean onInterceptTouchEvent(MotionEvent ev) { 219 | if (!this.isEnableSwipe) { 220 | return super.onInterceptTouchEvent(ev); 221 | } 222 | switch (ev.getAction()) { 223 | case MotionEvent.ACTION_DOWN: 224 | //多跟手指的触摸处理 isFingerTouch为true的话 表示之前已经有一个down事件了, 225 | if (isFingerTouch) { 226 | return true; 227 | } else { 228 | isFingerTouch = true; 229 | } 230 | //第一个触点的id, 此时可能有多个触点,但至少一个,计算滑动速率用 231 | mPointerId = ev.getPointerId(0); 232 | mLastRawX = ev.getRawX(); 233 | break; 234 | case MotionEvent.ACTION_MOVE: 235 | //大于系统给出的这个数值,就认为是滑动了 事件进行拦截,在onTouch中进行逻辑操作 236 | if (Math.abs(ev.getRawX() - mFirstRawX) >= mScaledTouchSlop) { 237 | longClickable(false); 238 | return true; 239 | } 240 | break; 241 | } 242 | return super.onInterceptTouchEvent(ev); 243 | } 244 | 245 | @Override 246 | public boolean onTouchEvent(MotionEvent ev) { 247 | //如果关闭了侧滑 直接super 248 | if (!this.isEnableSwipe) { 249 | return super.onTouchEvent(ev); 250 | } 251 | acquireVelocityTracker(ev); 252 | switch (ev.getAction()) { 253 | case MotionEvent.ACTION_MOVE: 254 | //有阻塞 255 | if (chokeIntercept) { 256 | break; 257 | } 258 | //计算移动的距离 259 | float gap = mLastRawX - ev.getRawX(); 260 | //view滑动 261 | scrollBy((int) (gap), 0); 262 | if (Math.abs(gap) > mScaledTouchSlop || Math.abs(getScrollX()) > mScaledTouchSlop) { 263 | getParent().requestDisallowInterceptTouchEvent(true); 264 | } 265 | //超过范围的话--->归位 266 | //目前是右滑的话 (菜单在左边) 267 | if (isEnableLeftMenu) { 268 | if (getScrollX() < -mMenuWidth) { 269 | scrollTo(-mMenuWidth, 0); 270 | } else if (getScrollX() > 0) { 271 | scrollTo(0, 0); 272 | } 273 | } else { 274 | if (getScrollX() < 0) { 275 | scrollTo(0, 0); 276 | } else if (getScrollX() > mMenuWidth) { 277 | scrollTo(mMenuWidth, 0); 278 | } 279 | } 280 | //重新赋值 281 | mLastRawX = ev.getRawX(); 282 | break; 283 | case MotionEvent.ACTION_UP: 284 | case MotionEvent.ACTION_CANCEL: 285 | //unitis值为1000(毫秒)时间单位内运动了多少个像素 正负最多为mScaledMaximumFlingVelocity 286 | mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity); 287 | float velocityX = mVelocityTracker.getXVelocity(mPointerId); 288 | //释放VelocityTracker 289 | recycleVelocityTracker(); 290 | if (!chokeIntercept && Math.abs(ev.getRawX() - mFirstRawX) >= mScaledTouchSlop) { 291 | //获取x方向的运动速度 292 | Log.d(TAG, "onTouchEvent: " + velocityX); 293 | //滑动速度超过1000 认为是快速滑动了 294 | if (Math.abs(velocityX) > 1000) { 295 | if (velocityX < -1000) {//左滑了 296 | if (!isEnableLeftMenu) { 297 | //展开Menu 298 | expandMenuAnim(); 299 | } else { 300 | //关闭Menu 301 | closeMenuAnim(); 302 | } 303 | } else {//右滑了 304 | if (!isEnableLeftMenu) { 305 | //关闭Menu 306 | closeMenuAnim(); 307 | } else { 308 | //展开Menu 309 | expandMenuAnim(); 310 | } 311 | } 312 | } else { 313 | //超过菜单布局的40% 就展开 反之关闭 314 | if (Math.abs(getScrollX()) > mMenuWidth * 0.4) {//否则就判断滑动距离 315 | //展开Menu 316 | expandMenuAnim(); 317 | } else { 318 | //关闭Menu 319 | closeMenuAnim(); 320 | } 321 | } 322 | return true; 323 | } 324 | break; 325 | } 326 | return super.onTouchEvent(ev); 327 | } 328 | 329 | 330 | //向VelocityTracker添加MotionEvent 331 | private void acquireVelocityTracker(final MotionEvent event) { 332 | if (null == mVelocityTracker) { 333 | mVelocityTracker = VelocityTracker.obtain(); 334 | } 335 | mVelocityTracker.addMovement(event); 336 | } 337 | 338 | //释放VelocityTracker 339 | private void recycleVelocityTracker() { 340 | if (null != mVelocityTracker) { 341 | mVelocityTracker.clear(); 342 | mVelocityTracker.recycle(); 343 | mVelocityTracker = null; 344 | } 345 | } 346 | 347 | 348 | public void expandMenuAnim() { 349 | longClickable(false); 350 | //清除动画 351 | cleanAnim(); 352 | //展开就赋值 353 | mCacheView = SwipeMenuLayout.this; 354 | 355 | mExpandAnim = ValueAnimator.ofInt(getScrollX(), isEnableLeftMenu ? -mMenuWidth : mMenuWidth); 356 | mExpandAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 357 | @Override 358 | public void onAnimationUpdate(ValueAnimator animation) { 359 | scrollTo((Integer) animation.getAnimatedValue(), 0); 360 | } 361 | }); 362 | mExpandAnim.addListener(new AnimatorListenerAdapter() { 363 | @Override 364 | public void onAnimationEnd(Animator animation) { 365 | super.onAnimationEnd(animation); 366 | if (mSwipeMenuStateListener != null) { 367 | mSwipeMenuStateListener.menuIsOpen(true); 368 | } 369 | } 370 | }); 371 | mExpandAnim.setInterpolator(new OvershootInterpolator()); 372 | mExpandAnim.setDuration(animDuration).start(); 373 | } 374 | 375 | /** 376 | * 平滑关闭 377 | */ 378 | public void closeMenuAnim() { 379 | mCacheView = null; 380 | //清除动画 381 | cleanAnim(); 382 | mCloseAnim = ValueAnimator.ofInt(getScrollX(), 0); 383 | mCloseAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 384 | @Override 385 | public void onAnimationUpdate(ValueAnimator animation) { 386 | scrollTo((Integer) animation.getAnimatedValue(), 0); 387 | } 388 | }); 389 | mCloseAnim.addListener(new AnimatorListenerAdapter() { 390 | @Override 391 | public void onAnimationEnd(Animator animation) { 392 | super.onAnimationEnd(animation); 393 | longClickable(true); 394 | if (mSwipeMenuStateListener != null) { 395 | mSwipeMenuStateListener.menuIsOpen(false); 396 | } 397 | } 398 | }); 399 | mCloseAnim.setInterpolator(new AccelerateInterpolator()); 400 | mCloseAnim.setDuration(animDuration).start(); 401 | } 402 | 403 | //清除动画 防止上个动画没执行完 用户操作了另一个item 404 | private void cleanAnim() { 405 | if (mCloseAnim != null && mCloseAnim.isRunning()) { 406 | mCloseAnim.cancel(); 407 | } 408 | if (mExpandAnim != null && mExpandAnim.isRunning()) { 409 | mExpandAnim.cancel(); 410 | } 411 | } 412 | 413 | /* 414 | 每次ViewDetach的时候 415 | 1 mCacheView置为null 防止内存泄漏(mCacheView是一个静态变量) 416 | 2 侧滑删除后自己后,这个View被Recycler回收;复用 下一个进入屏幕的View的状态应该是普通状态,而不是展开状态。 417 | */ 418 | @Override 419 | protected void onDetachedFromWindow() { 420 | //避免多次调用 421 | if (getScrollX() != 0) { 422 | quickCloseMenu(); 423 | mCacheView = null; 424 | } 425 | super.onDetachedFromWindow(); 426 | } 427 | 428 | //快速关闭 没有动画时间 429 | public void quickCloseMenu() { 430 | if (getScrollX() != 0) { 431 | cleanAnim(); 432 | scrollTo(0, 0); 433 | mCacheView = null; 434 | } 435 | } 436 | 437 | //快速打开 没有动画时间 438 | public void quickExpandMenu() { 439 | if (getScrollX() == 0) { 440 | cleanAnim(); 441 | int x = isEnableLeftMenu ? -mMenuWidth : mMenuWidth; 442 | scrollTo(x, 0); 443 | mCacheView = null; 444 | } 445 | } 446 | 447 | //展开时,禁止自身的长按 448 | private void longClickable(boolean enable) { 449 | setLongClickable(enable); 450 | // if (null != mContentView) { 451 | // mContentView.setLongClickable(enable); 452 | // } 453 | } 454 | 455 | //展开时,禁止自身的长按 456 | @Override 457 | public boolean performLongClick() { 458 | if (getScrollX() != 0) { 459 | return true; 460 | } 461 | return super.performLongClick(); 462 | } 463 | 464 | //获取上一个打开的view,用来关闭 上一个打开的。暂时应该用不到 465 | public SwipeMenuLayout getCacheView() { 466 | return mCacheView; 467 | } 468 | 469 | //当前是否展开 470 | public boolean isExpandMenu() { 471 | return Math.abs(getScaleX()) >= mMenuWidth; 472 | } 473 | 474 | //获取是否打开阻塞 475 | public boolean isOpenChoke() { 476 | return isOpenChoke; 477 | } 478 | 479 | //设置是否打开阻塞 480 | public SwipeMenuLayout setOpenChoke(boolean openChoke) { 481 | isOpenChoke = openChoke; 482 | return this; 483 | } 484 | 485 | //获取是否打开了侧滑菜单功能 486 | public boolean isEnableSwipe() { 487 | return isEnableSwipe; 488 | } 489 | 490 | //设置是否开启侧滑菜单 491 | public SwipeMenuLayout setEnableSwipe(boolean enableSwipe) { 492 | isEnableSwipe = enableSwipe; 493 | return this; 494 | } 495 | 496 | //获取是否打开了 菜单在左侧功能 497 | public boolean isEnableLeftMenu() { 498 | return isEnableLeftMenu; 499 | } 500 | 501 | //设置菜单是否在左侧 502 | public SwipeMenuLayout setEnableLeftMenu(boolean enableLeftMenu) { 503 | isEnableLeftMenu = enableLeftMenu; 504 | return this; 505 | } 506 | 507 | //获取点击菜单后是否直接关闭菜单 508 | public boolean isClickMenuAndClose() { 509 | return isClickMenuAndClose; 510 | } 511 | 512 | //设置 点击菜单后是否直接关闭菜单 513 | public SwipeMenuLayout setClickMenuAndClose(boolean clickMenuAndClose) { 514 | isClickMenuAndClose = clickMenuAndClose; 515 | return this; 516 | } 517 | 518 | public SwipeMenuLayout setSwipeMenuStateListener(SwipeMenuStateListener listener) { 519 | this.mSwipeMenuStateListener = listener; 520 | return this; 521 | } 522 | } 523 | -------------------------------------------------------------------------------- /swipemenu/src/main/java/cn/ljp/swipemenu/SwipeMenuStateListener.java: -------------------------------------------------------------------------------- 1 | package cn.ljp.swipemenu; 2 | 3 | 4 | /* 5 | *@创建者 L_jp 6 | *@创建时间 2019/6/12 17:05. 7 | *@描述 8 | * 9 | *@更新者 $Author$ 10 | *@更新时间 $Date$ 11 | *@更新描述 12 | */ 13 | public interface SwipeMenuStateListener { 14 | void menuIsOpen(boolean isOpen); 15 | } 16 | -------------------------------------------------------------------------------- /swipemenu/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | --------------------------------------------------------------------------------