├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── dictionaries
│ └── guxiuzhong.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── gxz
│ │ └── swapmenurecyclerview
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── gxz
│ │ │ └── swapmenurecyclerview
│ │ │ ├── DividerItemDecoration.java
│ │ │ ├── MainActivity.java
│ │ │ ├── MyViewHolder.java
│ │ │ └── RecyclerAdapter.java
│ └── res
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── content_main.xml
│ │ └── item.xml
│ │ ├── menu
│ │ └── menu_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-v21
│ │ └── styles.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── gxz
│ └── swapmenurecyclerview
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── gxz
│ │ └── library
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── gxz
│ │ │ └── library
│ │ │ ├── SwapRecyclerView.java
│ │ │ ├── SwapWrapperUtils.java
│ │ │ ├── SwipeMenuBuilder.java
│ │ │ ├── bean
│ │ │ ├── SwipeMenu.java
│ │ │ └── SwipeMenuItem.java
│ │ │ └── view
│ │ │ ├── SwipeMenuLayout.java
│ │ │ └── SwipeMenuView.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── gxz
│ └── library
│ └── ExampleUnitTest.java
├── screenshots
├── layout.png
├── swap2.gif
└── swap3.gif
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | SwapMenuRecyclerView
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/dictionaries/guxiuzhong.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
24 |
25 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | 1.8
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 支持左滑显示菜单的RecyclerView
2 | 可以定制滑出来menu的样式
3 | #效果图
4 |
5 |
6 |
7 | #layout布局图
8 |
9 |
10 | #原理分析
11 |
12 |
13 | #Maven
14 |
15 | ```java
16 |
17 | com.gxz
18 | swapmenurecyclerview
19 | 1.0
20 | pom
21 |
22 | ```
23 | # Gradle
24 |
25 | ```java
26 |
27 | compile 'com.gxz:swapmenurecyclerview:1.0'
28 |
29 | ```
30 |
31 | #Ivy
32 | ```java
33 |
34 |
35 |
36 |
37 |
38 | ```
39 |
40 | #使用方法
41 | 1.在 RecyclerView.Adapter中
42 |
43 | ```java
44 | in activity
45 | public class MainActivity extends AppCompatActivity implements SwipeMenuBuilder
46 | ....
47 |
48 | //构造SwipeMenuView
49 | @Override
50 | public SwipeMenuView create() {
51 |
52 | SwipeMenu menu = new SwipeMenu(this);
53 |
54 | SwipeMenuItem item = new SwipeMenuItem(MainActivity.this);
55 | item.setTitle("分享")
56 | .setTitleColor(Color.WHITE)
57 | .setBackground(new ColorDrawable(Color.GRAY))
58 | .setWidth(dp2px(80))
59 | .setTitleSize(20)
60 | .setIcon(android.R.drawable.ic_menu_share);
61 |
62 | menu.addMenuItem(item);
63 |
64 | item = new SwipeMenuItem(MainActivity.this);
65 | item.setTitle("删除")
66 | .setTitleColor(Color.WHITE)
67 | .setBackground(new ColorDrawable(Color.RED));
68 | menu.addMenuItem(item);
69 |
70 | SwipeMenuView menuView = new SwipeMenuView(menu);
71 |
72 | menuView.setOnMenuItemClickListener(mOnSwipeItemClickListener);
73 |
74 | return menuView;
75 | }
76 |
77 | ```
78 |
79 | in adapter
80 |
81 | ```java
82 | @Override
83 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
84 | //根据数据创建右边的操作view
85 | SwipeMenuView menuView = swipeMenuBuilder.create();
86 | //包装用户的item布局
87 | SwipeMenuLayout swipeMenuLayout = SwapWrapperUtils.wrap(parent, R.layout.item, menuView, new BounceInterpolator(), new LinearInterpolator());
88 | MyViewHolder holder = new MyViewHolder(swipeMenuLayout);
89 | setListener(parent, holder, viewType);
90 | return holder;
91 | }
92 | ```
93 |
94 | #操作菜单的回调和状态的回调
95 | ```java
96 | private SwipeMenuView.OnMenuItemClickListener mOnSwipeItemClickListener = new SwipeMenuView.OnMenuItemClickListener() {
97 |
98 | @Override
99 | public void onMenuItemClick(int pos, SwipeMenu menu, int index) {
100 | Toast.makeText(MainActivity.this, menu.getMenuItem(index).getTitle(), Toast.LENGTH_LONG).show();
101 | if (index == 1) {
102 | recyclerView.smoothCloseMenu(pos);
103 | list.remove(pos);
104 | adapter.remove(pos);
105 | }
106 | }
107 | };
108 |
109 | recyclerView.setOnSwipeListener(new SwapRecyclerView.OnSwipeListener() {
110 | @Override
111 | public void onSwipeStart(int position) {
112 | Toast.makeText(MainActivity.this,"onSwipeStart-"+position,Toast.LENGTH_SHORT).show();
113 | }
114 |
115 | @Override
116 | public void onSwipeEnd(int position) {
117 | Toast.makeText(MainActivity.this, "onSwipeEnd-" + position, Toast.LENGTH_SHORT).show();
118 | }
119 | });
120 |
121 | ```
122 | ##其它设置
123 | 关闭某一条打开的menu
124 | 1.recyclerView.smoothCloseMenu(pos);
125 | 2.设置SwipeMenuLayout的差值器-动画效果
126 | ```java
127 | public void setCloseInterpolator(Interpolator interpolator) {
128 | mCloseInterpolator = interpolator;
129 | }
130 |
131 | public void setOpenInterpolator(Interpolator interpolator) {
132 | mOpenInterpolator = interpolator;
133 | }
134 |
135 | ```
136 |
137 |
138 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | applicationId "com.gxz.swapmenurecyclerview"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.4.0'
26 | compile 'com.android.support:design:23.4.0'
27 | compile project(':library')
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/guxiuzhong/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gxz/swapmenurecyclerview/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gxz/swapmenurecyclerview/DividerItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.Rect;
7 | import android.graphics.drawable.Drawable;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.view.View;
11 |
12 | /*
13 | * Copyright (C) 2014 The Android Open Source Project
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | */
27 | public class DividerItemDecoration extends RecyclerView.ItemDecoration {
28 |
29 | private static final int[] ATTRS = new int[]{
30 | android.R.attr.listDivider
31 | };
32 |
33 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
34 |
35 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
36 |
37 | private Drawable mDivider;
38 |
39 | private int mOrientation;
40 |
41 | public DividerItemDecoration(Context context, int orientation) {
42 | final TypedArray a = context.obtainStyledAttributes(ATTRS);
43 | mDivider = a.getDrawable(0);
44 | a.recycle();
45 | setOrientation(orientation);
46 | }
47 |
48 | public void setOrientation(int orientation) {
49 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
50 | throw new IllegalArgumentException("invalid orientation");
51 | }
52 | mOrientation = orientation;
53 | }
54 |
55 | @Override
56 | public void onDraw(Canvas c, RecyclerView parent) {
57 | if (mOrientation == VERTICAL_LIST) {
58 | drawVertical(c, parent);
59 | } else {
60 | drawHorizontal(c, parent);
61 | }
62 | }
63 |
64 | public void drawVertical(Canvas c, RecyclerView parent) {
65 | final int left = parent.getPaddingLeft();
66 | final int right = parent.getWidth() - parent.getPaddingRight();
67 |
68 | final int childCount = parent.getChildCount();
69 | for (int i = 0; i < childCount; i++) {
70 | final View child = parent.getChildAt(i);
71 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
72 | .getLayoutParams();
73 | final int top = child.getBottom() + params.bottomMargin;
74 | final int bottom = top + mDivider.getIntrinsicHeight();
75 | mDivider.setBounds(left, top, right, bottom);
76 | mDivider.draw(c);
77 | }
78 | }
79 |
80 | public void drawHorizontal(Canvas c, RecyclerView parent) {
81 | final int top = parent.getPaddingTop();
82 | final int bottom = parent.getHeight() - parent.getPaddingBottom();
83 |
84 | final int childCount = parent.getChildCount();
85 | for (int i = 0; i < childCount; i++) {
86 | final View child = parent.getChildAt(i);
87 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
88 | .getLayoutParams();
89 | final int left = child.getRight() + params.rightMargin;
90 | final int right = left + mDivider.getIntrinsicHeight();
91 | mDivider.setBounds(left, top, right, bottom);
92 | mDivider.draw(c);
93 | }
94 | }
95 |
96 | @Override
97 | public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
98 | if (mOrientation == VERTICAL_LIST) {
99 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
100 | } else {
101 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
102 | }
103 | }
104 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gxz/swapmenurecyclerview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import android.graphics.Color;
4 | import android.graphics.drawable.ColorDrawable;
5 | import android.os.Bundle;
6 | import android.support.design.widget.FloatingActionButton;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.support.v7.widget.DefaultItemAnimator;
9 | import android.support.v7.widget.LinearLayoutManager;
10 | import android.support.v7.widget.RecyclerView;
11 | import android.support.v7.widget.Toolbar;
12 | import android.util.TypedValue;
13 | import android.view.View;
14 | import android.widget.Toast;
15 |
16 | import com.gxz.library.SwapRecyclerView;
17 | import com.gxz.library.SwipeMenuBuilder;
18 | import com.gxz.library.bean.SwipeMenu;
19 | import com.gxz.library.bean.SwipeMenuItem;
20 | import com.gxz.library.view.SwipeMenuView;
21 |
22 | import java.util.ArrayList;
23 | import java.util.List;
24 |
25 | public class MainActivity extends AppCompatActivity implements SwipeMenuBuilder {
26 |
27 |
28 | private FloatingActionButton floatingActionButton;
29 | private RecyclerAdapter adapter;
30 | private List list = new ArrayList<>();
31 | private SwapRecyclerView recyclerView;
32 | private int pos;
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_main);
38 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
39 | setSupportActionBar(toolbar);
40 | floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
41 |
42 |
43 | recyclerView = (SwapRecyclerView) findViewById(R.id.id_rv);
44 | initData();
45 | adapter = new RecyclerAdapter(list, this);
46 | // recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
47 | recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
48 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
49 |
50 | recyclerView.setItemAnimator(new DefaultItemAnimator());
51 | recyclerView.setAdapter(adapter);
52 |
53 | recyclerView.setOnSwipeListener(new SwapRecyclerView.OnSwipeListener() {
54 | @Override
55 | public void onSwipeStart(int position) {
56 | // Toast.makeText(MainActivity.this,"onSwipeStart-"+position,Toast.LENGTH_SHORT).show();
57 | }
58 |
59 | @Override
60 | public void onSwipeEnd(int position) {
61 | // Toast.makeText(MainActivity.this, "onSwipeEnd-" + position, Toast.LENGTH_SHORT).show();
62 | pos = position;
63 | }
64 | });
65 |
66 | floatingActionButton.setOnClickListener(new View.OnClickListener() {
67 | @Override
68 | public void onClick(View v) {
69 | recyclerView.smoothCloseMenu(pos);
70 | }
71 | });
72 |
73 | adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
74 | @Override
75 | public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
76 | Toast.makeText(MainActivity.this, "onItemClick-->>>"+list.get(position), Toast.LENGTH_LONG).show();
77 | }
78 |
79 | @Override
80 | public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
81 | Toast.makeText(MainActivity.this, "onItemLongClick-->>>"+list.get(position), Toast.LENGTH_LONG).show();
82 | return true;//!!!!!!!!!!!!
83 | }
84 |
85 | });
86 |
87 | }
88 |
89 | private SwipeMenuView.OnMenuItemClickListener mOnSwipeItemClickListener = new SwipeMenuView.OnMenuItemClickListener() {
90 |
91 | @Override
92 | public void onMenuItemClick(int pos, SwipeMenu menu, int index) {
93 | Toast.makeText(MainActivity.this, menu.getMenuItem(index).getTitle(), Toast.LENGTH_LONG).show();
94 | if (index == 1) {
95 | recyclerView.smoothCloseMenu(pos);
96 | list.remove(pos);
97 | adapter.remove(pos);
98 | }
99 | }
100 | };
101 |
102 | private void initData() {
103 | for (int i = 1; i < 50; i++) {
104 | list.add("第 " + i + " 项数据");
105 | }
106 | }
107 |
108 |
109 | @Override
110 | public SwipeMenuView create() {
111 |
112 | SwipeMenu menu = new SwipeMenu(this);
113 |
114 | SwipeMenuItem item = new SwipeMenuItem(MainActivity.this);
115 | item.setTitle("分享")
116 | .setTitleColor(Color.WHITE)
117 | .setBackground(new ColorDrawable(Color.GRAY))
118 | .setWidth(dp2px(80))
119 | .setTitleSize(20)
120 | .setIcon(android.R.drawable.ic_menu_share);
121 |
122 | menu.addMenuItem(item);
123 |
124 | item = new SwipeMenuItem(MainActivity.this);
125 | item.setTitle("删除")
126 | .setTitleColor(Color.WHITE)
127 | .setBackground(new ColorDrawable(Color.RED));
128 | menu.addMenuItem(item);
129 |
130 | SwipeMenuView menuView = new SwipeMenuView(menu);
131 |
132 | menuView.setOnMenuItemClickListener(mOnSwipeItemClickListener);
133 |
134 | return menuView;
135 | }
136 |
137 | private int dp2px(int dp) {
138 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
139 | getResources().getDisplayMetrics());
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gxz/swapmenurecyclerview/MyViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 | import android.widget.TextView;
6 | import android.widget.Toast;
7 |
8 | /**
9 | * ================================================
10 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
11 | * 版 本:
12 | * 创建日期:16/7/8-下午6:10
13 | * 描 述:
14 | * 修订历史:
15 | * ================================================
16 | */
17 | public class MyViewHolder extends RecyclerView.ViewHolder {
18 |
19 | public TextView textView;
20 |
21 | public MyViewHolder(View itemView) {
22 | super(itemView);
23 | textView = (TextView) itemView.findViewById(R.id.id_tv);
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gxz/swapmenurecyclerview/RecyclerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.view.animation.BounceInterpolator;
8 | import android.view.animation.LinearInterpolator;
9 | import android.widget.Toast;
10 |
11 | import com.gxz.library.SwapWrapperUtils;
12 | import com.gxz.library.SwipeMenuBuilder;
13 | import com.gxz.library.view.SwipeMenuLayout;
14 | import com.gxz.library.view.SwipeMenuView;
15 |
16 | import java.util.List;
17 |
18 |
19 | /**
20 | * ================================================
21 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
22 | * 版 本:
23 | * 创建日期:16/7/8-下午6:11
24 | * 描 述:
25 | * 修订历史:
26 | * ================================================
27 | */
28 | public class RecyclerAdapter extends RecyclerView.Adapter {
29 | private List mData;
30 | private Context mContext;
31 | private SwipeMenuBuilder swipeMenuBuilder;
32 |
33 | public RecyclerAdapter(List mData, Context context) {
34 | this.mData = mData;
35 | this.mContext = context;
36 | swipeMenuBuilder = (SwipeMenuBuilder) this.mContext;
37 | }
38 |
39 | @Override
40 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
41 | //根据数据创建右边的操作view
42 | SwipeMenuView menuView = swipeMenuBuilder.create();
43 | //包装用户的item布局
44 | SwipeMenuLayout swipeMenuLayout = SwapWrapperUtils.wrap(parent, R.layout.item, menuView, new BounceInterpolator(), new LinearInterpolator());
45 | MyViewHolder holder = new MyViewHolder(swipeMenuLayout);
46 | setListener(parent, holder, viewType);
47 | return holder;
48 | }
49 |
50 | @Override
51 | public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
52 | MyViewHolder viewHolder = (MyViewHolder) holder;
53 | viewHolder.textView.setText(mData.get(position).toString());
54 | viewHolder.textView.setOnClickListener(new View.OnClickListener() {
55 | @Override
56 | public void onClick(View v) {
57 | Toast.makeText(mContext, "textView-onClick->>>" + mData.get(position).toString(), Toast.LENGTH_LONG).show();
58 |
59 | }
60 | });
61 | }
62 |
63 | @Override
64 | public int getItemCount() {
65 | return mData.size();
66 | }
67 |
68 | public void remove(int pos) {
69 | this.notifyItemRemoved(pos);
70 | }
71 |
72 | protected void setListener(final ViewGroup parent, final MyViewHolder viewHolder, int viewType) {
73 | viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
74 | @Override
75 | public void onClick(View v) {
76 | if (mOnItemClickListener != null) {
77 | int position = viewHolder.getAdapterPosition();
78 | mOnItemClickListener.onItemClick(v, viewHolder, mData.get(position), position);
79 | }
80 | }
81 | });
82 |
83 | viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
84 | @Override
85 | public boolean onLongClick(View v) {
86 | if (mOnItemClickListener != null) {
87 | int position = viewHolder.getAdapterPosition();
88 | return mOnItemClickListener.onItemLongClick(v, viewHolder, mData.get(position), position);
89 | }
90 | return false;
91 | }
92 | });
93 | }
94 |
95 |
96 | public OnItemClickListener mOnItemClickListener;
97 |
98 | public interface OnItemClickListener {
99 | void onItemClick(View view, RecyclerView.ViewHolder holder, T o, int position);
100 |
101 | boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, T o, int position);
102 | }
103 |
104 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
105 | this.mOnItemClickListener = onItemClickListener;
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SwapMenuRecyclerView
3 | Settings
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gxz/swapmenurecyclerview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.gxz.swapmenurecyclerview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.1.2'
9 | classpath 'com.novoda:bintray-release:0.3.4'
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
25 | allprojects {
26 | tasks.withType(Javadoc) {
27 | options{
28 | encoding "UTF-8"
29 | charSet 'UTF-8'
30 | links "http://docs.oracle.com/javase/7/docs/api"
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | testCompile 'junit:junit:4.12'
24 | compile 'com.android.support:appcompat-v7:23.4.0'
25 | compile 'com.android.support:design:23.4.0'
26 | }
27 |
28 | publish {
29 | userOrg = 'ta893115871'//bintray.com用户名
30 | groupId = 'com.gxz'//jcenter上的路径
31 | artifactId = 'swapmenurecyclerview'//项目名称
32 | publishVersion = '1.0'//版本号
33 | desc = 'swapmenurecyclerview'//描述
34 | website = 'https://github.com/ta893115871/SwapMenuRecyclerView'////网站,一般是github上的地址
35 | licences = ['Apache-2.0']
36 | }
37 |
38 | // ./gradlew clean build bintrayUpload
39 | // -PbintrayUser=ta893115871
40 | // -PbintrayKey=xxxxxxxxxxxxxxxxxxxxxx
41 | // -PdryRun=false
42 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/guxiuzhong/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/gxz/library/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/SwapRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library;
2 |
3 | import android.content.Context;
4 | import android.graphics.Rect;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.util.AttributeSet;
9 | import android.view.MotionEvent;
10 | import android.view.View;
11 | import android.view.ViewConfiguration;
12 |
13 | import com.gxz.library.view.SwipeMenuLayout;
14 |
15 | /**
16 | * ================================================
17 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
18 | * 版 本:
19 | * 创建日期:16/7/8-下午4:42
20 | * 描 述:自定义SwapRecyclerView实现左滑显示菜单Menu
21 | * 修订历史:
22 | * ================================================
23 | */
24 | public class SwapRecyclerView extends RecyclerView {
25 | private static final int TOUCH_STATE_NONE = 0; //没有滑动
26 | private static final int TOUCH_STATE_X = 1; //是否处于左右滑动
27 | private static final int TOUCH_STATE_Y = 2; //是否处于上下滑动
28 | private int mTouchState = TOUCH_STATE_NONE; //当前状态
29 |
30 | private float mDownX; //按下去的X坐标
31 | private float mDownY; //按下去的Y坐标
32 | private Rect mTouchFrame = new Rect(); //点击后坐标产生的矩形Rect
33 | private int mTouchPosition = -1; //当前按下去的位置
34 | private int oldPos = -1; //上一次按下去的位置
35 | private SwipeMenuLayout mTouchView; //当前按下去的位置的那个view
36 | private int mTouchSlop; //是否达到了滑动的距离
37 | private OnSwipeListener mOnSwipeListener;
38 |
39 | public SwapRecyclerView(Context context) {
40 | super(context);
41 | init();
42 | }
43 |
44 | public SwapRecyclerView(Context context, @Nullable AttributeSet attrs) {
45 | super(context, attrs);
46 | init();
47 | }
48 |
49 | public SwapRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
50 | super(context, attrs, defStyle);
51 | init();
52 | }
53 |
54 | private void init() {
55 | mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
56 | }
57 |
58 |
59 | @Override
60 | public boolean onInterceptTouchEvent(MotionEvent event) {
61 | int x = (int) event.getX();
62 | int y = (int) event.getY();
63 | switch (event.getAction()) {
64 | case MotionEvent.ACTION_DOWN:
65 | mDownX = event.getX();
66 | mDownY = event.getY();
67 | //是否拦截的返回值
68 | boolean handled = super.onInterceptTouchEvent(event);
69 | mTouchState = TOUCH_STATE_NONE;
70 | oldPos = mTouchPosition;
71 | //找到当前点击坐标下的所处于SwapRecyclerView的位置
72 | int mFirstPosition = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
73 | int count = getChildCount();
74 | for (int i = 0; i < count; i++) {
75 | final View child = getChildAt(i);
76 | if (child.getVisibility() == View.VISIBLE) {
77 | child.getHitRect(mTouchFrame);
78 | //判断是否点击到该控件上
79 | if (mTouchFrame.contains(x, y)) {
80 | mTouchPosition = mFirstPosition + i;
81 | break;
82 | }
83 | }
84 | }
85 | //找到了
86 | if (mTouchPosition != -1) {
87 | //通过position得到item的viewHolder,并判断合法性
88 | View view = getChildAt(mTouchPosition - mFirstPosition);
89 | RecyclerView.ViewHolder viewHolder = getChildViewHolder(view);
90 | if (viewHolder.itemView instanceof SwipeMenuLayout) {
91 | //menuView处于打开且点击的不在menu区域
92 | if (mTouchView != null && mTouchView.isOpen() && !inRangeOfView(mTouchView.getmMenuView(), event)) {
93 | //拦截事件,交给自己的onTouch方法处理.
94 | return true;
95 | }
96 | mTouchView = (SwipeMenuLayout) view;
97 | } else {
98 | throw new RuntimeException("viewHolder.itemView must be SwipeMenuLayout layout");
99 | }
100 | //将事件交给SwipeMenuLayout处理down事件
101 | mTouchView.onSwipe(event);
102 | }
103 | //down事件,如果没有打开menu,则不拦截,仍然交给系统
104 | return handled;
105 | case MotionEvent.ACTION_MOVE:
106 | float dy = Math.abs((event.getY() - mDownY));
107 | float dx = Math.abs((event.getX() - mDownX));
108 | //达到了滑动的临界值
109 | if (Math.abs(dy) > mTouchSlop || Math.abs(dx) > mTouchSlop) {
110 | if (mTouchState == TOUCH_STATE_NONE) {
111 | if (Math.abs(dy) > mTouchSlop) {//上下滑动的
112 | mTouchState = TOUCH_STATE_Y;
113 | } else if (dx > mTouchSlop) {//左右滑动的
114 | mTouchState = TOUCH_STATE_X;
115 | if (mOnSwipeListener != null) {
116 | mOnSwipeListener.onSwipeStart(mTouchPosition);
117 | }
118 | }
119 | }
120 | return true;//拦截事件,交给自己的onTouch方法处理.
121 | }
122 | }
123 | return super.onInterceptTouchEvent(event);
124 | }
125 |
126 | @Override
127 | public boolean onTouchEvent(MotionEvent event) {
128 | if (event.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null)
129 | return super.onTouchEvent(event);
130 | switch (event.getAction()) {
131 | case MotionEvent.ACTION_DOWN:
132 | //如果当前是处于打开的且用户按下去正好是打开menu的那行
133 | if (mTouchPosition == oldPos && mTouchView != null
134 | && mTouchView.isOpen()) {
135 | mTouchState = TOUCH_STATE_X;
136 | mTouchView.onSwipe(event);
137 | return true;
138 | } else {
139 | //如果不是直接关闭
140 | if (mTouchView != null && mTouchView.isOpen()) {
141 | mTouchView.smoothCloseMenu();
142 | mTouchView = null;
143 | return super.onTouchEvent(event);
144 | }
145 | }
146 | break;
147 | case MotionEvent.ACTION_MOVE:
148 | //左右滑动交给mTouchView处理,事件消费了
149 | if (mTouchState == TOUCH_STATE_X) {
150 | if (mTouchView != null) {
151 | mTouchView.onSwipe(event);
152 | }
153 | event.setAction(MotionEvent.ACTION_CANCEL);
154 | super.onTouchEvent(event);
155 | return true;
156 | }
157 | break;
158 | case MotionEvent.ACTION_UP:
159 | //处于左右滑动
160 | if (mTouchState == TOUCH_STATE_X) {
161 | if (mTouchView != null) {
162 | mTouchView.onSwipe(event);//打开活着关闭
163 | if (!mTouchView.isOpen()) {//关闭后复原变量
164 | mTouchPosition = -1;
165 | mTouchView = null;
166 | } else {
167 | mTouchView.getmMenuView().setPosition(mTouchPosition);
168 | }
169 | }
170 | if (mOnSwipeListener != null) {
171 | mOnSwipeListener.onSwipeEnd(mTouchPosition);
172 | }
173 | event.setAction(MotionEvent.ACTION_CANCEL);
174 | super.onTouchEvent(event);
175 | return true;
176 | }
177 | break;
178 | default:
179 | break;
180 | }
181 | return super.onTouchEvent(event);
182 | }
183 |
184 | /**
185 | * 判断点击事件是否在某个view内
186 | *
187 | * @param view view
188 | * @param ev ev
189 | * @return true 是 false 不是
190 | */
191 | public static boolean inRangeOfView(View view, MotionEvent ev) {
192 | int[] location = new int[2];
193 | view.getLocationOnScreen(location);
194 | int x = location[0];
195 | int y = location[1];
196 | if (ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())) {
197 | return false;
198 | }
199 | return true;
200 | }
201 |
202 | public void smoothCloseMenu(int position) {
203 | int mFirstPosition = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
204 | if (position >= 0) {
205 | View view = getChildAt(position - mFirstPosition);
206 | RecyclerView.ViewHolder viewHolder = getChildViewHolder(view);
207 | if (viewHolder.itemView instanceof SwipeMenuLayout) {
208 | SwipeMenuLayout swipeMenuLayout = (SwipeMenuLayout) viewHolder.itemView;
209 | if (swipeMenuLayout.isOpen()) {
210 | swipeMenuLayout.smoothCloseMenu();
211 | }
212 | }
213 | }
214 | }
215 |
216 | public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
217 | this.mOnSwipeListener = onSwipeListener;
218 | }
219 |
220 | public interface OnSwipeListener {
221 | void onSwipeStart(int position);
222 |
223 | void onSwipeEnd(int position);
224 | }
225 |
226 |
227 | }
228 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/SwapWrapperUtils.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.view.animation.Interpolator;
7 |
8 | import com.gxz.library.view.SwipeMenuLayout;
9 | import com.gxz.library.view.SwipeMenuView;
10 |
11 | /**
12 | * ================================================
13 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
14 | * 版 本:
15 | * 创建日期:16/7/10-上午12:17
16 | * 描 述:包装RecyclerView的item的辅助类
17 | * 修订历史:
18 | * ================================================
19 | */
20 | public class SwapWrapperUtils {
21 |
22 | /***
23 | * 包装RecyclerView的item
24 | *
25 | * @param parent parent
26 | * @param layoutId layoutId
27 | * @param menuView menuView 菜单的view
28 | * @param closeInterpolator closeInterpolator 关闭的差值器
29 | * @param openInterpolator openInterpolator 打开的差值器
30 | * @return SwipeMenuLayout 包装完后的SwipeMenuLayout
31 | */
32 | public static SwipeMenuLayout wrap(ViewGroup parent, int layoutId, SwipeMenuView menuView, Interpolator closeInterpolator, Interpolator openInterpolator) {
33 | View contentView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
34 | SwipeMenuLayout swipeMenuLayout = new SwipeMenuLayout(contentView, menuView, closeInterpolator, openInterpolator);
35 | return swipeMenuLayout;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/SwipeMenuBuilder.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library;
2 |
3 | import com.gxz.library.view.SwipeMenuView;
4 |
5 | /**
6 | * ================================================
7 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
8 | * 版 本:
9 | * 创建日期:16/7/9-下午10:57
10 | * 描 述:SwipeMenuView 接口生成类
11 | * 修订历史:
12 | * ================================================
13 | */
14 | public interface SwipeMenuBuilder {
15 |
16 | SwipeMenuView create();
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/bean/SwipeMenu.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library.bean;
2 |
3 | import android.content.Context;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * ================================================
10 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
11 | * 版 本:
12 | * 创建日期:16/7/8-下午5:10
13 | * 描 述:包含SwipeMenuItem对象的辅助类
14 | * 修订历史:
15 | * ================================================
16 | */
17 | public class SwipeMenu {
18 |
19 | private Context mContext;
20 | private List mItems;
21 |
22 | public SwipeMenu(Context context) {
23 | mContext = context;
24 | mItems = new ArrayList<>();
25 | }
26 |
27 | public Context getContext() {
28 | return mContext;
29 | }
30 |
31 | public void addMenuItem(SwipeMenuItem item) {
32 | mItems.add(item);
33 | }
34 |
35 | public void removeMenuItem(SwipeMenuItem item) {
36 | mItems.remove(item);
37 | }
38 |
39 | public List getMenuItems() {
40 | return mItems;
41 | }
42 |
43 | public SwipeMenuItem getMenuItem(int index) {
44 | return mItems.get(index);
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/bean/SwipeMenuItem.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library.bean;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.graphics.drawable.Drawable;
6 | import android.util.DisplayMetrics;
7 | import android.util.TypedValue;
8 |
9 | /**
10 | * ================================================
11 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
12 | * 版 本:
13 | * 创建日期:16/7/8-下午5:00
14 | * 描 述:menu的数据对象
15 | * 修订历史:
16 | * ================================================
17 | */
18 | public class SwipeMenuItem {
19 |
20 | private static final int TITLE_SIZE = 20;//sp
21 | private static final int WIDTH = 80;//dp
22 | private int id;
23 | private Context mContext;
24 | private String title;
25 | private Drawable icon;
26 | private Drawable background;
27 | private int titleColor;
28 | private int titleSize;
29 | private int width;
30 |
31 | public SwipeMenuItem(Context context) {
32 | mContext = context;
33 | //设置默认值
34 | DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
35 | titleColor = Color.WHITE;
36 | titleSize = TITLE_SIZE;
37 | width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, WIDTH, dm);
38 | }
39 |
40 | public int getId() {
41 | return id;
42 | }
43 |
44 | public SwipeMenuItem setId(int id) {
45 | this.id = id;
46 | return this;
47 | }
48 |
49 | public Context getmContext() {
50 | return mContext;
51 | }
52 |
53 | public SwipeMenuItem setmContext(Context mContext) {
54 | this.mContext = mContext;
55 | return this;
56 | }
57 |
58 | public String getTitle() {
59 | return title;
60 | }
61 |
62 | public SwipeMenuItem setTitle(String title) {
63 | this.title = title;
64 | return this;
65 | }
66 |
67 | public Drawable getIcon() {
68 | return icon;
69 | }
70 |
71 | public SwipeMenuItem setIcon(Drawable icon) {
72 | this.icon = icon;
73 | return this;
74 | }
75 |
76 | public SwipeMenuItem setIcon(int resId) {
77 | this.icon = mContext.getResources().getDrawable(resId);
78 | return this;
79 | }
80 |
81 | public Drawable getBackground() {
82 | return background;
83 | }
84 |
85 | public SwipeMenuItem setBackground(Drawable background) {
86 | this.background = background;
87 | return this;
88 | }
89 |
90 | public int getTitleColor() {
91 | return titleColor;
92 | }
93 |
94 | public SwipeMenuItem setTitleColor(int titleColor) {
95 | this.titleColor = titleColor;
96 | return this;
97 | }
98 |
99 | public int getTitleSize() {
100 | return titleSize;
101 | }
102 |
103 | public SwipeMenuItem setTitleSize(int titleSize) {
104 | this.titleSize = titleSize;
105 | return this;
106 | }
107 |
108 | public int getWidth() {
109 | return width;
110 | }
111 |
112 | public SwipeMenuItem setWidth(int width) {
113 | this.width = width;
114 | return this;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/view/SwipeMenuLayout.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library.view;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.support.v4.view.GestureDetectorCompat;
5 | import android.support.v4.widget.ScrollerCompat;
6 | import android.view.GestureDetector;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.view.ViewConfiguration;
10 | import android.view.ViewGroup;
11 | import android.view.animation.Interpolator;
12 | import android.widget.FrameLayout;
13 |
14 | /**
15 | * ================================================
16 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
17 | * 版 本:
18 | * 创建日期:16/7/8-下午5:19
19 | * 描 述:包装RecyclerView的item的布局,实现
20 | * 描 述:包装RecyclerView的item的布局,实现菜单操作的滑动打开与关闭
21 | * 修订历史:
22 | * ================================================
23 | */
24 | @SuppressLint("ViewConstructor")
25 | public class SwipeMenuLayout extends FrameLayout {
26 |
27 | private static final int DURATION = 350;
28 | private static final int STATE_CLOSE = 0;
29 | private static final int STATE_OPEN = 1;
30 | private int state = STATE_CLOSE;
31 | private boolean isFling;
32 |
33 | private View mContentView;
34 | private SwipeMenuView mMenuView;
35 | private GestureDetectorCompat mGestureDetector;
36 | private int mTouchSlop;
37 | private int mMinimumVelocity;
38 | private Interpolator mCloseInterpolator;
39 | private Interpolator mOpenInterpolator;
40 | private ScrollerCompat mOpenScroller;
41 | private ScrollerCompat mCloseScroller;
42 | private int mBaseX;
43 | private int mDownX;
44 |
45 |
46 | public SwipeMenuLayout(View contentView, SwipeMenuView menuView) {
47 | this(contentView, menuView, null, null);
48 | }
49 |
50 | public SwipeMenuLayout(View contentView, SwipeMenuView menuView, Interpolator closeInterpolator, Interpolator openInterpolator) {
51 | super(contentView.getContext());
52 | mContentView = contentView;
53 | mMenuView = menuView;
54 | mMenuView.setLayout(this);
55 | mCloseInterpolator = closeInterpolator;
56 | mOpenInterpolator = openInterpolator;
57 | init();
58 | }
59 |
60 | private void init() {
61 | mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
62 | mMinimumVelocity = (int) (ViewConfiguration.get(getContext())
63 | .getScaledMinimumFlingVelocity() * 2.2f);
64 | //屏幕的宽
65 | setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
66 | LayoutParams.WRAP_CONTENT));
67 | mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
68 | LayoutParams.WRAP_CONTENT));
69 | addView(mContentView);
70 | addView(mMenuView);
71 | GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
72 | @Override
73 | public boolean onDown(MotionEvent e) {
74 | isFling = false;
75 | return super.onDown(e);
76 | }
77 |
78 | @Override
79 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
80 | if ((e1.getX() - e2.getX()) > mTouchSlop
81 | && Math.abs(velocityX) > mMinimumVelocity) {
82 | isFling = true;
83 | }
84 | return super.onFling(e1, e2, velocityX, velocityY);
85 | }
86 | };
87 | mGestureDetector = new GestureDetectorCompat(getContext(),
88 | mGestureListener);
89 | if (mCloseInterpolator != null) {
90 | mCloseScroller = ScrollerCompat.create(getContext(),
91 | mCloseInterpolator);
92 | } else {
93 | mCloseScroller = ScrollerCompat.create(getContext());
94 | }
95 | if (mOpenInterpolator != null) {
96 | mOpenScroller = ScrollerCompat.create(getContext(),
97 | mOpenInterpolator);
98 | } else {
99 | mOpenScroller = ScrollerCompat.create(getContext());
100 | }
101 | }
102 |
103 | @Override
104 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
105 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
106 | //测量mMenuView的宽,高为mContentView的高
107 | mMenuView.measure(MeasureSpec.makeMeasureSpec(0,
108 | MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(
109 | getMeasuredHeight(), MeasureSpec.EXACTLY));
110 | }
111 |
112 |
113 | @Override
114 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
115 | mContentView.layout(0, 0, getMeasuredWidth(),
116 | mContentView.getMeasuredHeight());
117 | //在mContentView的右侧
118 | mMenuView.layout(getMeasuredWidth(), 0,
119 | getMeasuredWidth() + mMenuView.getMeasuredWidth(),
120 | mContentView.getMeasuredHeight());
121 | }
122 |
123 |
124 | public void onSwipe(MotionEvent event) {
125 | mGestureDetector.onTouchEvent(event);
126 | switch (event.getAction()) {
127 | case MotionEvent.ACTION_DOWN:
128 | mDownX = (int) event.getX();
129 | isFling = false;
130 | break;
131 | case MotionEvent.ACTION_MOVE:
132 | //按下去-当前的位置
133 | int dis = (int) (mDownX - event.getX());
134 | //menuView打开状态dis+mMenuView宽
135 | if (state == STATE_OPEN) {
136 | dis += mMenuView.getWidth();
137 | }
138 | swipe(dis);
139 | break;
140 | case MotionEvent.ACTION_UP:
141 | //快速滑动,或者超过了mMenuView宽的一半则打开,否则关闭
142 | if (isFling || (mDownX - event.getX()) > (mMenuView.getWidth() / 2)) {
143 | smoothOpenMenu();
144 | } else {
145 | smoothCloseMenu();
146 | }
147 | break;
148 | }
149 | }
150 |
151 | /**
152 | * 更改位置
153 | *
154 | * @param dis dis mContentView的left
155 | */
156 | private void swipe(int dis) {
157 | //mContentView的最大为mMenuView的宽
158 | if (dis > mMenuView.getWidth()) {
159 | dis = mMenuView.getWidth();
160 | }
161 | //mContentView-left的最小值为0即正常值
162 | if (dis < 0) {
163 | dis = 0;
164 | }
165 | //设置完mContentView的left就可以得出right以及mMenuView的left和right了
166 | //主要是left,right
167 | //left 最大值为-mMenuView.getWidth()
168 | mContentView.layout(-dis, mContentView.getTop(),
169 | mContentView.getWidth() - dis, getMeasuredHeight());
170 | mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),
171 | mContentView.getWidth() + mMenuView.getWidth() - dis,
172 | mMenuView.getBottom());
173 | }
174 |
175 | @Override
176 | public void computeScroll() {
177 | //让mMenuView打开
178 | if (state == STATE_OPEN) {
179 | //是否停止了滑动
180 | if (mOpenScroller.computeScrollOffset()) {
181 | swipe(mOpenScroller.getCurrX());
182 | //重绘UI
183 | postInvalidate();
184 | }
185 | } else {//让mMenuView关闭
186 | //mContentView的
187 | if (mCloseScroller.computeScrollOffset()) {
188 | //mBaseX为当前的mContentView的left,可以结合
189 | swipe(mBaseX - mCloseScroller.getCurrX());
190 | postInvalidate();
191 | }
192 | }
193 | }
194 |
195 | /**
196 | * 平滑的关闭mMenuView
197 | */
198 | public void smoothCloseMenu() {
199 | state = STATE_CLOSE;
200 | mBaseX = -mContentView.getLeft();
201 | //关闭是我们要让mContentView的慢慢的减小,
202 | //mCloseScroller.getCurrX()的范围是(0,mBaseX)
203 | mCloseScroller.startScroll(0, 0, mBaseX, 0, DURATION);
204 | postInvalidate();
205 | }
206 | /**
207 | * 平滑的打开mMenuView
208 | */
209 | public void smoothOpenMenu() {
210 | state = STATE_OPEN;
211 | //其实我们这里是用到了Scroller类产生的值(当然借助Interpolator来实现不同的值渐变,从而实现不同的效果)
212 | //打开的时候mContentView的left从当前的-mContentView.getLeft()到mMenuView.getWidth()
213 | //在computeScroll方法中 swipe(mOpenScroller.getCurrX());即可
214 | //mOpenScroller.getCurrX()的范围是(-mContentView.getLeft(),mMenuView.getWidth())
215 | //-mContentView.getLeft()为正值
216 | mOpenScroller.startScroll(-mContentView.getLeft(), 0,
217 | mMenuView.getWidth(), 0, DURATION);
218 | postInvalidate();
219 | }
220 | /**
221 | *无平滑的关闭mMenuView
222 | */
223 | public void closeMenu() {
224 | if (mCloseScroller.computeScrollOffset()) {
225 | mCloseScroller.abortAnimation();
226 | }
227 | if (state == STATE_OPEN) {
228 | state = STATE_CLOSE;
229 | swipe(0);
230 | }
231 | }
232 | /**
233 | * 无平滑的打开mMenuView
234 | */
235 | public void openMenu() {
236 | if (state == STATE_CLOSE) {
237 | state = STATE_OPEN;
238 | swipe(mMenuView.getWidth());
239 | }
240 | }
241 |
242 | public boolean isOpen() {
243 | return state == STATE_OPEN;
244 | }
245 |
246 | public SwipeMenuView getmMenuView() {
247 | return mMenuView;
248 | }
249 |
250 | public void setCloseInterpolator(Interpolator interpolator) {
251 | mCloseInterpolator = interpolator;
252 | }
253 |
254 | public void setOpenInterpolator(Interpolator interpolator) {
255 | mOpenInterpolator = interpolator;
256 | }
257 |
258 | public Interpolator getOpenInterpolator() {
259 | return mOpenInterpolator;
260 | }
261 |
262 | public Interpolator getCloseInterpolator() {
263 | return mCloseInterpolator;
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gxz/library/view/SwipeMenuView.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library.view;
2 |
3 | import android.text.TextUtils;
4 | import android.view.Gravity;
5 | import android.view.View;
6 | import android.widget.ImageView;
7 | import android.widget.LinearLayout;
8 | import android.widget.TextView;
9 |
10 | import com.gxz.library.bean.SwipeMenu;
11 | import com.gxz.library.bean.SwipeMenuItem;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * ================================================
17 | * 作 者:顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
18 | * 版 本:
19 | * 创建日期:16/7/8-下午5:14
20 | * 描 述:
21 | * 修订历史:
22 | * ================================================
23 | */
24 | public class SwipeMenuView extends LinearLayout implements View.OnClickListener {
25 |
26 | private SwipeMenuLayout mLayout;
27 | private SwipeMenu mMenu;
28 | private OnMenuItemClickListener mOnMenuItemClickListener;
29 | private int position;
30 |
31 | public int getPosition() {
32 | return position;
33 | }
34 |
35 | public void setPosition(int position) {
36 | this.position = position;
37 | }
38 |
39 | public SwipeMenuView(SwipeMenu menu) {
40 | super(menu.getContext());
41 | setOrientation(LinearLayout.HORIZONTAL);
42 | mMenu = menu;
43 | List items = mMenu.getMenuItems();
44 | int id = 0;
45 | for (SwipeMenuItem item : items) {
46 | addItem(item, id++);
47 | }
48 | }
49 |
50 | private void addItem(SwipeMenuItem item, int id) {
51 | LayoutParams params = new LayoutParams(item.getWidth(),
52 | LayoutParams.MATCH_PARENT);
53 | LinearLayout parent = new LinearLayout(getContext());
54 | parent.setId(id);
55 | parent.setGravity(Gravity.CENTER);
56 | parent.setOrientation(LinearLayout.VERTICAL);
57 | parent.setLayoutParams(params);
58 | parent.setBackgroundDrawable(item.getBackground());
59 | parent.setOnClickListener(this);
60 | addView(parent);
61 |
62 | if (item.getIcon() != null) {
63 | parent.addView(createIcon(item));
64 | }
65 | if (!TextUtils.isEmpty(item.getTitle())) {
66 | parent.addView(createTitle(item));
67 | }
68 |
69 | }
70 |
71 | private ImageView createIcon(SwipeMenuItem item) {
72 | ImageView iv = new ImageView(getContext());
73 | iv.setImageDrawable(item.getIcon());
74 | return iv;
75 | }
76 |
77 | private TextView createTitle(SwipeMenuItem item) {
78 | TextView tv = new TextView(getContext());
79 | tv.setText(item.getTitle());
80 | tv.setGravity(Gravity.CENTER);
81 | tv.setTextSize(item.getTitleSize());
82 | tv.setTextColor(item.getTitleColor());
83 | return tv;
84 | }
85 |
86 | @Override
87 | public void onClick(View v) {
88 | if (mOnMenuItemClickListener != null && mLayout.isOpen()) {
89 | mOnMenuItemClickListener.onMenuItemClick(position, mMenu, v.getId());
90 | }
91 | }
92 |
93 | public interface OnMenuItemClickListener {
94 | void onMenuItemClick(int position, SwipeMenu menu, int index);
95 | }
96 |
97 | public void setOnMenuItemClickListener(
98 | OnMenuItemClickListener mOnMenuItemClickListener) {
99 | this.mOnMenuItemClickListener = mOnMenuItemClickListener;
100 | }
101 |
102 | public void setLayout(SwipeMenuLayout mLayout) {
103 | this.mLayout = mLayout;
104 | }
105 |
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/com/gxz/library/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.gxz.library;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/screenshots/layout.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/screenshots/layout.png
--------------------------------------------------------------------------------
/screenshots/swap2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/screenshots/swap2.gif
--------------------------------------------------------------------------------
/screenshots/swap3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ta893115871/SwapMenuRecyclerView/7bd5864c19af4fca7278c7897e4ae9f6171b180d/screenshots/swap3.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------