├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_channel_edit.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── dimens.xml
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── drawable
│ │ │ ├── bg_channel_n.xml
│ │ │ ├── bg_channel_p.xml
│ │ │ ├── bg_channel.xml
│ │ │ └── bg_btn.xml
│ │ ├── layout
│ │ │ ├── item_drag.xml
│ │ │ ├── activity_demo.xml
│ │ │ ├── activity_main.xml
│ │ │ ├── item_other_channel_header.xml
│ │ │ ├── item_other.xml
│ │ │ ├── item_my.xml
│ │ │ └── item_my_channel_header.xml
│ │ └── values-w820dp
│ │ │ └── dimens.xml
│ │ ├── java
│ │ └── me
│ │ │ └── yokeyword
│ │ │ └── itemtouchhelperdemo
│ │ │ ├── helper
│ │ │ ├── OnItemMoveListener.java
│ │ │ ├── OnDragVHListener.java
│ │ │ └── ItemDragHelperCallback.java
│ │ │ ├── demochannel
│ │ │ ├── ChannelEntity.java
│ │ │ ├── ChannelActivity.java
│ │ │ └── ChannelAdapter.java
│ │ │ ├── MainActivity.java
│ │ │ └── demodrag
│ │ │ ├── DragActivity.java
│ │ │ └── DragAdapter.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
├── compiler.xml
├── gradle.xml
└── misc.xml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | ItemTouchHelperDemo
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_channel_edit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_channel_edit.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/ItemTouchHelperDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_channel_n.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_channel_p.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ItemTouchHelperDemo
2 | 使用ItemTouchHelper实现今日头条 网易新闻 的频道排序、频道移动
3 |
4 | # 效果演示
5 | 
6 |
7 | # 源码分析
8 | 查看[我的这篇简书](http://www.jianshu.com/p/d30fd8da4eac)
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 21 11:34:03 PDT 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.8-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/helper/OnItemMoveListener.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.helper;
2 |
3 | /**
4 | * Item移动后 触发
5 | * Created by YoKeyword on 15/12/28.
6 | */
7 | public interface OnItemMoveListener {
8 | void onItemMove(int fromPosition, int toPosition);
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ItemTouchHelperDemo
3 | 长按编辑,拖拽排序
4 | 我的频道
5 | 其他频道
6 | 编辑
7 | 完成
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_drag.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_channel.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #9c9c9c
7 | #DDDDDD
8 | #ef4836
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/helper/OnDragVHListener.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.helper;
2 |
3 | /**
4 | * ViewHolder 被选中 以及 拖拽释放 触发监听器
5 | * Created by YoKeyword on 15/12/29.
6 | */
7 | public interface OnDragVHListener {
8 | /**
9 | * Item被选中时触发
10 | */
11 | void onItemSelected();
12 |
13 |
14 | /**
15 | * Item在拖拽结束/滑动结束后触发
16 | */
17 | void onItemFinish();
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | e
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_btn.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/demochannel/ChannelEntity.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.demochannel;
2 |
3 | /**
4 | * 频道实体类
5 | * Created by YoKeyword on 15/12/29.
6 | */
7 | public class ChannelEntity {
8 |
9 | private long id;
10 | private String name;
11 |
12 | public long getId() {
13 | return id;
14 | }
15 |
16 | public void setId(long id) {
17 | this.id = id;
18 | }
19 |
20 | public String getName() {
21 | return name;
22 | }
23 |
24 | public void setName(String name) {
25 | this.name = name;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/YoKeyword/Documents/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
21 |
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | applicationId "me.yokeyword.itemtouchhelperdemo"
9 | minSdkVersion 14
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.1.1'
25 | compile 'com.android.support:recyclerview-v7:23.1.1'
26 | }
27 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_other_channel_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
14 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_other.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_my.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
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
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 | import android.widget.Button;
9 |
10 | import me.yokeyword.itemtouchhelperdemo.demochannel.ChannelActivity;
11 | import me.yokeyword.itemtouchhelperdemo.demodrag.DragActivity;
12 |
13 | /**
14 | * Created by YoKeyword on 16/1/4.
15 | */
16 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
17 |
18 | @Override
19 | protected void onCreate(@Nullable Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 |
23 | Button mBtnDrag = (Button) findViewById(R.id.btn_drag);
24 | Button mBtnChannel = (Button) findViewById(R.id.btn_channl);
25 | mBtnDrag.setOnClickListener(this);
26 | mBtnChannel.setOnClickListener(this);
27 | }
28 |
29 | @Override
30 | public void onClick(View v) {
31 | switch (v.getId()) {
32 | case R.id.btn_drag:
33 | startActivity(new Intent(MainActivity.this, DragActivity.class));
34 | break;
35 | case R.id.btn_channl:
36 | startActivity(new Intent(MainActivity.this, ChannelActivity.class));
37 | break;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_my_channel_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
24 |
25 |
37 |
38 |
43 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/demodrag/DragActivity.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.demodrag;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.GridLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.support.v7.widget.helper.ItemTouchHelper;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | import me.yokeyword.itemtouchhelperdemo.R;
13 | import me.yokeyword.itemtouchhelperdemo.helper.ItemDragHelperCallback;
14 |
15 | /**
16 | * 拖拽
17 | * Created by YoKeyword on 16/1/4.
18 | */
19 | public class DragActivity extends AppCompatActivity {
20 | private RecyclerView mRecy;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_demo);
26 |
27 | mRecy = (RecyclerView) findViewById(R.id.recy);
28 | init();
29 | }
30 |
31 | private void init() {
32 | final List items = new ArrayList<>();
33 | for (int i = 0; i < 18; i++) {
34 | items.add("Index " + i);
35 | }
36 |
37 | GridLayoutManager manager = new GridLayoutManager(this, 2);
38 | mRecy.setLayoutManager(manager);
39 |
40 | ItemDragHelperCallback callback = new ItemDragHelperCallback(){
41 | @Override
42 | public boolean isLongPressDragEnabled() {
43 | // 长按拖拽打开
44 | return true;
45 | }
46 | };
47 | ItemTouchHelper helper = new ItemTouchHelper(callback);
48 | helper.attachToRecyclerView(mRecy);
49 |
50 | DragAdapter adapter = new DragAdapter(this, items);
51 | mRecy.setAdapter(adapter);
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/demodrag/DragAdapter.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.demodrag;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 |
11 | import java.util.List;
12 |
13 | import me.yokeyword.itemtouchhelperdemo.R;
14 | import me.yokeyword.itemtouchhelperdemo.helper.OnDragVHListener;
15 | import me.yokeyword.itemtouchhelperdemo.helper.OnItemMoveListener;
16 |
17 | /**
18 | * 仅拖拽排序
19 | * Created by YoKeyword on 16/1/4.
20 | */
21 | public class DragAdapter extends RecyclerView.Adapter implements OnItemMoveListener {
22 | private List mItems;
23 | private LayoutInflater mInflater;
24 |
25 | public DragAdapter(Context context, List items) {
26 | mInflater = LayoutInflater.from(context);
27 | this.mItems = items;
28 | }
29 |
30 | @Override
31 | public DragViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
32 | return new DragViewHolder(mInflater.inflate(R.layout.item_drag, parent, false));
33 | }
34 |
35 | @Override
36 | public void onBindViewHolder(DragViewHolder holder, int position) {
37 | holder.tv.setText(mItems.get(position));
38 | }
39 |
40 | @Override
41 | public int getItemCount() {
42 | return mItems.size();
43 | }
44 |
45 | @Override
46 | public void onItemMove(int fromPosition, int toPosition) {
47 | String item = mItems.get(fromPosition);
48 | mItems.remove(fromPosition);
49 | mItems.add(toPosition, item);
50 | notifyItemMoved(fromPosition, toPosition);
51 | }
52 |
53 | class DragViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener{
54 | TextView tv;
55 |
56 | public DragViewHolder(View itemView) {
57 | super(itemView);
58 | tv = (TextView) itemView.findViewById(R.id.tv);
59 | }
60 |
61 | @Override
62 | public void onItemSelected() {
63 | itemView.setBackgroundColor(Color.LTGRAY);
64 | }
65 |
66 | @Override
67 | public void onItemFinish() {
68 | itemView.setBackgroundColor(0);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/demochannel/ChannelActivity.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.demochannel;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.support.v7.widget.GridLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.support.v7.widget.helper.ItemTouchHelper;
8 | import android.view.View;
9 | import android.widget.Toast;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import me.yokeyword.itemtouchhelperdemo.R;
15 | import me.yokeyword.itemtouchhelperdemo.helper.ItemDragHelperCallback;
16 |
17 | /**
18 | * 频道 增删改查 排序
19 | * Created by YoKeyword on 15/12/29.
20 | */
21 | public class ChannelActivity extends AppCompatActivity {
22 |
23 | private RecyclerView mRecy;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_demo);
29 |
30 | mRecy = (RecyclerView) findViewById(R.id.recy);
31 | init();
32 | }
33 |
34 | private void init() {
35 | final List items = new ArrayList<>();
36 | for (int i = 0; i < 18; i++) {
37 | ChannelEntity entity = new ChannelEntity();
38 | entity.setName("频道" + i);
39 | items.add(entity);
40 | }
41 | final List otherItems = new ArrayList<>();
42 | for (int i = 0; i < 20; i++) {
43 | ChannelEntity entity = new ChannelEntity();
44 | entity.setName("其他" + i);
45 | otherItems.add(entity);
46 | }
47 |
48 | GridLayoutManager manager = new GridLayoutManager(this, 4);
49 | mRecy.setLayoutManager(manager);
50 |
51 | ItemDragHelperCallback callback = new ItemDragHelperCallback();
52 | final ItemTouchHelper helper = new ItemTouchHelper(callback);
53 | helper.attachToRecyclerView(mRecy);
54 |
55 | final ChannelAdapter adapter = new ChannelAdapter(this, helper, items, otherItems);
56 | manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
57 | @Override
58 | public int getSpanSize(int position) {
59 | int viewType = adapter.getItemViewType(position);
60 | return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
61 | }
62 | });
63 | mRecy.setAdapter(adapter);
64 |
65 | adapter.setOnMyChannelItemClickListener(new ChannelAdapter.OnMyChannelItemClickListener() {
66 | @Override
67 | public void onItemClick(View v, int position) {
68 | Toast.makeText(ChannelActivity.this, items.get(position).getName(), Toast.LENGTH_SHORT).show();
69 | }
70 | });
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/helper/ItemDragHelperCallback.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.helper;
2 |
3 | import android.support.v7.widget.GridLayoutManager;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.support.v7.widget.StaggeredGridLayoutManager;
6 | import android.support.v7.widget.helper.ItemTouchHelper;
7 |
8 | /**
9 | * ItemDragHelperCallback
10 | * Created by YoKeyword on 15/12/29.
11 | */
12 | public class ItemDragHelperCallback extends ItemTouchHelper.Callback {
13 |
14 | @Override
15 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
16 | int dragFlags;
17 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
18 | if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager) {
19 | dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
20 | } else {
21 | dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
22 | }
23 | // 如果想支持滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END
24 | int swipeFlags = 0;
25 | return makeMovementFlags(dragFlags, swipeFlags);
26 | }
27 |
28 | @Override
29 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
30 | // 不同Type之间不可移动
31 | if (viewHolder.getItemViewType() != target.getItemViewType()) {
32 | return false;
33 | }
34 |
35 | if (recyclerView.getAdapter() instanceof OnItemMoveListener) {
36 | OnItemMoveListener listener = ((OnItemMoveListener) recyclerView.getAdapter());
37 | listener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
38 | }
39 | return true;
40 | }
41 |
42 | @Override
43 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
44 |
45 | }
46 |
47 | @Override
48 | public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
49 | // 不在闲置状态
50 | if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
51 | if (viewHolder instanceof OnDragVHListener) {
52 | OnDragVHListener itemViewHolder = (OnDragVHListener) viewHolder;
53 | itemViewHolder.onItemSelected();
54 | }
55 | }
56 | super.onSelectedChanged(viewHolder, actionState);
57 | }
58 |
59 | @Override
60 | public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
61 | if (viewHolder instanceof OnDragVHListener) {
62 | OnDragVHListener itemViewHolder = (OnDragVHListener) viewHolder;
63 | itemViewHolder.onItemFinish();
64 | }
65 | super.clearView(recyclerView, viewHolder);
66 | }
67 |
68 | @Override
69 | public boolean isLongPressDragEnabled() {
70 | // 不支持长按拖拽功能 手动控制
71 | return false;
72 | }
73 |
74 | @Override
75 | public boolean isItemViewSwipeEnabled() {
76 | // 不支持滑动功能
77 | return false;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | Abstraction issuesJava
39 |
40 |
41 | Android
42 |
43 |
44 | Android Lint
45 |
46 |
47 | Assignment issuesJava
48 |
49 |
50 | Bitwise operation issuesJava
51 |
52 |
53 | C/C++
54 |
55 |
56 | Class metricsJava
57 |
58 |
59 | Class structureJava
60 |
61 |
62 | Cloning issuesJava
63 |
64 |
65 | Code maturity issuesJava
66 |
67 |
68 | Code style issuesJava
69 |
70 |
71 | Compiler issuesJava
72 |
73 |
74 | Control FlowGroovy
75 |
76 |
77 | Control flow issuesJava
78 |
79 |
80 | Data flow issuesJava
81 |
82 |
83 | Dependency issuesJava
84 |
85 |
86 | Encapsulation issuesJava
87 |
88 |
89 | Error handlingJava
90 |
91 |
92 | Finalization issuesJava
93 |
94 |
95 | General
96 |
97 |
98 | GeneralC/C++
99 |
100 |
101 | GeneralJava
102 |
103 |
104 | Google Cloud Endpoints
105 |
106 |
107 | Gradle
108 |
109 |
110 | Groovy
111 |
112 |
113 | HTML
114 |
115 |
116 | ImportsJava
117 |
118 |
119 | Inheritance issuesJava
120 |
121 |
122 | Initialization issuesJava
123 |
124 |
125 | Internationalization issues
126 |
127 |
128 | Internationalization issuesJava
129 |
130 |
131 | J2ME issuesJava
132 |
133 |
134 | JUnit issuesJava
135 |
136 |
137 | Java
138 |
139 |
140 | Java language level issuesJava
141 |
142 |
143 | Java language level migration aidsJava
144 |
145 |
146 | JavaBeans issuesJava
147 |
148 |
149 | JavaFX
150 |
151 |
152 | Javadoc issuesJava
153 |
154 |
155 | Logging issuesJava
156 |
157 |
158 | Manifest
159 |
160 |
161 | Maven
162 |
163 |
164 | Memory issuesJava
165 |
166 |
167 | Method MetricsGroovy
168 |
169 |
170 | Method metricsJava
171 |
172 |
173 | Modularization issuesJava
174 |
175 |
176 | Naming ConventionsGroovy
177 |
178 |
179 | Numeric issuesJava
180 |
181 |
182 | Packaging issuesJava
183 |
184 |
185 | Pattern Validation
186 |
187 |
188 | Performance issuesJava
189 |
190 |
191 | Portability issuesJava
192 |
193 |
194 | Potentially confusing code constructsGroovy
195 |
196 |
197 | Probable bugsGradle
198 |
199 |
200 | Probable bugsGroovy
201 |
202 |
203 | Probable bugsJava
204 |
205 |
206 | Properties Files
207 |
208 |
209 | Properties FilesJava
210 |
211 |
212 | Security issuesJava
213 |
214 |
215 | Serialization issuesJava
216 |
217 |
218 | StyleGroovy
219 |
220 |
221 | TestNG
222 |
223 |
224 | Threading issuesGroovy
225 |
226 |
227 | Threading issuesJava
228 |
229 |
230 | Visibility issuesJava
231 |
232 |
233 | toString() issuesJava
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 | 1.8
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
--------------------------------------------------------------------------------
/app/src/main/java/me/yokeyword/itemtouchhelperdemo/demochannel/ChannelAdapter.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.itemtouchhelperdemo.demochannel;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.os.Handler;
6 | import android.support.v4.view.MotionEventCompat;
7 | import android.support.v7.widget.GridLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.support.v7.widget.helper.ItemTouchHelper;
10 | import android.view.LayoutInflater;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.view.animation.Animation;
15 | import android.view.animation.TranslateAnimation;
16 | import android.widget.FrameLayout;
17 | import android.widget.ImageView;
18 | import android.widget.TextView;
19 |
20 | import java.util.List;
21 |
22 | import me.yokeyword.itemtouchhelperdemo.R;
23 | import me.yokeyword.itemtouchhelperdemo.helper.OnDragVHListener;
24 | import me.yokeyword.itemtouchhelperdemo.helper.OnItemMoveListener;
25 |
26 | /**
27 | * 拖拽排序 + 增删
28 | * Created by YoKeyword on 15/12/28.
29 | */
30 | public class ChannelAdapter extends RecyclerView.Adapter implements OnItemMoveListener {
31 | // 我的频道 标题部分
32 | public static final int TYPE_MY_CHANNEL_HEADER = 0;
33 | // 我的频道
34 | public static final int TYPE_MY = 1;
35 | // 其他频道 标题部分
36 | public static final int TYPE_OTHER_CHANNEL_HEADER = 2;
37 | // 其他频道
38 | public static final int TYPE_OTHER = 3;
39 |
40 | // 我的频道之前的header数量 该demo中 即标题部分 为 1
41 | private static final int COUNT_PRE_MY_HEADER = 1;
42 | // 其他频道之前的header数量 该demo中 即标题部分 为 COUNT_PRE_MY_HEADER + 1
43 | private static final int COUNT_PRE_OTHER_HEADER = COUNT_PRE_MY_HEADER + 1;
44 |
45 | private static final long ANIM_TIME = 360L;
46 |
47 | // touch 点击开始时间
48 | private long startTime;
49 | // touch 间隔时间 用于分辨是否是 "点击"
50 | private static final long SPACE_TIME = 100;
51 |
52 | private LayoutInflater mInflater;
53 | private ItemTouchHelper mItemTouchHelper;
54 |
55 | // 是否为 编辑 模式
56 | private boolean isEditMode;
57 |
58 | private List mMyChannelItems, mOtherChannelItems;
59 |
60 | // 我的频道点击事件
61 | private OnMyChannelItemClickListener mChannelItemClickListener;
62 |
63 | public ChannelAdapter(Context context, ItemTouchHelper helper, List mMyChannelItems, List mOtherChannelItems) {
64 | this.mInflater = LayoutInflater.from(context);
65 | this.mItemTouchHelper = helper;
66 | this.mMyChannelItems = mMyChannelItems;
67 | this.mOtherChannelItems = mOtherChannelItems;
68 | }
69 |
70 | @Override
71 | public int getItemViewType(int position) {
72 | if (position == 0) { // 我的频道 标题部分
73 | return TYPE_MY_CHANNEL_HEADER;
74 | } else if (position == mMyChannelItems.size() + 1) { // 其他频道 标题部分
75 | return TYPE_OTHER_CHANNEL_HEADER;
76 | } else if (position > 0 && position < mMyChannelItems.size() + 1) {
77 | return TYPE_MY;
78 | } else {
79 | return TYPE_OTHER;
80 | }
81 | }
82 |
83 | @Override
84 | public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
85 | final View view;
86 | switch (viewType) {
87 | case TYPE_MY_CHANNEL_HEADER:
88 | view = mInflater.inflate(R.layout.item_my_channel_header, parent, false);
89 | final MyChannelHeaderViewHolder holder = new MyChannelHeaderViewHolder(view);
90 | holder.tvBtnEdit.setOnClickListener(new View.OnClickListener() {
91 | @Override
92 | public void onClick(View v) {
93 | if (!isEditMode) {
94 | startEditMode((RecyclerView) parent);
95 | holder.tvBtnEdit.setText(R.string.finish);
96 | } else {
97 | cancelEditMode((RecyclerView) parent);
98 | holder.tvBtnEdit.setText(R.string.edit);
99 | }
100 | }
101 | });
102 | return holder;
103 |
104 | case TYPE_MY:
105 | view = mInflater.inflate(R.layout.item_my, parent, false);
106 | final MyViewHolder myHolder = new MyViewHolder(view);
107 |
108 | myHolder.textView.setOnClickListener(new View.OnClickListener() {
109 | @Override
110 | public void onClick(final View v) {
111 | int position = myHolder.getAdapterPosition();
112 | if (isEditMode) {
113 | RecyclerView recyclerView = ((RecyclerView) parent);
114 | View targetView = recyclerView.getLayoutManager().findViewByPosition(mMyChannelItems.size() + COUNT_PRE_OTHER_HEADER);
115 | View currentView = recyclerView.getLayoutManager().findViewByPosition(position);
116 | // 如果targetView不在屏幕内,则indexOfChild为-1 此时不需要添加动画,因为此时notifyItemMoved自带一个向目标移动的动画
117 | // 如果在屏幕内,则添加一个位移动画
118 | if (recyclerView.indexOfChild(targetView) >= 0) {
119 | int targetX, targetY;
120 |
121 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
122 | int spanCount = ((GridLayoutManager) manager).getSpanCount();
123 |
124 | // 移动后 高度将变化 (我的频道Grid 最后一个item在新的一行第一个)
125 | if ((mMyChannelItems.size() - COUNT_PRE_MY_HEADER) % spanCount == 0) {
126 | View preTargetView = recyclerView.getLayoutManager().findViewByPosition(mMyChannelItems.size() + COUNT_PRE_OTHER_HEADER - 1);
127 | targetX = preTargetView.getLeft();
128 | targetY = preTargetView.getTop();
129 | } else {
130 | targetX = targetView.getLeft();
131 | targetY = targetView.getTop();
132 | }
133 |
134 | moveMyToOther(myHolder);
135 | startAnimation(recyclerView, currentView, targetX, targetY);
136 |
137 | } else {
138 | moveMyToOther(myHolder);
139 | }
140 | } else {
141 | mChannelItemClickListener.onItemClick(v, position - COUNT_PRE_MY_HEADER);
142 | }
143 | }
144 | });
145 |
146 | myHolder.textView.setOnLongClickListener(new View.OnLongClickListener() {
147 | @Override
148 | public boolean onLongClick(final View v) {
149 | if (!isEditMode) {
150 | RecyclerView recyclerView = ((RecyclerView) parent);
151 | startEditMode(recyclerView);
152 |
153 | // header 按钮文字 改成 "完成"
154 | View view = recyclerView.getChildAt(0);
155 | if (view == recyclerView.getLayoutManager().findViewByPosition(0)) {
156 | TextView tvBtnEdit = (TextView) view.findViewById(R.id.tv_btn_edit);
157 | tvBtnEdit.setText(R.string.finish);
158 | }
159 | }
160 |
161 | mItemTouchHelper.startDrag(myHolder);
162 | return true;
163 | }
164 | });
165 |
166 | myHolder.textView.setOnTouchListener(new View.OnTouchListener() {
167 | @Override
168 | public boolean onTouch(View v, MotionEvent event) {
169 | if (isEditMode) {
170 | switch (MotionEventCompat.getActionMasked(event)) {
171 | case MotionEvent.ACTION_DOWN:
172 | startTime = System.currentTimeMillis();
173 | break;
174 | case MotionEvent.ACTION_MOVE:
175 | if (System.currentTimeMillis() - startTime > SPACE_TIME) {
176 | mItemTouchHelper.startDrag(myHolder);
177 | }
178 | break;
179 | case MotionEvent.ACTION_CANCEL:
180 | case MotionEvent.ACTION_UP:
181 | startTime = 0;
182 | break;
183 | }
184 |
185 | }
186 | return false;
187 | }
188 | });
189 | return myHolder;
190 |
191 | case TYPE_OTHER_CHANNEL_HEADER:
192 | view = mInflater.inflate(R.layout.item_other_channel_header, parent, false);
193 | return new RecyclerView.ViewHolder(view) {
194 | };
195 |
196 | case TYPE_OTHER:
197 | view = mInflater.inflate(R.layout.item_other, parent, false);
198 | final OtherViewHolder otherHolder = new OtherViewHolder(view);
199 | otherHolder.textView.setOnClickListener(new View.OnClickListener() {
200 | @Override
201 | public void onClick(View v) {
202 | RecyclerView recyclerView = ((RecyclerView) parent);
203 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
204 | int currentPiosition = otherHolder.getAdapterPosition();
205 | // 如果RecyclerView滑动到底部,移动的目标位置的y轴 - height
206 | View currentView = manager.findViewByPosition(currentPiosition);
207 | // 目标位置的前一个item 即当前MyChannel的最后一个
208 | View preTargetView = manager.findViewByPosition(mMyChannelItems.size() - 1 + COUNT_PRE_MY_HEADER);
209 |
210 | // 如果targetView不在屏幕内,则为-1 此时不需要添加动画,因为此时notifyItemMoved自带一个向目标移动的动画
211 | // 如果在屏幕内,则添加一个位移动画
212 | if (recyclerView.indexOfChild(preTargetView) >= 0) {
213 | int targetX = preTargetView.getLeft();
214 | int targetY = preTargetView.getTop();
215 |
216 | int targetPosition = mMyChannelItems.size() - 1 + COUNT_PRE_OTHER_HEADER;
217 |
218 | GridLayoutManager gridLayoutManager = ((GridLayoutManager) manager);
219 | int spanCount = gridLayoutManager.getSpanCount();
220 | // target 在最后一行第一个
221 | if ((targetPosition - COUNT_PRE_MY_HEADER) % spanCount == 0) {
222 | View targetView = manager.findViewByPosition(targetPosition);
223 | targetX = targetView.getLeft();
224 | targetY = targetView.getTop();
225 | } else {
226 | targetX += preTargetView.getWidth();
227 |
228 | // 最后一个item可见
229 | if (gridLayoutManager.findLastVisibleItemPosition() == getItemCount() - 1) {
230 | // 最后的item在最后一行第一个位置
231 | if ((getItemCount() - 1 - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER) % spanCount == 0) {
232 | // RecyclerView实际高度 > 屏幕高度 && RecyclerView实际高度 < 屏幕高度 + item.height
233 | int firstVisiblePostion = gridLayoutManager.findFirstVisibleItemPosition();
234 | if (firstVisiblePostion == 0) {
235 | // FirstCompletelyVisibleItemPosition == 0 即 内容不满一屏幕 , targetY值不需要变化
236 | // // FirstCompletelyVisibleItemPosition != 0 即 内容满一屏幕 并且 可滑动 , targetY值 + firstItem.getTop
237 | if (gridLayoutManager.findFirstCompletelyVisibleItemPosition() != 0) {
238 | int offset = (-recyclerView.getChildAt(0).getTop()) - recyclerView.getPaddingTop();
239 | targetY += offset;
240 | }
241 | } else { // 在这种情况下 并且 RecyclerView高度变化时(即可见第一个item的 position != 0),
242 | // 移动后, targetY值 + 一个item的高度
243 | targetY += preTargetView.getHeight();
244 | }
245 | }
246 | } else {
247 | System.out.println("current--No");
248 | }
249 | }
250 |
251 | // 如果当前位置是otherChannel可见的最后一个
252 | // 并且 当前位置不在grid的第一个位置
253 | // 并且 目标位置不在grid的第一个位置
254 |
255 | // 则 需要延迟250秒 notifyItemMove , 这是因为这种情况 , 并不触发ItemAnimator , 会直接刷新界面
256 | // 导致我们的位移动画刚开始,就已经notify完毕,引起不同步问题
257 | if (currentPiosition == gridLayoutManager.findLastVisibleItemPosition()
258 | && (currentPiosition - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER) % spanCount != 0
259 | && (targetPosition - COUNT_PRE_MY_HEADER) % spanCount != 0) {
260 | moveOtherToMyWithDelay(otherHolder);
261 | } else {
262 | moveOtherToMy(otherHolder);
263 | }
264 | startAnimation(recyclerView, currentView, targetX, targetY);
265 |
266 | } else {
267 | moveOtherToMy(otherHolder);
268 | }
269 | }
270 | });
271 | return otherHolder;
272 | }
273 | return null;
274 | }
275 |
276 | @Override
277 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
278 | if (holder instanceof MyViewHolder) {
279 |
280 | MyViewHolder myHolder = (MyViewHolder) holder;
281 | myHolder.textView.setText(mMyChannelItems.get(position - COUNT_PRE_MY_HEADER).getName());
282 | if (isEditMode) {
283 | myHolder.imgEdit.setVisibility(View.VISIBLE);
284 | } else {
285 | myHolder.imgEdit.setVisibility(View.INVISIBLE);
286 | }
287 |
288 | } else if (holder instanceof OtherViewHolder) {
289 |
290 | ((OtherViewHolder) holder).textView.setText(mOtherChannelItems.get(position - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER).getName());
291 |
292 | } else if (holder instanceof MyChannelHeaderViewHolder) {
293 |
294 | MyChannelHeaderViewHolder headerHolder = (MyChannelHeaderViewHolder) holder;
295 | if (isEditMode) {
296 | headerHolder.tvBtnEdit.setText(R.string.finish);
297 | } else {
298 | headerHolder.tvBtnEdit.setText(R.string.edit);
299 | }
300 | }
301 | }
302 |
303 | @Override
304 | public int getItemCount() {
305 | // 我的频道 标题 + 我的频道.size + 其他频道 标题 + 其他频道.size
306 | return mMyChannelItems.size() + mOtherChannelItems.size() + COUNT_PRE_OTHER_HEADER;
307 | }
308 |
309 | /**
310 | * 开始增删动画
311 | */
312 | private void startAnimation(RecyclerView recyclerView, final View currentView, float targetX, float targetY) {
313 | final ViewGroup viewGroup = (ViewGroup) recyclerView.getParent();
314 | final ImageView mirrorView = addMirrorView(viewGroup, recyclerView, currentView);
315 |
316 | Animation animation = getTranslateAnimator(
317 | targetX - currentView.getLeft(), targetY - currentView.getTop());
318 | currentView.setVisibility(View.INVISIBLE);
319 | mirrorView.startAnimation(animation);
320 |
321 | animation.setAnimationListener(new Animation.AnimationListener() {
322 | @Override
323 | public void onAnimationStart(Animation animation) {
324 | }
325 |
326 | @Override
327 | public void onAnimationEnd(Animation animation) {
328 | viewGroup.removeView(mirrorView);
329 | if (currentView.getVisibility() == View.INVISIBLE) {
330 | currentView.setVisibility(View.VISIBLE);
331 | }
332 | }
333 |
334 | @Override
335 | public void onAnimationRepeat(Animation animation) {
336 |
337 | }
338 | });
339 | }
340 |
341 | /**
342 | * 我的频道 移动到 其他频道
343 | *
344 | * @param myHolder
345 | */
346 | private void moveMyToOther(MyViewHolder myHolder) {
347 | int position = myHolder.getAdapterPosition();
348 |
349 | int startPosition = position - COUNT_PRE_MY_HEADER;
350 | if (startPosition > mMyChannelItems.size() - 1) {
351 | return;
352 | }
353 | ChannelEntity item = mMyChannelItems.get(startPosition);
354 | mMyChannelItems.remove(startPosition);
355 | mOtherChannelItems.add(0, item);
356 |
357 | notifyItemMoved(position, mMyChannelItems.size() + COUNT_PRE_OTHER_HEADER);
358 | }
359 |
360 | /**
361 | * 其他频道 移动到 我的频道
362 | *
363 | * @param otherHolder
364 | */
365 | private void moveOtherToMy(OtherViewHolder otherHolder) {
366 | int position = processItemRemoveAdd(otherHolder);
367 | if (position == -1) {
368 | return;
369 | }
370 | notifyItemMoved(position, mMyChannelItems.size() - 1 + COUNT_PRE_MY_HEADER);
371 | }
372 |
373 | /**
374 | * 其他频道 移动到 我的频道 伴随延迟
375 | *
376 | * @param otherHolder
377 | */
378 | private void moveOtherToMyWithDelay(OtherViewHolder otherHolder) {
379 | final int position = processItemRemoveAdd(otherHolder);
380 | if (position == -1) {
381 | return;
382 | }
383 | delayHandler.postDelayed(new Runnable() {
384 | @Override
385 | public void run() {
386 | notifyItemMoved(position, mMyChannelItems.size() - 1 + COUNT_PRE_MY_HEADER);
387 | }
388 | }, ANIM_TIME);
389 | }
390 |
391 | private Handler delayHandler = new Handler();
392 |
393 | private int processItemRemoveAdd(OtherViewHolder otherHolder) {
394 | int position = otherHolder.getAdapterPosition();
395 |
396 | int startPosition = position - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER;
397 | if (startPosition > mOtherChannelItems.size() - 1) {
398 | return -1;
399 | }
400 | ChannelEntity item = mOtherChannelItems.get(startPosition);
401 | mOtherChannelItems.remove(startPosition);
402 | mMyChannelItems.add(item);
403 | return position;
404 | }
405 |
406 |
407 | /**
408 | * 添加需要移动的 镜像View
409 | */
410 | private ImageView addMirrorView(ViewGroup parent, RecyclerView recyclerView, View view) {
411 | /**
412 | * 我们要获取cache首先要通过setDrawingCacheEnable方法开启cache,然后再调用getDrawingCache方法就可以获得view的cache图片了。
413 | buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。
414 | 若想更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
415 | 当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。
416 | */
417 | view.destroyDrawingCache();
418 | view.setDrawingCacheEnabled(true);
419 | final ImageView mirrorView = new ImageView(recyclerView.getContext());
420 | Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
421 | mirrorView.setImageBitmap(bitmap);
422 | view.setDrawingCacheEnabled(false);
423 | int[] locations = new int[2];
424 | view.getLocationOnScreen(locations);
425 | int[] parenLocations = new int[2];
426 | recyclerView.getLocationOnScreen(parenLocations);
427 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
428 | params.setMargins(locations[0], locations[1] - parenLocations[1], 0, 0);
429 | parent.addView(mirrorView, params);
430 |
431 | return mirrorView;
432 | }
433 |
434 | @Override
435 | public void onItemMove(int fromPosition, int toPosition) {
436 | ChannelEntity item = mMyChannelItems.get(fromPosition - COUNT_PRE_MY_HEADER);
437 | mMyChannelItems.remove(fromPosition - COUNT_PRE_MY_HEADER);
438 | mMyChannelItems.add(toPosition - COUNT_PRE_MY_HEADER, item);
439 | notifyItemMoved(fromPosition, toPosition);
440 | }
441 |
442 | /**
443 | * 开启编辑模式
444 | *
445 | * @param parent
446 | */
447 | private void startEditMode(RecyclerView parent) {
448 | isEditMode = true;
449 |
450 | int visibleChildCount = parent.getChildCount();
451 | for (int i = 0; i < visibleChildCount; i++) {
452 | View view = parent.getChildAt(i);
453 | ImageView imgEdit = (ImageView) view.findViewById(R.id.img_edit);
454 | if (imgEdit != null) {
455 | imgEdit.setVisibility(View.VISIBLE);
456 | }
457 | }
458 | }
459 |
460 | /**
461 | * 完成编辑模式
462 | *
463 | * @param parent
464 | */
465 | private void cancelEditMode(RecyclerView parent) {
466 | isEditMode = false;
467 |
468 | int visibleChildCount = parent.getChildCount();
469 | for (int i = 0; i < visibleChildCount; i++) {
470 | View view = parent.getChildAt(i);
471 | ImageView imgEdit = (ImageView) view.findViewById(R.id.img_edit);
472 | if (imgEdit != null) {
473 | imgEdit.setVisibility(View.INVISIBLE);
474 | }
475 | }
476 | }
477 |
478 | /**
479 | * 获取位移动画
480 | */
481 | private TranslateAnimation getTranslateAnimator(float targetX, float targetY) {
482 | TranslateAnimation translateAnimation = new TranslateAnimation(
483 | Animation.RELATIVE_TO_SELF, 0f,
484 | Animation.ABSOLUTE, targetX,
485 | Animation.RELATIVE_TO_SELF, 0f,
486 | Animation.ABSOLUTE, targetY);
487 | // RecyclerView默认移动动画250ms 这里设置360ms 是为了防止在位移动画结束后 remove(view)过早 导致闪烁
488 | translateAnimation.setDuration(ANIM_TIME);
489 | translateAnimation.setFillAfter(true);
490 | return translateAnimation;
491 | }
492 |
493 | interface OnMyChannelItemClickListener {
494 | void onItemClick(View v, int position);
495 | }
496 |
497 | public void setOnMyChannelItemClickListener(OnMyChannelItemClickListener listener) {
498 | this.mChannelItemClickListener = listener;
499 | }
500 |
501 | /**
502 | * 我的频道
503 | */
504 | class MyViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {
505 | private TextView textView;
506 | private ImageView imgEdit;
507 |
508 | public MyViewHolder(View itemView) {
509 | super(itemView);
510 | textView = (TextView) itemView.findViewById(R.id.tv);
511 | imgEdit = (ImageView) itemView.findViewById(R.id.img_edit);
512 | }
513 |
514 | /**
515 | * item 被选中时
516 | */
517 | @Override
518 | public void onItemSelected() {
519 | textView.setBackgroundResource(R.drawable.bg_channel_p);
520 | }
521 |
522 | /**
523 | * item 取消选中时
524 | */
525 | @Override
526 | public void onItemFinish() {
527 | textView.setBackgroundResource(R.drawable.bg_channel);
528 | }
529 | }
530 |
531 | /**
532 | * 其他频道
533 | */
534 | class OtherViewHolder extends RecyclerView.ViewHolder {
535 | private TextView textView;
536 |
537 | public OtherViewHolder(View itemView) {
538 | super(itemView);
539 | textView = (TextView) itemView.findViewById(R.id.tv);
540 | }
541 | }
542 |
543 | /**
544 | * 我的频道 标题部分
545 | */
546 | class MyChannelHeaderViewHolder extends RecyclerView.ViewHolder {
547 | private TextView tvBtnEdit;
548 |
549 | public MyChannelHeaderViewHolder(View itemView) {
550 | super(itemView);
551 | tvBtnEdit = (TextView) itemView.findViewById(R.id.tv_btn_edit);
552 | }
553 | }
554 | }
555 |
--------------------------------------------------------------------------------