├── .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 | 12 | 13 | 14 | 26 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | 55 | 56 | 57 | 1.8 58 | 59 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 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 | 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 | 5 | 10 | 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 |