├── .gitignore ├── README.md ├── build.gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── main ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ ├── android │ │ └── support │ │ │ └── v7 │ │ │ └── widget │ │ │ └── helper │ │ │ └── MosrItemTouchHelper.java │ └── com │ │ └── mosr │ │ └── recyclerview │ │ └── itemTouchUsing │ │ ├── ListViewDecoration.java │ │ ├── MainActivity.java │ │ ├── WrapContentLinearLayoutManager.java │ │ ├── adapter │ │ └── MainAdapter.java │ │ ├── app.java │ │ ├── entity │ │ └── InvitationInfo.java │ │ └── itemtouch │ │ ├── DefaultItemTouchHelpCallback.java │ │ └── DefaultItemTouchHelper.java │ └── res │ ├── drawable │ └── divider_recycler.xml │ ├── layout │ ├── activity_main.xml │ ├── item_img.xml │ └── item_text.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 │ └── img.jpg │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── 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/ 10 | /gradle/ 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 图文混排发帖(完美复现汽车之家论坛发帖) 2 | ![图文混排发帖](http://img.blog.csdn.net/20170719105921343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE1Njk2OTk5NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 3 | (效果图为最终项目中的) 4 | 5 | **本文源码已经托管在GitHub上,欢迎Fork多多star。**[地址](https://github.com/StrangerMosr/RecyclerViewItemTouchUsing/tree/master) 6 | 7 | **CSDN**[地址](http://blog.csdn.net/qq569699973/article/details/75390091) 8 | 9 | 最近重构一个项目,增加了一个新需求,要类似汽车之家的图文混排发帖,图片文字可自由移动位置(如效果图) 10 | 11 | 功能:图文混排,自由排列文字与图片的位置,图片之间自动加入输入框,两个输入框若相邻且有一个为空,则删除一个保留另外一个,若都有内容则不删除,删除文字时,若输入框内容为空,则删除整个输入框 12 | 13 | 实现:RecyclerView + ItemTouchHelper(为什么用RecyclerView,不用ListView,这个个人觉得RecyclerView的效果要比ListView好,汽车之家应该是ListView(猜测)) 14 | 15 | 代码: 16 | 首先要改造一下ItemTouchHelper这个类,这是Android提供的拖拽帮助类,在android.support.v7.widget.helper包下。 17 | 改造的原因: 18 | ![这里写图片描述](http://img.blog.csdn.net/20170719150501204?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE1Njk2OTk5NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 19 | 20 | 图中的Callback,在下面的处理过程中需要使用,但是他是protected的,所以我们要想要拿出来就要这样: 21 | ![这里写图片描述](http://img.blog.csdn.net/20170719150810549?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE1Njk2OTk5NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 22 | 23 | 新建一个package 包名如图要一样,然后新建一个class 集成ItemTouchHelper这个类。 24 | 25 | ``` 26 | public class MosrItemTouchHelper extends ItemTouchHelper { 27 | public MosrItemTouchHelper(Callback callback) { 28 | super(callback); 29 | } 30 | 31 | public Callback getCallback() { 32 | return mCallback; 33 | } 34 | } 35 | ``` 36 | 加上get方法就可以了。 37 | 38 | ``` 39 | public class DefaultItemTouchHelper extends MosrItemTouchHelper { 40 | 41 | private DefaultItemTouchHelpCallback itemTouchHelpCallback; 42 | 43 | public DefaultItemTouchHelper(DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener) { 44 | super(new DefaultItemTouchHelpCallback(onItemTouchCallbackListener)); 45 | itemTouchHelpCallback = (DefaultItemTouchHelpCallback) getCallback(); 46 | } 47 | 48 | /** 49 | * 设置是否可以被拖拽 50 | * 51 | * @param canDrag 是true,否false 52 | */ 53 | public void setDragEnable(boolean canDrag) { 54 | itemTouchHelpCallback.setDragEnable(canDrag); 55 | } 56 | 57 | /** 58 | * 设置是否可以被滑动 59 | * 60 | * @param canSwipe 是true,否false 61 | */ 62 | public void setSwipeEnable(boolean canSwipe) { 63 | itemTouchHelpCallback.setSwipeEnable(canSwipe); 64 | } 65 | } 66 | ``` 67 | 这个类用户设置拖拽,滑动动作的可用性。 68 | 69 | ``` 70 | public class DefaultItemTouchHelpCallback extends ItemTouchHelper.Callback { 71 | 72 | /** 73 | * Item操作的回调 74 | */ 75 | private OnItemTouchCallbackListener onItemTouchCallbackListener; 76 | 77 | /** 78 | * 是否可以拖拽 79 | */ 80 | private boolean isCanDrag = false; 81 | /** 82 | * 是否可以被滑动 83 | */ 84 | private boolean isCanSwipe = false; 85 | 86 | public DefaultItemTouchHelpCallback(OnItemTouchCallbackListener onItemTouchCallbackListener) { 87 | this.onItemTouchCallbackListener = onItemTouchCallbackListener; 88 | } 89 | 90 | /** 91 | * 设置Item操作的回调,去更新UI和数据源 92 | * 93 | * @param onItemTouchCallbackListener 94 | */ 95 | public void setOnItemTouchCallbackListener(OnItemTouchCallbackListener onItemTouchCallbackListener) { 96 | this.onItemTouchCallbackListener = onItemTouchCallbackListener; 97 | } 98 | 99 | /** 100 | * 设置是否可以被拖拽 101 | * 102 | * @param canDrag 是true,否false 103 | */ 104 | public void setDragEnable(boolean canDrag) { 105 | isCanDrag = canDrag; 106 | } 107 | 108 | /** 109 | * 设置是否可以被滑动 110 | * 111 | * @param canSwipe 是true,否false 112 | */ 113 | public void setSwipeEnable(boolean canSwipe) { 114 | isCanSwipe = canSwipe; 115 | } 116 | 117 | /** 118 | * 当Item被长按的时候是否可以被拖拽 119 | * 120 | * @return 121 | */ 122 | @Override 123 | public boolean isLongPressDragEnabled() { 124 | return isCanDrag; 125 | } 126 | 127 | /** 128 | * Item是否可以被滑动(H:左右滑动,V:上下滑动) 129 | * 130 | * @return 131 | */ 132 | @Override 133 | public boolean isItemViewSwipeEnabled() { 134 | return isCanSwipe; 135 | } 136 | 137 | /** 138 | * 当用户拖拽或者滑动Item的时候需要我们告诉系统滑动或者拖拽的方向 139 | * 140 | * @param recyclerView 141 | * @param viewHolder 142 | * 143 | * @return 144 | */ 145 | @Override 146 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 147 | RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 148 | if (layoutManager instanceof GridLayoutManager) {// GridLayoutManager 149 | // flag如果值是0,相当于这个功能被关闭 150 | int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN; 151 | int swipeFlag = 0; 152 | // create make 153 | return makeMovementFlags(dragFlag, swipeFlag); 154 | } else if (layoutManager instanceof LinearLayoutManager) {// linearLayoutManager 155 | LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; 156 | int orientation = linearLayoutManager.getOrientation(); 157 | 158 | int dragFlag = 0; 159 | int swipeFlag = 0; 160 | 161 | // 为了方便理解,相当于分为横着的ListView和竖着的ListView 162 | if (orientation == LinearLayoutManager.HORIZONTAL) {// 如果是横向的布局 163 | swipeFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 164 | dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 165 | } else if (orientation == LinearLayoutManager.VERTICAL) {// 如果是竖向的布局,相当于ListView 166 | dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 167 | swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 168 | } 169 | return makeMovementFlags(dragFlag, swipeFlag); 170 | } 171 | return 0; 172 | } 173 | 174 | /** 175 | * 当Item被拖拽的时候被回调 176 | * 177 | * @param recyclerView recyclerView 178 | * @param srcViewHolder 拖拽的ViewHolder 179 | * @param targetViewHolder 目的地的viewHolder 180 | * 181 | * @return 182 | */ 183 | @Override 184 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcViewHolder, RecyclerView.ViewHolder targetViewHolder) { 185 | if (onItemTouchCallbackListener != null) { 186 | return onItemTouchCallbackListener.onMove(srcViewHolder.getAdapterPosition(), targetViewHolder.getAdapterPosition()); 187 | } 188 | return false; 189 | } 190 | 191 | @Override 192 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 193 | if (onItemTouchCallbackListener != null) { 194 | onItemTouchCallbackListener.onSwiped(viewHolder.getAdapterPosition()); 195 | } 196 | } 197 | 198 | public interface OnItemTouchCallbackListener { 199 | /** 200 | * 当某个Item被滑动删除的时候 201 | * 202 | * @param adapterPosition item的position 203 | */ 204 | void onSwiped(int adapterPosition); 205 | 206 | /** 207 | * 当两个Item位置互换的时候被回调 208 | * 209 | * @param srcPosition 拖拽的item的position 210 | * @param targetPosition 目的地的Item的position 211 | * 212 | * @return 开发者处理了操作应该返回true,开发者没有处理就返回false 213 | */ 214 | boolean onMove(int srcPosition, int targetPosition); 215 | } 216 | } 217 | ``` 218 | 这个类用户处理拖拽,滑动等动作。 219 | 220 | 221 | 接下来,创建一个RecyclerView布局,初始化,设置布局管理器,设置适配器, 222 | 加入如下代码 223 | 224 | ``` 225 | 226 | // 把ItemTouchHelper和itemTouchHelper绑定 227 | itemTouchHelper = new DefaultItemTouchHelper(onItemTouchCallbackListener); 228 | itemTouchHelper.attachToRecyclerView(recyclerView); 229 | 230 | mainAdapter.setItemTouchHelper(itemTouchHelper); 231 | 232 | itemTouchHelper.setDragEnable(false); 233 | itemTouchHelper.setSwipeEnable(false); 234 | ``` 235 | 236 | ``` 237 | private DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener = new DefaultItemTouchHelpCallback.OnItemTouchCallbackListener() { 238 | @Override 239 | public void onSwiped(int adapterPosition) { 240 | if (userInfoList != null) { 241 | userInfoList.remove(adapterPosition); 242 | mainAdapter.notifyItemRemoved(adapterPosition); 243 | } 244 | } 245 | 246 | @Override 247 | public synchronized boolean onMove(int srcPosition, int targetPosition) { 248 | mSrcPosition = srcPosition; 249 | mTargetPosition = targetPosition; 250 | Log.e("mosr", "srcPosition: " + srcPosition); 251 | Log.e("mosr", "targetPosition: " + targetPosition); 252 | if (userInfoList != null) { 253 | // 更换数据源中的数据Item的位置 254 | Collections.swap(userInfoList, srcPosition, targetPosition); 255 | 256 | // 更新UI中的Item的位置,主要是给用户看到交互效果 257 | mainAdapter.notifyItemMoved(srcPosition, targetPosition); 258 | mSelectPostion = targetPosition; 259 | isMoveing = true; 260 | return true; 261 | } 262 | return false; 263 | } 264 | }; 265 | ``` 266 | 267 | Adapter代码处理 指定View 设置onLongClickListener 268 | 269 | ``` 270 | public void setHeight(int mHeight, RecyclerView.ViewHolder mViewHolder) { 271 | if (this.mHeight == mHeight) 272 | return; 273 | this.mHeight = mHeight; 274 | if (mHeight == 400) 275 | try { 276 | for (MainContentViewHolder viewHolder : mList) { 277 | viewHolder.setData(); 278 | } 279 | } catch (Exception e) { 280 | } 281 | else 282 | notifyDataSetChanged(); 283 | mList.clear(); 284 | if (null != mViewHolder) 285 | itemTouchHelper.startDrag(mViewHolder); 286 | 287 | } 288 | ``` 289 | **!!!因为拖拽时需求让图片的高度减少,所以要动态设置图片View 的高度,上述代码可见变小时不是用notifyDataSetChanged 而是通过ViewHolder的SetDate方法,是因为notifyDataSetChanged 会重新绘制布局导致ItemToucHelper失去焦点,谨记!!!** 290 | 291 | 当手离开屏幕时还原图片高度 292 | 293 | ``` 294 | @Override 295 | public boolean onTouch(View v, MotionEvent event) { 296 | switch (event.getAction()) { 297 | case MotionEvent.ACTION_MOVE: 298 | break; 299 | case MotionEvent.ACTION_UP: 300 | notifyItemView(); 301 | mainAdapter.setHeight(700, null); 302 | break; 303 | } 304 | return false; 305 | } 306 | 307 | ``` 308 | 309 | 处理图片之间的输入框,输入框与输入框之间的关系 310 | 311 | ``` 312 | private void notifyItemView() { 313 | if (!isMoveing) 314 | return; 315 | try { 316 | if (userInfoList.get(0).getIsPic() != 1)//第一行 317 | userInfoList.add(0, new InvitationInfo("", "", 1)); 318 | if (userInfoList.get(userInfoList.size() - 1).getIsPic() != 1) //最后一行 319 | userInfoList.add(new InvitationInfo("", "", 1)); 320 | for (int i = 0; i < userInfoList.size(); i++) { 321 | if (i > 0 && userInfoList.get(i).getIsPic() == 0 && userInfoList.get(i).getIsPic() == userInfoList.get(i - 1).getIsPic()) { 322 | userInfoList.add(i, new InvitationInfo("", "", 1)); 323 | mSelectPostion = mSelectPostion < i ? mSelectPostion : mSelectPostion++; 324 | i++; 325 | } 326 | } 327 | 328 | for (int i = 0; i < userInfoList.size(); i++) { 329 | if (userInfoList.get(i).getIsPic() == 1 && i > 0 && userInfoList.get(i).getIsPic() == userInfoList.get(i - 1).getIsPic()) 330 | if (TextUtils.isEmpty(userInfoList.get(i).getText())) { 331 | userInfoList.remove(i); 332 | mSelectPostion = mSelectPostion < i ? mSelectPostion : mSelectPostion--; 333 | i--; 334 | } else if (TextUtils.isEmpty(userInfoList.get(i - 1).getText())) { 335 | userInfoList.remove(i - 1); 336 | mSelectPostion = mSelectPostion < i - 1 ? mSelectPostion : mSelectPostion--; 337 | i--; 338 | } 339 | } 340 | } catch (IndexOutOfBoundsException e) { 341 | e.printStackTrace(); 342 | } finally { 343 | mainAdapter.notifyDataSetChanged(); 344 | recyclerView.smoothScrollToPosition(mSelectPostion); 345 | isMoveing = false; 346 | } 347 | } 348 | ``` 349 | 核心部分都在上面了,由于是调研时写的Demo 所以很多地方不严谨,逻辑不清晰,借鉴者多加留意,参考我在GitHub上托管的源码。 350 | 如有问题Q我1515789527 351 | -------------------------------------------------------------------------------- /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 | 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 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /main/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /main/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.mosr.recyclerview.itemTouchUsing" 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 | compile 'com.android.support:appcompat-v7:23.3.0' 25 | compile 'com.android.support:recyclerview-v7:23.3.0' 26 | compile 'com.yolanda.nohttp:nohttp:1.0.0' 27 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3' 28 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3' 29 | } 30 | -------------------------------------------------------------------------------- /main/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:\Develope\android-sdk-windows/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 | -------------------------------------------------------------------------------- /main/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /main/src/main/java/android/support/v7/widget/helper/MosrItemTouchHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package android.support.v7.widget.helper; 17 | 18 | public class MosrItemTouchHelper extends ItemTouchHelper { 19 | public MosrItemTouchHelper(Callback callback) { 20 | super(callback); 21 | } 22 | 23 | public Callback getCallback() { 24 | return mCallback; 25 | } 26 | 27 | public float getInitialTouchX() { 28 | return mInitialTouchX; 29 | } 30 | 31 | public float getInitialTouchY() { 32 | return mInitialTouchY; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/ListViewDecoration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.mosr.recyclerview.itemTouchUsing; 17 | 18 | import android.content.Context; 19 | import android.graphics.Canvas; 20 | import android.graphics.Rect; 21 | import android.graphics.drawable.Drawable; 22 | import android.support.v7.widget.RecyclerView; 23 | import android.view.View; 24 | 25 | import com.yolanda.nohttp.tools.ResourcesCompat; 26 | 27 | public class ListViewDecoration extends RecyclerView.ItemDecoration { 28 | 29 | private Drawable mDrawable; 30 | 31 | public ListViewDecoration(Context context, int resId) { 32 | mDrawable = ResourcesCompat.getDrawable(resId); 33 | } 34 | 35 | @Override 36 | public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 37 | final int left = parent.getPaddingLeft(); 38 | final int right = parent.getWidth() - parent.getPaddingRight(); 39 | 40 | final int childCount = parent.getChildCount(); 41 | for (int i = 0; i < childCount - 1; i++) { 42 | final View child = parent.getChildAt(i); 43 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 44 | // 以下计算主要用来确定绘制的位置 45 | final int top = child.getBottom() + params.bottomMargin; 46 | final int bottom = top + mDrawable.getIntrinsicHeight(); 47 | mDrawable.setBounds(left, top, right, bottom); 48 | mDrawable.draw(c); 49 | } 50 | } 51 | 52 | @Override 53 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 54 | outRect.set(0, 0, 0, mDrawable.getIntrinsicWidth()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.mosr.recyclerview.itemTouchUsing; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.DefaultItemAnimator; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.text.TextUtils; 9 | import android.util.Log; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.widget.Toast; 13 | 14 | import com.mosr.recyclerview.itemTouchUsing.adapter.MainAdapter; 15 | import com.mosr.recyclerview.itemTouchUsing.entity.InvitationInfo; 16 | import com.mosr.recyclerview.itemTouchUsing.itemtouch.DefaultItemTouchHelpCallback; 17 | import com.mosr.recyclerview.itemTouchUsing.itemtouch.DefaultItemTouchHelper; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collections; 21 | import java.util.List; 22 | 23 | public class MainActivity extends AppCompatActivity implements View.OnTouchListener { 24 | 25 | private RecyclerView recyclerView; 26 | 27 | /** 28 | * 数据源 29 | */ 30 | private volatile List userInfoList = null; 31 | /** 32 | * 数据适配器 33 | */ 34 | private MainAdapter mainAdapter; 35 | /** 36 | * 滑动拖拽的帮助类 37 | */ 38 | private DefaultItemTouchHelper itemTouchHelper; 39 | 40 | private boolean isMoveing = false; 41 | private int mSrcPosition; 42 | private int mTargetPosition; 43 | /** 44 | * 选中的item位置 45 | */ 46 | private int mSelectPostion = 0; 47 | 48 | @Override 49 | protected void onCreate(Bundle savedInstanceState) { 50 | super.onCreate(savedInstanceState); 51 | setContentView(R.layout.activity_main); 52 | findViewById(R.id.btn_update).setOnClickListener(onClickListener); 53 | findViewById(R.id.btn_add).setOnClickListener(onClickListener); 54 | recyclerView = (RecyclerView) findViewById(R.id.rv_main); 55 | recyclerView.setItemAnimator(new DefaultItemAnimator()); 56 | 57 | // 必须要设置一个布局管理器 58 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 59 | linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 60 | recyclerView.setLayoutManager(linearLayoutManager); 61 | 62 | mainAdapter = new MainAdapter(userInfoList, this, recyclerView); 63 | mainAdapter.setOnItemClickListener(onItemClickListener); 64 | recyclerView.setAdapter(mainAdapter); 65 | recyclerView.setOnTouchListener(this); 66 | // 模拟数据 67 | userInfoList = new ArrayList<>(); 68 | userInfoList.add(new InvitationInfo("", "", 1)); 69 | userInfoList.add(new InvitationInfo("", "", 0)); 70 | userInfoList.add(new InvitationInfo("", "", 1)); 71 | userInfoList.add(new InvitationInfo("", "", 0)); 72 | userInfoList.add(new InvitationInfo("", "", 1)); 73 | mainAdapter.notifyDataSetChanged(userInfoList); 74 | 75 | 76 | // 把ItemTouchHelper和itemTouchHelper绑定 77 | itemTouchHelper = new DefaultItemTouchHelper(onItemTouchCallbackListener); 78 | itemTouchHelper.attachToRecyclerView(recyclerView); 79 | 80 | mainAdapter.setItemTouchHelper(itemTouchHelper); 81 | 82 | itemTouchHelper.setDragEnable(false); 83 | itemTouchHelper.setSwipeEnable(false); 84 | } 85 | 86 | private View.OnClickListener onClickListener = new View.OnClickListener() { 87 | @Override 88 | public void onClick(View v) { 89 | switch (v.getId()) { 90 | case R.id.btn_update: 91 | recyclerView.scrollToPosition(3); 92 | break; 93 | case R.id.btn_add: 94 | userInfoList.add(new InvitationInfo("", "", 0)); 95 | userInfoList.add(new InvitationInfo("", "", 1)); 96 | mainAdapter.notifyDataSetChanged(); 97 | recyclerView.scrollToPosition(mainAdapter.getItemCount() - 1); 98 | break; 99 | } 100 | } 101 | }; 102 | 103 | private DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener = new DefaultItemTouchHelpCallback.OnItemTouchCallbackListener() { 104 | @Override 105 | public void onSwiped(int adapterPosition) { 106 | if (userInfoList != null) { 107 | userInfoList.remove(adapterPosition); 108 | mainAdapter.notifyItemRemoved(adapterPosition); 109 | } 110 | } 111 | 112 | @Override 113 | public synchronized boolean onMove(int srcPosition, int targetPosition) { 114 | mSrcPosition = srcPosition; 115 | mTargetPosition = targetPosition; 116 | Log.e("mosr", "srcPosition: " + srcPosition); 117 | Log.e("mosr", "targetPosition: " + targetPosition); 118 | if (userInfoList != null) { 119 | // 更换数据源中的数据Item的位置 120 | Collections.swap(userInfoList, srcPosition, targetPosition); 121 | 122 | // 更新UI中的Item的位置,主要是给用户看到交互效果 123 | mainAdapter.notifyItemMoved(srcPosition, targetPosition); 124 | mSelectPostion = targetPosition; 125 | isMoveing = true; 126 | return true; 127 | } 128 | return false; 129 | } 130 | }; 131 | 132 | /** 133 | * RecyclerView的Item点击监听 134 | */ 135 | private MainAdapter.OnItemClickListener onItemClickListener = new MainAdapter.OnItemClickListener() { 136 | @Override 137 | public void onItemClick(View view, int position) { 138 | Toast.makeText(MainActivity.this, "第" + position + "被点击", Toast.LENGTH_SHORT).show(); 139 | } 140 | }; 141 | 142 | @Override 143 | public boolean onTouch(View v, MotionEvent event) { 144 | switch (event.getAction()) { 145 | case MotionEvent.ACTION_MOVE: 146 | break; 147 | case MotionEvent.ACTION_UP: 148 | notifyItemView(); 149 | mainAdapter.setHeight(700, null); 150 | break; 151 | } 152 | return false; 153 | } 154 | 155 | private void notifyItemView() { 156 | if (!isMoveing) 157 | return; 158 | try { 159 | if (userInfoList.get(0).getIsPic() != 1)//第一行 160 | userInfoList.add(0, new InvitationInfo("", "", 1)); 161 | if (userInfoList.get(userInfoList.size() - 1).getIsPic() != 1) //最后一行 162 | userInfoList.add(new InvitationInfo("", "", 1)); 163 | for (int i = 0; i < userInfoList.size(); i++) { 164 | if (i > 0 && userInfoList.get(i).getIsPic() == 0 && userInfoList.get(i).getIsPic() == userInfoList.get(i - 1).getIsPic()) { 165 | userInfoList.add(i, new InvitationInfo("", "", 1)); 166 | mSelectPostion = mSelectPostion < i ? mSelectPostion : mSelectPostion++; 167 | i++; 168 | } 169 | } 170 | 171 | for (int i = 0; i < userInfoList.size(); i++) { 172 | if (userInfoList.get(i).getIsPic() == 1 && i > 0 && userInfoList.get(i).getIsPic() == userInfoList.get(i - 1).getIsPic()) 173 | if (TextUtils.isEmpty(userInfoList.get(i).getText())) { 174 | userInfoList.remove(i); 175 | mSelectPostion = mSelectPostion < i ? mSelectPostion : mSelectPostion--; 176 | i--; 177 | } else if (TextUtils.isEmpty(userInfoList.get(i - 1).getText())) { 178 | userInfoList.remove(i - 1); 179 | mSelectPostion = mSelectPostion < i - 1 ? mSelectPostion : mSelectPostion--; 180 | i--; 181 | } 182 | } 183 | } catch (IndexOutOfBoundsException e) { 184 | e.printStackTrace(); 185 | } finally { 186 | mainAdapter.notifyDataSetChanged(); 187 | recyclerView.smoothScrollToPosition(mSelectPostion); 188 | isMoveing = false; 189 | } 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/WrapContentLinearLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.mosr.recyclerview.itemTouchUsing; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.LinearLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.AttributeSet; 7 | 8 | /** 9 | * Synopsis ${SYNOPSIS} 10 | * Author Mosr 11 | * version ${VERSION} 12 | * Create 2017/3/8 11:09 13 | * Email intimatestranger@sina.cn 14 | */ 15 | public class WrapContentLinearLayoutManager extends LinearLayoutManager { 16 | public WrapContentLinearLayoutManager(Context context) { 17 | super(context); 18 | } 19 | 20 | public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { 21 | super(context, orientation, reverseLayout); 22 | } 23 | 24 | public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 25 | super(context, attrs, defStyleAttr, defStyleRes); 26 | } 27 | 28 | @Override 29 | public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { 30 | try { 31 | super.onLayoutChildren(recycler, state); 32 | } catch (IndexOutOfBoundsException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/adapter/MainAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.mosr.recyclerview.itemTouchUsing.adapter; 17 | 18 | import android.content.Context; 19 | import android.graphics.Color; 20 | import android.graphics.drawable.Drawable; 21 | import android.graphics.drawable.GradientDrawable; 22 | import android.support.v7.widget.RecyclerView; 23 | import android.text.Editable; 24 | import android.text.TextUtils; 25 | import android.text.TextWatcher; 26 | import android.view.KeyEvent; 27 | import android.view.LayoutInflater; 28 | import android.view.MotionEvent; 29 | import android.view.View; 30 | import android.view.ViewGroup; 31 | import android.view.inputmethod.InputMethodManager; 32 | import android.widget.EditText; 33 | import android.widget.ImageButton; 34 | import android.widget.ImageView; 35 | import android.widget.LinearLayout; 36 | import android.widget.RelativeLayout; 37 | 38 | import com.mosr.recyclerview.itemTouchUsing.R; 39 | import com.mosr.recyclerview.itemTouchUsing.entity.InvitationInfo; 40 | import com.mosr.recyclerview.itemTouchUsing.itemtouch.DefaultItemTouchHelper; 41 | 42 | import java.util.ArrayList; 43 | import java.util.List; 44 | 45 | /** 46 | *

主页面的List的适配器

47 | */ 48 | public class MainAdapter extends RecyclerView.Adapter { 49 | /** 50 | * Item点击监听 51 | */ 52 | private OnItemClickListener mItemOnClickListener; 53 | /** 54 | * 数据 55 | */ 56 | private List userInfos = null; 57 | 58 | /** 59 | * Item拖拽滑动帮助 60 | */ 61 | private DefaultItemTouchHelper itemTouchHelper; 62 | 63 | private int mHeight = 700; 64 | 65 | private Context mContext; 66 | 67 | private Drawable mDrawable; 68 | private int mPostion; 69 | 70 | private boolean isLong = false; 71 | 72 | private RecyclerView mRecyclerView; 73 | private ArrayList mList; 74 | 75 | public MainAdapter() { 76 | } 77 | 78 | public MainAdapter(List userInfos, Context mContext, RecyclerView mRecyclerView) { 79 | this.userInfos = userInfos; 80 | this.mContext = mContext; 81 | this.mRecyclerView = mRecyclerView; 82 | mList = new ArrayList<>(); 83 | setEditTextDrawable(0); 84 | } 85 | 86 | public void notifyDataSetChanged(List userInfos) { 87 | this.userInfos = userInfos; 88 | super.notifyDataSetChanged(); 89 | } 90 | 91 | public void setItemTouchHelper(DefaultItemTouchHelper itemTouchHelper) { 92 | this.itemTouchHelper = itemTouchHelper; 93 | } 94 | 95 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) { 96 | this.mItemOnClickListener = onItemClickListener; 97 | } 98 | 99 | public void setEditTextDrawable(int resId) { 100 | mDrawable = mContext.getResources().getDrawable(resId == 0 ? android.R.drawable.ic_menu_edit : resId); 101 | mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), 102 | mDrawable.getIntrinsicHeight()); 103 | } 104 | 105 | @Override 106 | public MainContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 107 | return new MainContentViewHolder(LayoutInflater.from(parent.getContext()).inflate(viewType == 0 ? com.mosr.recyclerview.itemTouchUsing.R.layout.item_img : com.mosr.recyclerview.itemTouchUsing.R.layout.item_text, parent, false), viewType); 108 | } 109 | 110 | @Override 111 | public void onBindViewHolder(MainContentViewHolder holder, int position) { 112 | holder.setData(); 113 | mList.add(holder); 114 | } 115 | 116 | @Override 117 | public int getItemViewType(int position) { 118 | return userInfos.get(position).getIsPic(); 119 | } 120 | 121 | @Override 122 | public int getItemCount() { 123 | return userInfos == null ? 0 : userInfos.size(); 124 | } 125 | 126 | 127 | public InvitationInfo getData(int position) { 128 | if (position < 0) 129 | return null; 130 | return userInfos.get(position); 131 | } 132 | 133 | 134 | public int getHeight() { 135 | return mHeight; 136 | } 137 | 138 | public void setHeight(int mHeight, RecyclerView.ViewHolder mViewHolder) { 139 | if (this.mHeight == mHeight) 140 | return; 141 | this.mHeight = mHeight; 142 | if (mHeight == 400) 143 | try { 144 | for (MainContentViewHolder viewHolder : mList) { 145 | viewHolder.setData(); 146 | } 147 | } catch (Exception e) { 148 | } 149 | else 150 | notifyDataSetChanged(); 151 | mList.clear(); 152 | if (null != mViewHolder) 153 | itemTouchHelper.startDrag(mViewHolder); 154 | 155 | } 156 | 157 | public interface OnItemClickListener { 158 | void onItemClick(View view, int position); 159 | } 160 | 161 | class MainContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener, View.OnLongClickListener { 162 | private View itemView; 163 | private EditText edt_input; 164 | 165 | private ImageView img_pic; 166 | 167 | private ImageButton imb_move, imb_delete; 168 | 169 | private RelativeLayout rel_parent; 170 | 171 | private int mViewType; 172 | 173 | public MainContentViewHolder(View itemView, int viewType) { 174 | super(itemView); 175 | this.itemView = itemView; 176 | this.mViewType = viewType; 177 | itemView.setOnClickListener(this); 178 | switch (mViewType) { 179 | case 0: 180 | rel_parent = (RelativeLayout) itemView.findViewById(com.mosr.recyclerview.itemTouchUsing.R.id.rel_parent); 181 | img_pic = (ImageView) itemView.findViewById(com.mosr.recyclerview.itemTouchUsing.R.id.img_pic); 182 | imb_move = (ImageButton) itemView.findViewById(com.mosr.recyclerview.itemTouchUsing.R.id.imb_move); 183 | imb_delete = (ImageButton) itemView.findViewById(com.mosr.recyclerview.itemTouchUsing.R.id.imb_delete); 184 | imb_move.setOnLongClickListener(this); 185 | imb_delete.setOnClickListener(this); 186 | break; 187 | default: 188 | edt_input = (EditText) itemView.findViewById(com.mosr.recyclerview.itemTouchUsing.R.id.edt_input); 189 | edt_input.addTextChangedListener(new TextWatcher() { 190 | @Override 191 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 192 | } 193 | 194 | @Override 195 | public void onTextChanged(CharSequence s, int start, int before, int count) { 196 | userInfos.get(getAdapterPosition()).setText(s.toString()); 197 | } 198 | 199 | @Override 200 | public void afterTextChanged(Editable s) { 201 | 202 | } 203 | }); 204 | edt_input.setOnKeyListener(new View.OnKeyListener() { 205 | @Override 206 | public boolean onKey(View v, int keyCode, KeyEvent event) { 207 | if (keyCode == KeyEvent.KEYCODE_DEL 208 | && event.getAction() == KeyEvent.ACTION_DOWN) { 209 | if (v instanceof EditText) 210 | if (TextUtils.isEmpty(((EditText) v).getText().toString().trim()) && null != userInfos && userInfos.size() > 0) { 211 | if (getAdapterPosition() > 0 && userInfos.size() - 1 > getAdapterPosition() 212 | && (1 213 | == userInfos.get(getAdapterPosition() - 1).getIsPic() 214 | || userInfos.size() > getAdapterPosition() && 1 215 | == userInfos.get(getAdapterPosition() + 1).getIsPic())) {//中间item 216 | userInfos.remove(getAdapterPosition()); 217 | notifyItemRemoved(getAdapterPosition()); 218 | } else if (getAdapterPosition() == 0 && (userInfos.size() > 1 && 1 219 | == userInfos.get(getAdapterPosition() + 1).getIsPic())) {//头部item 220 | userInfos.remove(getAdapterPosition()); 221 | notifyItemRemoved(getAdapterPosition()); 222 | } else if (userInfos.size() - 1 == getAdapterPosition() && userInfos.size() > 1 223 | && userInfos.get(getAdapterPosition() - 1).getIsPic() == 1) {//尾部item 224 | userInfos.remove(getAdapterPosition()); 225 | notifyItemRemoved(getAdapterPosition()); 226 | } 227 | } else { 228 | int mLength = ((EditText) v).getText().toString().length(); 229 | ((EditText) v).getText().delete(mLength, mLength); 230 | } 231 | } 232 | return false; 233 | } 234 | }); 235 | /** 236 | * EditText得到和失去焦点时,自定义处理内容 237 | */ 238 | edt_input.setOnFocusChangeListener(new View.OnFocusChangeListener() { 239 | @Override 240 | public void onFocusChange(View v, boolean hasFocus) { 241 | if (v instanceof EditText) { 242 | boolean isEmpty = TextUtils.isEmpty(((EditText) v).getText().toString().trim()); 243 | ((EditText) v).setCompoundDrawables(null, null, !hasFocus && isEmpty ? mDrawable : 244 | null, null); 245 | if (!hasFocus) {//关闭软键盘 246 | InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 247 | imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 248 | } 249 | } 250 | } 251 | }); 252 | break; 253 | 254 | } 255 | } 256 | 257 | /** 258 | * 给这个Item设置数据 259 | */ 260 | public void setData() { 261 | switch (mViewType) { 262 | case 0: 263 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 264 | layoutParams.height = mHeight; 265 | rel_parent.setLayoutParams(layoutParams); 266 | break; 267 | default: 268 | InvitationInfo userInfo = getData(getAdapterPosition()); 269 | if (null == userInfo) 270 | return; 271 | if (!TextUtils.isEmpty(userInfo.getText())) { 272 | edt_input.setText(userInfo.getText()); 273 | edt_input.setSelection(userInfo.getText().length()); 274 | } else 275 | edt_input.setText(""); 276 | 277 | if (mHeight == 400) { 278 | GradientDrawable mGradientDrawable = new GradientDrawable(); 279 | mGradientDrawable.setShape(GradientDrawable.RECTANGLE); 280 | mGradientDrawable.setStroke(4, Color.BLUE, 16, 12); 281 | itemView.setBackgroundDrawable(mGradientDrawable); 282 | } else { 283 | itemView.setBackgroundColor(Color.TRANSPARENT); 284 | } 285 | edt_input.setMaxLines(edt_input.getLineCount() > 2 && mHeight == 300 ? 2 : Integer.MAX_VALUE); 286 | edt_input.setSelection(0); 287 | 288 | edt_input.setCompoundDrawables(null, null, TextUtils.isEmpty(edt_input.getText().toString().trim()) ? mDrawable : 289 | null, null); 290 | break; 291 | } 292 | } 293 | 294 | @Override 295 | public void onClick(View view) { 296 | try { 297 | if (view == itemView && itemTouchHelper != null) { 298 | mItemOnClickListener.onItemClick(view, getAdapterPosition()); 299 | } else if (view.getId() == com.mosr.recyclerview.itemTouchUsing.R.id.imb_delete) { 300 | if (MainAdapter.this.getItemViewType(getAdapterPosition() - 1) == 1 301 | && MainAdapter.this.getItemViewType(getAdapterPosition() - 1) == MainAdapter.this.getItemViewType(getAdapterPosition() + 1)) { 302 | if (TextUtils.isEmpty(userInfos.get(getAdapterPosition() - 1).getText())) { 303 | userInfos.remove(getAdapterPosition() - 1); 304 | notifyItemRemoved(getAdapterPosition() - 1); 305 | } else if (TextUtils.isEmpty(userInfos.get(getAdapterPosition() + 1).getText())) { 306 | userInfos.remove(getAdapterPosition() + 1); 307 | notifyItemRemoved(getAdapterPosition() + 1); 308 | } 309 | } 310 | userInfos.remove(getAdapterPosition()); 311 | notifyItemRemoved(getAdapterPosition()); 312 | } 313 | } catch (IndexOutOfBoundsException e) { 314 | e.printStackTrace(); 315 | } 316 | } 317 | 318 | @Override 319 | public boolean onTouch(View view, MotionEvent event) { 320 | if (event.getAction() == MotionEvent.ACTION_UP) 321 | setHeight(700, null); 322 | switch (view.getId()) { 323 | case R.id.imb_move: 324 | if (event.getAction() == MotionEvent.ACTION_DOWN) 325 | setHeight(400, this); 326 | itemTouchHelper.startDrag(this); 327 | break; 328 | // 329 | } 330 | return false; 331 | } 332 | 333 | @Override 334 | public boolean onLongClick(View v) { 335 | setHeight(400, this); 336 | return true; 337 | } 338 | } 339 | 340 | } 341 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/app.java: -------------------------------------------------------------------------------- 1 | package com.mosr.recyclerview.itemTouchUsing; 2 | 3 | import android.app.Application; 4 | 5 | import com.squareup.leakcanary.LeakCanary; 6 | 7 | /** 8 | * Synopsis ${SYNOPSIS} 9 | * Author Mosr 10 | * version ${VERSION} 11 | * Create 2017/3/10 15:40 12 | * Email intimatestranger@sina.cn 13 | */ 14 | public class app extends Application { 15 | @Override 16 | public void onCreate() { 17 | super.onCreate(); 18 | LeakCanary.install(this); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/entity/InvitationInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.mosr.recyclerview.itemTouchUsing.entity; 17 | 18 | public class InvitationInfo { 19 | 20 | private String text; 21 | private String image; 22 | 23 | /** 24 | * 是否是图片 0是1否 25 | */ 26 | private int isPic; 27 | 28 | public InvitationInfo() { 29 | } 30 | 31 | public InvitationInfo(String text, String image, int isPic) { 32 | this.text = text; 33 | this.image = image; 34 | this.isPic = isPic; 35 | } 36 | 37 | public int getIsPic() { 38 | return isPic; 39 | } 40 | 41 | public void setIsPic(int isPic) { 42 | this.isPic = isPic; 43 | } 44 | 45 | public String getText() { 46 | return text; 47 | } 48 | 49 | public void setText(String text) { 50 | this.text = text; 51 | } 52 | 53 | public String getImage() { 54 | return image; 55 | } 56 | 57 | public void setImage(String image) { 58 | this.image = image; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/itemtouch/DefaultItemTouchHelpCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.mosr.recyclerview.itemTouchUsing.itemtouch; 17 | 18 | import android.support.v7.widget.GridLayoutManager; 19 | import android.support.v7.widget.LinearLayoutManager; 20 | import android.support.v7.widget.RecyclerView; 21 | import android.support.v7.widget.helper.ItemTouchHelper; 22 | 23 | public class DefaultItemTouchHelpCallback extends ItemTouchHelper.Callback { 24 | 25 | /** 26 | * Item操作的回调 27 | */ 28 | private OnItemTouchCallbackListener onItemTouchCallbackListener; 29 | 30 | /** 31 | * 是否可以拖拽 32 | */ 33 | private boolean isCanDrag = false; 34 | /** 35 | * 是否可以被滑动 36 | */ 37 | private boolean isCanSwipe = false; 38 | 39 | public DefaultItemTouchHelpCallback(OnItemTouchCallbackListener onItemTouchCallbackListener) { 40 | this.onItemTouchCallbackListener = onItemTouchCallbackListener; 41 | } 42 | 43 | /** 44 | * 设置Item操作的回调,去更新UI和数据源 45 | * 46 | * @param onItemTouchCallbackListener 47 | */ 48 | public void setOnItemTouchCallbackListener(OnItemTouchCallbackListener onItemTouchCallbackListener) { 49 | this.onItemTouchCallbackListener = onItemTouchCallbackListener; 50 | } 51 | 52 | /** 53 | * 设置是否可以被拖拽 54 | * 55 | * @param canDrag 是true,否false 56 | */ 57 | public void setDragEnable(boolean canDrag) { 58 | isCanDrag = canDrag; 59 | } 60 | 61 | /** 62 | * 设置是否可以被滑动 63 | * 64 | * @param canSwipe 是true,否false 65 | */ 66 | public void setSwipeEnable(boolean canSwipe) { 67 | isCanSwipe = canSwipe; 68 | } 69 | 70 | /** 71 | * 当Item被长按的时候是否可以被拖拽 72 | * 73 | * @return 74 | */ 75 | @Override 76 | public boolean isLongPressDragEnabled() { 77 | return isCanDrag; 78 | } 79 | 80 | /** 81 | * Item是否可以被滑动(H:左右滑动,V:上下滑动) 82 | * 83 | * @return 84 | */ 85 | @Override 86 | public boolean isItemViewSwipeEnabled() { 87 | return isCanSwipe; 88 | } 89 | 90 | /** 91 | * 当用户拖拽或者滑动Item的时候需要我们告诉系统滑动或者拖拽的方向 92 | * 93 | * @param recyclerView 94 | * @param viewHolder 95 | * 96 | * @return 97 | */ 98 | @Override 99 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 100 | RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 101 | if (layoutManager instanceof GridLayoutManager) {// GridLayoutManager 102 | // flag如果值是0,相当于这个功能被关闭 103 | int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN; 104 | int swipeFlag = 0; 105 | // create make 106 | return makeMovementFlags(dragFlag, swipeFlag); 107 | } else if (layoutManager instanceof LinearLayoutManager) {// linearLayoutManager 108 | LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; 109 | int orientation = linearLayoutManager.getOrientation(); 110 | 111 | int dragFlag = 0; 112 | int swipeFlag = 0; 113 | 114 | // 为了方便理解,相当于分为横着的ListView和竖着的ListView 115 | if (orientation == LinearLayoutManager.HORIZONTAL) {// 如果是横向的布局 116 | swipeFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 117 | dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 118 | } else if (orientation == LinearLayoutManager.VERTICAL) {// 如果是竖向的布局,相当于ListView 119 | dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 120 | swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 121 | } 122 | return makeMovementFlags(dragFlag, swipeFlag); 123 | } 124 | return 0; 125 | } 126 | 127 | /** 128 | * 当Item被拖拽的时候被回调 129 | * 130 | * @param recyclerView recyclerView 131 | * @param srcViewHolder 拖拽的ViewHolder 132 | * @param targetViewHolder 目的地的viewHolder 133 | * 134 | * @return 135 | */ 136 | @Override 137 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcViewHolder, RecyclerView.ViewHolder targetViewHolder) { 138 | if (onItemTouchCallbackListener != null) { 139 | return onItemTouchCallbackListener.onMove(srcViewHolder.getAdapterPosition(), targetViewHolder.getAdapterPosition()); 140 | } 141 | return false; 142 | } 143 | 144 | @Override 145 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 146 | if (onItemTouchCallbackListener != null) { 147 | onItemTouchCallbackListener.onSwiped(viewHolder.getAdapterPosition()); 148 | } 149 | } 150 | 151 | public interface OnItemTouchCallbackListener { 152 | /** 153 | * 当某个Item被滑动删除的时候 154 | * 155 | * @param adapterPosition item的position 156 | */ 157 | void onSwiped(int adapterPosition); 158 | 159 | /** 160 | * 当两个Item位置互换的时候被回调 161 | * 162 | * @param srcPosition 拖拽的item的position 163 | * @param targetPosition 目的地的Item的position 164 | * 165 | * @return 开发者处理了操作应该返回true,开发者没有处理就返回false 166 | */ 167 | boolean onMove(int srcPosition, int targetPosition); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /main/src/main/java/com/mosr/recyclerview/itemTouchUsing/itemtouch/DefaultItemTouchHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Yolanda. All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.mosr.recyclerview.itemTouchUsing.itemtouch; 17 | 18 | import android.support.v7.widget.helper.MosrItemTouchHelper; 19 | 20 | public class DefaultItemTouchHelper extends MosrItemTouchHelper { 21 | 22 | private DefaultItemTouchHelpCallback itemTouchHelpCallback; 23 | 24 | public DefaultItemTouchHelper(DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener) { 25 | super(new DefaultItemTouchHelpCallback(onItemTouchCallbackListener)); 26 | itemTouchHelpCallback = (DefaultItemTouchHelpCallback) getCallback(); 27 | } 28 | 29 | /** 30 | * 设置是否可以被拖拽 31 | * 32 | * @param canDrag 是true,否false 33 | */ 34 | public void setDragEnable(boolean canDrag) { 35 | itemTouchHelpCallback.setDragEnable(canDrag); 36 | } 37 | 38 | /** 39 | * 设置是否可以被滑动 40 | * 41 | * @param canSwipe 是true,否false 42 | */ 43 | public void setSwipeEnable(boolean canSwipe) { 44 | itemTouchHelpCallback.setSwipeEnable(canSwipe); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /main/src/main/res/drawable/divider_recycler.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /main/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 |