├── .gitignore ├── README.md ├── build.gradle ├── dragrecyclerview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── cyan │ │ └── dragrecyclerview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── cyan │ │ │ └── dragrecyclerview │ │ │ ├── DragRecyclerView.java │ │ │ ├── HoldTouchHelper.java │ │ │ └── OnItemChangeListener.java │ └── res │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ └── java │ └── cn │ └── cyan │ └── dragrecyclerview │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── cyan │ │ └── sample │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── cyan │ │ │ └── sample │ │ │ ├── base │ │ │ ├── BaseFragment.java │ │ │ ├── DividerGridItemDecoration.java │ │ │ ├── MainActivity.java │ │ │ ├── SampleAdapter.java │ │ │ ├── SampleApplication.java │ │ │ └── SampleEntity.java │ │ │ ├── grid │ │ │ ├── FindIconHelper.java │ │ │ ├── GridAdapter.java │ │ │ └── GridFragment.java │ │ │ └── list │ │ │ ├── ListAdapter.java │ │ │ └── ListFragment.java │ └── res │ │ ├── drawable │ │ └── selector_item.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── fragment_sample.xml │ │ ├── item_grid.xml │ │ └── item_list.xml │ │ ├── menu │ │ └── main_menu.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ ├── beauty.png │ │ ├── gossip.png │ │ ├── hot.png │ │ ├── ic_launcher.png │ │ ├── more.png │ │ ├── news.png │ │ ├── news_international.png │ │ ├── politics.png │ │ └── sports.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── array.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── cyan │ └── sample │ └── ExampleUnitTest.java └── 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 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-DragRecyclerView 2 | 基于RecyclerView实现的可拖动列表,支持Grid及List风格。A draggable list based on the RecyclerView implementation that supports the Grid and List styles. 3 | 4 | ### Demo 5 | 6 | ![demo](http://i.imgur.com/0l8Dnbx.gif) 7 | 8 | 9 | ### Usage 10 | 11 | ##### Setup 1 12 | compile 'cn.cyan.dragrecyclerview:dragrecyclerview:1.0.3' 13 | 14 | ##### Setup 2 15 | 16 | 20 | 21 | ##### Setup 3 22 | 23 | dragRecyclerView 24 | .dragEnable(true) 25 | .showDragAnimation(true) 26 | .setDragAdapter(adapter(data)) // The adapter must implement OnItemChangeListener 27 | .bindEvent(onItemTouchEvent); 28 | 29 | ### License 30 | 31 | MIT licensed 32 | 33 | -------------------------------------------------------------------------------- /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.2.3' 9 | // NOTE: Do not place your application dependencies here; they belong 10 | // in the individual module build.gradle files 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | jcenter() 17 | } 18 | } 19 | 20 | task clean(type: Delete) { 21 | delete rootProject.buildDir 22 | } 23 | -------------------------------------------------------------------------------- /dragrecyclerview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /dragrecyclerview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.3" 6 | 7 | defaultConfig { 8 | minSdkVersion 15 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(include: ['*.jar'], dir: 'libs') 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | compile 'com.android.support:appcompat-v7:24.2.1' 30 | testCompile 'junit:junit:4.12' 31 | compile 'com.android.support:recyclerview-v7:24.2.1' 32 | } 33 | -------------------------------------------------------------------------------- /dragrecyclerview/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 D:\android\sdk24/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 | -------------------------------------------------------------------------------- /dragrecyclerview/src/androidTest/java/cn/cyan/dragrecyclerview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.dragrecyclerview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("cn.cyan.dragrecyclerview.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/DragRecyclerView.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.dragrecyclerview; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.support.annotation.Nullable; 8 | import android.support.v7.widget.GridLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.support.v7.widget.helper.ItemTouchHelper; 11 | import android.util.AttributeSet; 12 | import android.view.View; 13 | import android.view.animation.Animation; 14 | import android.view.animation.ScaleAnimation; 15 | 16 | /** 17 | * User : Cyan(newbeeeeeeeee@gmail.com) 18 | * Date : 2016/9/28 19 | */ 20 | public class DragRecyclerView extends RecyclerView { 21 | 22 | private OnItemChangeListener adapter; 23 | private boolean dragEnable; 24 | private boolean showDragAnimation; 25 | 26 | 27 | public DragRecyclerView(Context context) { 28 | super(context); 29 | init(context, null); 30 | } 31 | 32 | public DragRecyclerView(Context context, @Nullable AttributeSet attrs) { 33 | super(context, attrs); 34 | init(context, attrs); 35 | } 36 | 37 | private void init(Context context, AttributeSet attrs) { 38 | if (attrs != null) { 39 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DragRecyclerView); 40 | dragEnable = ta.getBoolean(R.styleable.DragRecyclerView_drag_enable, true); 41 | showDragAnimation = ta.getBoolean(R.styleable.DragRecyclerView_show_drag_animation, true); 42 | ta.recycle(); 43 | } else { 44 | dragEnable = true; 45 | showDragAnimation = true; 46 | } 47 | } 48 | 49 | ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { 50 | @Override 51 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 52 | if (!dragEnable) { 53 | return ItemTouchHelper.ACTION_STATE_IDLE; 54 | } 55 | if (recyclerView.getLayoutManager() instanceof GridLayoutManager) { 56 | final int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 57 | return makeMovementFlags(dragFlag, ItemTouchHelper.ACTION_STATE_IDLE); 58 | } else { 59 | final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 60 | final int swipeFlags = ItemTouchHelper.ACTION_STATE_IDLE; 61 | return makeMovementFlags(dragFlags, swipeFlags); 62 | } 63 | } 64 | 65 | @Override 66 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 67 | int oldPosition = viewHolder.getAdapterPosition(); 68 | int targetPosition = target.getAdapterPosition(); 69 | adapter.onItemMoved(oldPosition, targetPosition); 70 | return false; 71 | } 72 | 73 | @Override 74 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 75 | 76 | } 77 | 78 | @Override 79 | public boolean isItemViewSwipeEnabled() { 80 | return true; 81 | } 82 | 83 | @Override 84 | public boolean isLongPressDragEnabled() { 85 | return false; 86 | } 87 | 88 | @Override 89 | public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { 90 | if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { 91 | final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); 92 | viewHolder.itemView.setAlpha(alpha); 93 | viewHolder.itemView.setTranslationX(dX); 94 | } else { 95 | super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); 96 | } 97 | } 98 | 99 | @Override 100 | public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { 101 | if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { 102 | viewHolder.itemView.setBackgroundColor(Color.LTGRAY); 103 | if (showDragAnimation) zoomView(viewHolder.itemView); 104 | } 105 | super.onSelectedChanged(viewHolder, actionState); 106 | } 107 | 108 | @Override 109 | public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) { 110 | super.clearView(recyclerView, viewHolder); 111 | viewHolder.itemView.setAlpha(1.0f); 112 | viewHolder.itemView.setBackgroundColor(Color.WHITE); 113 | if (showDragAnimation) revertView(viewHolder.itemView); 114 | } 115 | 116 | @Override 117 | public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, ViewHolder target) { 118 | return adapter.onItemDrop(target.getAdapterPosition()); 119 | } 120 | }); 121 | 122 | private ScaleAnimation zoomAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 123 | private ScaleAnimation revertAnimation = new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 124 | 125 | private void zoomView(final View v) { 126 | v.setAnimation(zoomAnimation); 127 | zoomAnimation.setFillAfter(true); 128 | zoomAnimation.setDuration(200); 129 | zoomAnimation.start(); 130 | } 131 | 132 | private void revertView(final View v) { 133 | v.setAnimation(revertAnimation); 134 | revertAnimation.setFillAfter(true); 135 | revertAnimation.setDuration(400); 136 | revertAnimation.setAnimationListener(new Animation.AnimationListener() { 137 | @Override 138 | public void onAnimationStart(Animation animation) { 139 | } 140 | 141 | @Override 142 | public void onAnimationEnd(Animation animation) { 143 | v.clearAnimation(); 144 | } 145 | 146 | @Override 147 | public void onAnimationRepeat(Animation animation) { 148 | } 149 | }); 150 | revertAnimation.start(); 151 | } 152 | 153 | public DragRecyclerView dragEnable(boolean dragEnable) { 154 | this.dragEnable = dragEnable; 155 | return this; 156 | } 157 | 158 | public DragRecyclerView showDragAnimation(boolean showDragAnimation) { 159 | this.showDragAnimation = showDragAnimation; 160 | return this; 161 | } 162 | 163 | 164 | public DragRecyclerView setDragAdapter(OnItemChangeListener dragBaseAdapter) { 165 | if (dragBaseAdapter instanceof Adapter) { 166 | this.adapter = dragBaseAdapter; 167 | touchHelper.attachToRecyclerView(this); 168 | super.setAdapter((Adapter) adapter); 169 | } else { 170 | throw new IllegalArgumentException(); 171 | } 172 | return this; 173 | } 174 | 175 | 176 | public DragRecyclerView bindEvent(HoldTouchHelper.OnItemTouchEvent onItemTouchEvent) { 177 | HoldTouchHelper.bind(this, onItemTouchEvent); 178 | return this; 179 | } 180 | 181 | 182 | public void startDrag(ViewHolder viewHolder) { 183 | touchHelper.startDrag(viewHolder); 184 | } 185 | 186 | public void startDrag(int position) { 187 | touchHelper.startDrag(getChildViewHolder(getChildAt(position))); 188 | } 189 | 190 | } 191 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/HoldTouchHelper.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.dragrecyclerview; 2 | 3 | import android.support.v4.view.GestureDetectorCompat; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.GestureDetector; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | 9 | /** 10 | * User : Cyan(newbeeeeeeeee@gmail.com) 11 | * Date : 2016/9/28 12 | */ 13 | public class HoldTouchHelper { 14 | 15 | private RecyclerView recyclerView; 16 | private OnItemTouchEvent onItemTouchEvent; 17 | private GestureDetectorCompat detector; 18 | 19 | 20 | private HoldTouchHelper(RecyclerView view, OnItemTouchEvent event) { 21 | this.recyclerView = view; 22 | this.onItemTouchEvent = event; 23 | 24 | GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() { 25 | @Override 26 | public boolean onSingleTapUp(MotionEvent e) { 27 | if (onItemTouchEvent != null) { 28 | View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 29 | if (child != null) { 30 | RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child); 31 | onItemTouchEvent.onItemClick(recyclerView, vh, vh.getAdapterPosition()); 32 | } 33 | } 34 | return true; 35 | } 36 | 37 | @Override 38 | public void onLongPress(MotionEvent e) { 39 | View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 40 | if (child != null) { 41 | RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child); 42 | onItemTouchEvent.onLongPress(recyclerView, vh, vh.getAdapterPosition()); 43 | } 44 | } 45 | }; 46 | 47 | detector = new GestureDetectorCompat(recyclerView.getContext(), simpleOnGestureListener); 48 | 49 | RecyclerView.OnItemTouchListener onItemTouchListener = new RecyclerView.OnItemTouchListener() { 50 | @Override 51 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 52 | detector.onTouchEvent(e); 53 | return false; 54 | } 55 | 56 | @Override 57 | public void onTouchEvent(RecyclerView rv, MotionEvent e) { 58 | detector.onTouchEvent(e); 59 | } 60 | 61 | @Override 62 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 63 | } 64 | }; 65 | recyclerView.addOnItemTouchListener(onItemTouchListener); 66 | } 67 | 68 | public interface OnItemTouchEvent { 69 | void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position); 70 | 71 | void onItemClick(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position); 72 | } 73 | 74 | public static void bind(RecyclerView view, OnItemTouchEvent event) { 75 | new HoldTouchHelper(view, event); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.dragrecyclerview; 2 | 3 | 4 | /** 5 | * User : Cyan(newbeeeeeeeee@gmail.com) 6 | * Date : 2016/9/28 7 | */ 8 | public interface OnItemChangeListener { 9 | 10 | /* item can be dragged */ 11 | boolean onItemDrag(int position); 12 | 13 | /* item moved */ 14 | void onItemMoved(int form, int target); 15 | 16 | /* item can be dropped */ 17 | boolean onItemDrop(int position); 18 | } 19 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /dragrecyclerview/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DragRecyclerView 3 | 4 | -------------------------------------------------------------------------------- /dragrecyclerview/src/test/java/cn/cyan/dragrecyclerview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.dragrecyclerview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/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.14.1-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 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.3" 6 | defaultConfig { 7 | applicationId "cn.cyan.dragrecyclerview" 8 | minSdkVersion 15 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 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 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.2.1' 28 | testCompile 'junit:junit:4.12' 29 | compile project(':dragrecyclerview') 30 | } 31 | -------------------------------------------------------------------------------- /sample/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 D:\android\sdk24/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 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/cn/cyan/sample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("cn.cyan.dragtab", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.Toast; 11 | 12 | import java.util.List; 13 | 14 | import cn.cyan.dragrecyclerview.DragRecyclerView; 15 | import cn.cyan.dragrecyclerview.HoldTouchHelper; 16 | import cn.cyan.sample.R; 17 | 18 | /** 19 | * User : Cyan(newbeeeeeeeee@gmail.com) 20 | * Date : 2017/1/4 21 | */ 22 | 23 | public abstract class BaseFragment extends Fragment { 24 | 25 | private List data; 26 | 27 | @Nullable 28 | @Override 29 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 30 | 31 | View view = inflater.inflate(R.layout.fragment_sample, container, false); 32 | initView(view); 33 | return view; 34 | } 35 | 36 | private void initView(View view) { 37 | 38 | DragRecyclerView dragRecyclerView = (DragRecyclerView) view.findViewById(R.id.drv); 39 | dragRecyclerView.addItemDecoration(new DividerGridItemDecoration()); 40 | dragRecyclerView.setHasFixedSize(false); 41 | dragRecyclerView.setLayoutManager(layoutManager()); 42 | data = initData(); 43 | /** custom setting */ 44 | dragRecyclerView 45 | .dragEnable(true) 46 | .showDragAnimation(true) 47 | .setDragAdapter(adapter(data)) 48 | .bindEvent(onItemTouchEvent); 49 | } 50 | 51 | 52 | protected abstract SampleAdapter adapter(List data); 53 | 54 | protected abstract RecyclerView.LayoutManager layoutManager(); 55 | 56 | HoldTouchHelper.OnItemTouchEvent onItemTouchEvent = new HoldTouchHelper.OnItemTouchEvent() { 57 | @Override 58 | public void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { 59 | if (((SampleAdapter) recyclerView.getAdapter()).onItemDrag(position)) { 60 | ((DragRecyclerView) recyclerView).startDrag(position); 61 | } 62 | } 63 | 64 | @Override 65 | public void onItemClick(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { 66 | String text = data.get(position).getText(); 67 | Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); 68 | } 69 | }; 70 | 71 | 72 | protected abstract List initData(); 73 | 74 | } 75 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/DividerGridItemDecoration.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Rect; 8 | import android.graphics.drawable.ColorDrawable; 9 | import android.graphics.drawable.Drawable; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.view.View; 12 | 13 | /** 14 | * User : Cyan(newbeeeeeeeee@gmail.com) 15 | * Date : 2017/1/4 16 | */ 17 | public class DividerGridItemDecoration extends RecyclerView.ItemDecoration { 18 | 19 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 20 | private Drawable mDivider; 21 | private int lineWidth = 1; 22 | 23 | public DividerGridItemDecoration(Context context) { 24 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 25 | mDivider = a.getDrawable(0); 26 | a.recycle(); 27 | } 28 | 29 | private DividerGridItemDecoration(int color) { 30 | mDivider = new ColorDrawable(color); 31 | } 32 | 33 | public DividerGridItemDecoration() { 34 | this(Color.parseColor("#cccccc")); 35 | } 36 | 37 | @Override 38 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 39 | drawHorizontal(c, parent); 40 | drawVertical(c, parent); 41 | } 42 | 43 | private void drawHorizontal(Canvas c, RecyclerView parent) { 44 | int childCount = parent.getChildCount(); 45 | for (int i = 0; i < childCount; i++) { 46 | final View child = parent.getChildAt(i); 47 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 48 | .getLayoutParams(); 49 | final int left = child.getLeft() - params.leftMargin; 50 | final int right = child.getRight() + params.rightMargin 51 | + lineWidth; 52 | final int top = child.getBottom() + params.bottomMargin; 53 | final int bottom = top + lineWidth; 54 | mDivider.setBounds(left, top, right, bottom); 55 | mDivider.draw(c); 56 | } 57 | } 58 | 59 | private void drawVertical(Canvas c, RecyclerView parent) { 60 | final int childCount = parent.getChildCount(); 61 | for (int i = 0; i < childCount; i++) { 62 | final View child = parent.getChildAt(i); 63 | 64 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 65 | final int top = child.getTop() - params.topMargin; 66 | final int bottom = child.getBottom() + params.bottomMargin; 67 | final int left = child.getRight() + params.rightMargin; 68 | final int right = left + lineWidth; 69 | 70 | mDivider.setBounds(left, top, right, bottom); 71 | mDivider.draw(c); 72 | } 73 | } 74 | 75 | @Override 76 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 77 | outRect.set(0, 0, lineWidth, lineWidth); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | 9 | import cn.cyan.sample.R; 10 | import cn.cyan.sample.grid.GridFragment; 11 | import cn.cyan.sample.list.ListFragment; 12 | 13 | /** 14 | * User : Cyan(newbeeeeeeeee@gmail.com) 15 | * Date : 2017/1/4 16 | */ 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_main); 23 | setFragment(new GridFragment()); 24 | } 25 | 26 | @Override 27 | public boolean onCreateOptionsMenu(Menu menu) { 28 | getMenuInflater().inflate(R.menu.main_menu, menu); 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean onOptionsItemSelected(MenuItem item) { 34 | switch (item.getItemId()) { 35 | case R.id.grid: 36 | setFragment(new GridFragment()); 37 | break; 38 | case R.id.list: 39 | setFragment(new ListFragment()); 40 | break; 41 | } 42 | return super.onOptionsItemSelected(item); 43 | } 44 | 45 | private void setFragment(Fragment fragment) { 46 | getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/SampleAdapter.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | 6 | import java.util.Collections; 7 | import java.util.List; 8 | 9 | import cn.cyan.dragrecyclerview.OnItemChangeListener; 10 | 11 | /** 12 | * User : Cyan(newbeeeeeeeee@gmail.com) 13 | * Date : 2017/1/4 14 | */ 15 | public abstract class SampleAdapter extends RecyclerView.Adapter implements OnItemChangeListener { 16 | protected Context context; 17 | protected volatile List data; 18 | 19 | public SampleAdapter(Context context, List list) { 20 | this.context = context; 21 | this.data = list; 22 | } 23 | 24 | @Override 25 | public void onItemMoved(int form, int target) { 26 | if (form < target) { 27 | // after 28 | for (int i = form; i < target; i++) { 29 | Collections.swap(data, i, i + 1); 30 | } 31 | } else { 32 | // before 33 | for (int i = form; i > target; i--) { 34 | Collections.swap(data, i, i - 1); 35 | } 36 | } 37 | notifyItemMoved(form, target); 38 | } 39 | 40 | @Override 41 | public int getItemCount() { 42 | return data.size(); 43 | } 44 | 45 | @Override 46 | public boolean onItemDrag(int position) { 47 | return data.get(position).isDragEnable(); 48 | } 49 | 50 | @Override 51 | public boolean onItemDrop(int position) { 52 | return data.get(position).isDropEnable(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | /** 7 | * User : Cyan(baocq@maritech.cn) 8 | * Date : 2017/1/4 9 | */ 10 | public class SampleApplication extends Application { 11 | 12 | private static Context context; 13 | 14 | @Override 15 | public void onCreate() { 16 | context=getApplicationContext(); 17 | } 18 | 19 | public static Context getContext() { 20 | return context; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/base/SampleEntity.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.base; 2 | 3 | /** 4 | * User : Cyan(newbeeeeeeeee@gmail.com) 5 | * Date : 2017/1/4 6 | */ 7 | public class SampleEntity { 8 | 9 | private String text; 10 | private boolean dragEnable; 11 | private boolean dropEnable; 12 | 13 | 14 | public String getText() { 15 | return text; 16 | } 17 | 18 | public void setText(String text) { 19 | this.text = text; 20 | } 21 | 22 | public boolean isDragEnable() { 23 | return dragEnable; 24 | } 25 | 26 | public void setDragEnable(boolean dragEnable) { 27 | this.dragEnable = dragEnable; 28 | } 29 | 30 | public boolean isDropEnable() { 31 | return dropEnable; 32 | } 33 | 34 | public void setDropEnable(boolean dropEnable) { 35 | this.dropEnable = dropEnable; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/grid/FindIconHelper.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.grid; 2 | 3 | import android.support.annotation.StringRes; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import cn.cyan.sample.R; 9 | import cn.cyan.sample.base.SampleApplication; 10 | 11 | /** 12 | * User : Cyan(newbeeeeeeeee@gmail.com) 13 | * Date : 2017/1/4 14 | */ 15 | 16 | public class FindIconHelper { 17 | 18 | private static FindIconHelper instance; 19 | 20 | private Map icons; 21 | 22 | public static FindIconHelper getInstance() { 23 | if (instance == null) { 24 | instance = new FindIconHelper(); 25 | } 26 | return instance; 27 | } 28 | 29 | private FindIconHelper() { 30 | icons = new HashMap<>(); 31 | icons.put(getStr(R.string.hot),R.mipmap.hot); 32 | icons.put(getStr(R.string.news_international),R.mipmap.news_international); 33 | icons.put(getStr(R.string.news),R.mipmap.news); 34 | icons.put(getStr(R.string.beauty),R.mipmap.beauty); 35 | icons.put(getStr(R.string.politics),R.mipmap.politics); 36 | icons.put(getStr(R.string.sports),R.mipmap.sports); 37 | icons.put(getStr(R.string.gossip),R.mipmap.gossip); 38 | icons.put(getStr(R.string.more),R.mipmap.more); 39 | } 40 | 41 | private String getStr(@StringRes int resId){ 42 | return SampleApplication.getContext().getString(resId); 43 | } 44 | 45 | 46 | public int find(String name) { 47 | if (icons.keySet().contains(name)) { 48 | return icons.get(name); 49 | } 50 | return R.mipmap.ic_launcher; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/grid/GridAdapter.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.grid; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | 11 | import java.util.List; 12 | 13 | import cn.cyan.sample.R; 14 | import cn.cyan.sample.base.SampleAdapter; 15 | import cn.cyan.sample.base.SampleEntity; 16 | 17 | /** 18 | * User : Cyan(newbeeeeeeeee@gmail.com) 19 | * Date : 2017/1/4 20 | */ 21 | public class GridAdapter extends SampleAdapter { 22 | 23 | public GridAdapter(Context context, List list) { 24 | super(context, list); 25 | } 26 | 27 | @Override 28 | public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 29 | View itemView = LayoutInflater.from(context).inflate(R.layout.item_grid, parent, false); 30 | return new GridViewHolder(itemView); 31 | } 32 | 33 | @Override 34 | public void onBindViewHolder(GridViewHolder holder, int position) { 35 | holder.setText(data.get(position).getText()); 36 | } 37 | 38 | class GridViewHolder extends RecyclerView.ViewHolder { 39 | ImageView imageView; 40 | TextView textView; 41 | 42 | GridViewHolder(View itemView) { 43 | super(itemView); 44 | imageView = (ImageView) itemView.findViewById(R.id.imageView); 45 | textView = (TextView) itemView.findViewById(R.id.textView); 46 | } 47 | 48 | void setText(String name) { 49 | imageView.setImageResource(FindIconHelper.getInstance().find(name)); 50 | textView.setText(name); 51 | } 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/grid/GridFragment.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.grid; 2 | 3 | import android.support.v7.widget.GridLayoutManager; 4 | import android.support.v7.widget.RecyclerView; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import cn.cyan.sample.R; 10 | import cn.cyan.sample.base.BaseFragment; 11 | import cn.cyan.sample.base.SampleAdapter; 12 | import cn.cyan.sample.base.SampleEntity; 13 | 14 | /** 15 | * User : Cyan(newbeeeeeeeee@gmail.com) 16 | * Date : 2017/1/4 17 | */ 18 | public class GridFragment extends BaseFragment { 19 | @Override 20 | protected SampleAdapter adapter(List data) { 21 | return new GridAdapter(getActivity(),data); 22 | } 23 | 24 | @Override 25 | protected RecyclerView.LayoutManager layoutManager() { 26 | return new GridLayoutManager(getActivity(),3); 27 | } 28 | 29 | @Override 30 | protected List initData() { 31 | List result = new ArrayList<>(); 32 | String[] strings = getResources().getStringArray(R.array.grid_array); 33 | for (String s : strings) { 34 | SampleEntity item = new SampleEntity(); 35 | item.setText(s); 36 | item.setDragEnable(true); 37 | item.setDropEnable(true); 38 | result.add(item); 39 | } 40 | // add more 41 | SampleEntity addMore = new SampleEntity(); 42 | addMore.setText(getString(R.string.more)); 43 | addMore.setDragEnable(false); 44 | addMore.setDragEnable(false); 45 | result.add(addMore); 46 | return result; 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/list/ListAdapter.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.list; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import java.util.List; 11 | 12 | import cn.cyan.dragrecyclerview.OnItemChangeListener; 13 | import cn.cyan.sample.R; 14 | import cn.cyan.sample.base.SampleAdapter; 15 | import cn.cyan.sample.base.SampleEntity; 16 | 17 | /** 18 | * User : Cyan(newbeeeeeeeee@gmail.com) 19 | * Date : 2017/1/4 20 | */ 21 | public class ListAdapter extends SampleAdapter implements OnItemChangeListener { 22 | public ListAdapter(Context context, List list) { 23 | super(context, list); 24 | } 25 | 26 | @Override 27 | public ListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 28 | View itemView = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false); 29 | return new ListAdapter.ListViewHolder(itemView); 30 | } 31 | 32 | @Override 33 | public void onBindViewHolder(ListAdapter.ListViewHolder holder, int position) { 34 | holder.setText(data.get(position).getText()); 35 | } 36 | 37 | class ListViewHolder extends RecyclerView.ViewHolder { 38 | TextView textView; 39 | 40 | ListViewHolder(View itemView) { 41 | super(itemView); 42 | textView = (TextView) itemView.findViewById(R.id.textView); 43 | } 44 | 45 | void setText(String name) { 46 | textView.setText(name); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/cyan/sample/list/ListFragment.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample.list; 2 | 3 | import android.support.v7.widget.LinearLayoutManager; 4 | import android.support.v7.widget.RecyclerView; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import cn.cyan.sample.R; 10 | import cn.cyan.sample.base.BaseFragment; 11 | import cn.cyan.sample.base.SampleAdapter; 12 | import cn.cyan.sample.base.SampleEntity; 13 | 14 | /** 15 | * User : Cyan(newbeeeeeeeee@gmail.com) 16 | * Date : 2017/1/4 17 | */ 18 | public class ListFragment extends BaseFragment { 19 | @Override 20 | protected SampleAdapter adapter(List data) { 21 | return new ListAdapter(getActivity(),data); 22 | } 23 | 24 | @Override 25 | protected RecyclerView.LayoutManager layoutManager() { 26 | return new LinearLayoutManager(getActivity()); 27 | } 28 | 29 | @Override 30 | protected List initData() { 31 | List result = new ArrayList<>(); 32 | String[] strings = getResources().getStringArray(R.array.list_array); 33 | for (String s : strings) { 34 | SampleEntity item = new SampleEntity(); 35 | item.setText(s); 36 | item.setDragEnable(true); 37 | item.setDropEnable(true); 38 | result.add(item); 39 | } 40 | return result; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/selector_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/item_grid.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 22 | 23 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/item_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample/src/main/res/menu/main_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/beauty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/beauty.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/gossip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/gossip.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/hot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/hot.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/more.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/more.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/news.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/news.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/news_international.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/news_international.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/politics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/politics.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/sports.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xhdpi/sports.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bocq/Android-DragRecyclerView/3d99c34250c64cc43a9cfae488f090bf36b8d53c/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/array.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @string/hot 5 | @string/news_international 6 | @string/news 7 | @string/beauty 8 | @string/politics 9 | @string/sports 10 | @string/gossip 11 | 12 | 13 | 14 | 15 | A day is a miniature of eternity. 16 | Miracles sometimes occur, but one has to work terribly for them. 17 | It takes two to make a quarrel. 18 | Beware beginning. 19 | Miss the songs have a one and alone with in. 20 | all or nothing, now or never. 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #60696969 8 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DragRecyclerView 3 | Grid 4 | List 5 | 今日热点 6 | 国际新闻 7 | 及时快讯 8 | 美女图片 9 | 政经要闻 10 | 体育赛事 11 | 茶余饭后 12 | 添加更多 13 | 14 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/src/test/java/cn/cyan/sample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.cyan.sample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':dragrecyclerview' 2 | --------------------------------------------------------------------------------