├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── drag_sort │ │ └── dragsortrecyclerview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── drag_sort │ │ │ └── dragsortrecyclerview │ │ │ ├── MainActivity.java │ │ │ └── channel │ │ │ ├── ChannelActivity.java │ │ │ ├── ChannelAdapter.java │ │ │ ├── ChannelBean.java │ │ │ ├── IItemHelper.java │ │ │ ├── ToastUtil.java │ │ │ ├── ToucheCallBcak.java │ │ │ └── ViewHolder.java │ └── res │ │ ├── drawable │ │ ├── bg_btn.xml │ │ ├── bg_channel.xml │ │ ├── bg_channel_n.xml │ │ └── bg_channel_p.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── channel_activity.xml │ │ ├── channel_item.xml │ │ └── channel_title.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ ├── ic_channel_edit.png │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── drag_sort │ └── dragsortrecyclerview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ### DragSortRecyclerView 3 | 仿新闻类app频道切换页面,支持拖拽排序,交换频道等 4 | 5 | ### 效果图 6 | 7 | ![recycler_swip5](https://user-images.githubusercontent.com/27534854/29954760-c2e8be8e-8f0c-11e7-8f6d-5cd398bff5d8.gif) 8 | 9 | ### 博客地址: 10 | https://livesun.github.io/2017/09/01/RecyclerThreeStep/ 11 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "drag_sort.dragsortrecyclerview" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support:recyclerview-v7:25+' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /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 D:\as_sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/drag_sort/dragsortrecyclerview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("drag_sort.dragsortrecyclerview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import drag_sort.dragsortrecyclerview.channel.ChannelActivity; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | findViewById(R.id.click).setOnClickListener(new View.OnClickListener() { 17 | @Override 18 | public void onClick(View v) { 19 | startActivity(new Intent(MainActivity.this, ChannelActivity.class)); 20 | } 21 | }); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ChannelActivity.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.GridLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.support.v7.widget.helper.ItemTouchHelper; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import drag_sort.dragsortrecyclerview.R; 14 | 15 | 16 | /** 17 | * 类描述: 18 | * 创建人:livesun 19 | * 创建时间:2017/8/31 20 | * 修改人: 21 | * 修改时间: 22 | * github:https://github.com/livesun 23 | */ 24 | 25 | public class ChannelActivity extends AppCompatActivity implements ChannelAdapter.OnStartDragListener { 26 | 27 | private RecyclerView channelRecyler; 28 | private ChannelAdapter mAdapter; 29 | private ItemTouchHelper mTouchHelper; 30 | 31 | @Override 32 | protected void onCreate(@Nullable Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.channel_activity); 35 | channelRecyler = (RecyclerView) findViewById(R.id.channl_recyler); 36 | mAdapter = new ChannelAdapter(this,getDatas(),channelRecyler); 37 | GridLayoutManager manager=new GridLayoutManager(this,4); 38 | manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 39 | @Override 40 | public int getSpanSize(int position) { 41 | int itemViewType = mAdapter.getItemViewType(position); 42 | if(itemViewType==mAdapter.CHANNEL_TITLE){ 43 | return 4; 44 | } 45 | return 1; 46 | } 47 | }); 48 | channelRecyler.setLayoutManager(manager); 49 | channelRecyler.setAdapter(mAdapter); 50 | //创建ItemTouchHelper 51 | mTouchHelper = new ItemTouchHelper(new ToucheCallBcak(mAdapter)); 52 | //attach到RecyclerView中 53 | mTouchHelper.attachToRecyclerView(channelRecyler); 54 | mAdapter.setOnStartDragListener(this); 55 | } 56 | 57 | private List getDatas(){ 58 | List beanList=new ArrayList<>(); 59 | beanList.add(new ChannelBean("我的频道",1)); 60 | 61 | for(int i=0;i<10;i++){ 62 | beanList.add(new ChannelBean("频道"+i,2)); 63 | } 64 | 65 | beanList.add(new ChannelBean("其他频道",1)); 66 | 67 | for(int i=0;i<10;i++){ 68 | beanList.add(new ChannelBean("其他"+i,3)); 69 | } 70 | 71 | return beanList; 72 | 73 | } 74 | 75 | @Override 76 | public void startDrag(RecyclerView.ViewHolder holder) { 77 | mTouchHelper.startDrag(holder); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ChannelAdapter.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import drag_sort.dragsortrecyclerview.R; 16 | 17 | 18 | /** 19 | * 类描述: 20 | * 创建人:livesun 21 | * 创建时间:2017/8/31 22 | * 修改人: 23 | * 修改时间: 24 | * github:https://github.com/livesun 25 | */ 26 | 27 | public class ChannelAdapter extends RecyclerView.Adapter implements IItemHelper { 28 | //标题 29 | public static final int CHANNEL_TITLE=1; 30 | //我的频道 31 | public static final int MY_CHANNEL=2; 32 | //其他频道 33 | public static final int OTHER_CHANNEL=3; 34 | private final Context mContext; 35 | private List mDatas=new ArrayList<>(); 36 | //我的频道集合。 37 | List mMyChannel=new ArrayList<>(); 38 | //其他频道集合 39 | List mOtherChannel=new ArrayList<>(); 40 | 41 | private final LayoutInflater mInflater; 42 | private RecyclerView mRecyclerView; 43 | //是否编辑模式 44 | private boolean isEdit; 45 | 46 | public ChannelAdapter(Context context, List datas,RecyclerView recyclerView){ 47 | this.mContext = context; 48 | this.mDatas = datas; 49 | mInflater = LayoutInflater.from(context); 50 | this.mRecyclerView = recyclerView; 51 | for(int i=0;imMyChannel.size()+1){ 200 | int i = position - 2 - mMyChannel.size(); 201 | //其他 202 | ChannelBean item = mOtherChannel.get(position-2-mMyChannel.size()); 203 | mOtherChannel.remove(item); 204 | item.setTypeView(2); 205 | mMyChannel.add(item); 206 | //这里实现的不择手段了,,之后再改改 207 | notifyData(); 208 | notifyItemMoved(position, mMyChannel.size()); 209 | 210 | }else if(position>0&&position<=mMyChannel.size()){ 211 | //我的 212 | ChannelBean item = mMyChannel.get(position-1); 213 | mMyChannel.remove(item); 214 | item.setTypeView(3); 215 | mOtherChannel.add(0, item); 216 | 217 | notifyData(); 218 | notifyItemMoved(position, mMyChannel.size() + 2); 219 | } 220 | } 221 | 222 | 223 | @Override 224 | public void itemMoved(int oldPosition, int newPosition) { 225 | ChannelBean channelBean = mMyChannel.get(oldPosition - 1); 226 | mMyChannel.remove(oldPosition - 1); 227 | mMyChannel.add(newPosition-1,channelBean); 228 | notifyData(); 229 | notifyItemMoved(oldPosition, newPosition); 230 | } 231 | 232 | private void notifyData(){ 233 | //这里实现的不择手段了,,之后再改改 234 | mDatas.clear(); 235 | mDatas.add(new ChannelBean("我的频道",1)); 236 | mDatas.addAll(mMyChannel); 237 | mDatas.add(new ChannelBean("其他频道",1)); 238 | mDatas.addAll(mOtherChannel); 239 | } 240 | 241 | @Override 242 | public void itemDismiss(int position) { 243 | } 244 | 245 | private OnStartDragListener onStartDragListener; 246 | 247 | public interface OnStartDragListener{ 248 | void startDrag(RecyclerView.ViewHolder holder); 249 | } 250 | 251 | public void setOnStartDragListener(OnStartDragListener onStartDragListener){ 252 | 253 | this.onStartDragListener = onStartDragListener; 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ChannelBean.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | /** 4 | * 类描述: 5 | * 创建人:livesun 6 | * 创建时间:2017/8/31 7 | * 修改人: 8 | * 修改时间: 9 | * github:https://github.com/livesun 10 | */ 11 | 12 | public class ChannelBean { 13 | 14 | String src; 15 | int typeView; 16 | 17 | public ChannelBean(String src, int typeView) { 18 | this.src = src; 19 | this.typeView = typeView; 20 | } 21 | 22 | public String getSrc() { 23 | return src; 24 | } 25 | 26 | public void setSrc(String src) { 27 | this.src = src; 28 | } 29 | 30 | public int getTypeView() { 31 | return typeView; 32 | } 33 | 34 | public void setTypeView(int typeView) { 35 | this.typeView = typeView; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/IItemHelper.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | /** 4 | * Created by 29028 on 2017/8/19. 5 | */ 6 | 7 | public interface IItemHelper { 8 | 9 | void itemMoved(int oldPosition, int newPosition); 10 | void itemDismiss(int position); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | 7 | /** 8 | * Created by ChenWentan on 2016/8/23. 9 | */ 10 | public class ToastUtil { 11 | private static Toast toast; 12 | 13 | /** 14 | * 默认短时间显示 Toast 15 | * 16 | * @param sequence 17 | */ 18 | public static void showShort(Context context,CharSequence sequence) { 19 | 20 | if (toast == null) { 21 | toast = Toast.makeText(context, sequence, Toast.LENGTH_SHORT); 22 | 23 | } else { 24 | toast.setText(sequence); 25 | } 26 | toast.show(); 27 | 28 | } 29 | 30 | /** 31 | * 短时间显示Toast 32 | * 33 | * @param context 34 | * @param message 35 | */ 36 | public static void showShort(Context context, int message) { 37 | if (null == toast) { 38 | toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); 39 | // toast.setGravity(Gravity.CENTER, 0, 0); 40 | } else { 41 | toast.setText(message); 42 | } 43 | toast.show(); 44 | } 45 | 46 | /** 47 | * 长时间显示Toast 48 | * 49 | * @param context 50 | * @param message 51 | */ 52 | public static void showLong(Context context, CharSequence message) { 53 | if (null == toast) { 54 | toast = Toast.makeText(context, message, Toast.LENGTH_LONG); 55 | // toast.setGravity(Gravity.CENTER, 0, 0); 56 | } else { 57 | toast.setText(message); 58 | } 59 | toast.show(); 60 | } 61 | 62 | /** 63 | * 长时间显示Toast 64 | * 65 | * @param context 66 | * @param message 67 | */ 68 | public static void showLong(Context context, int message) { 69 | if (null == toast) { 70 | toast = Toast.makeText(context, message, Toast.LENGTH_LONG); 71 | // toast.setGravity(Gravity.CENTER, 0, 0); 72 | } else { 73 | toast.setText(message); 74 | } 75 | toast.show(); 76 | } 77 | 78 | /** 79 | * 自定义显示时间 80 | * 81 | * @param context 82 | * @param sequence 83 | * @param duration 84 | */ 85 | public static void show(Context context, CharSequence sequence, int duration) { 86 | if (toast == null) { 87 | toast = Toast.makeText(context, sequence, duration); 88 | } else { 89 | toast.setText(sequence); 90 | } 91 | toast.show(); 92 | 93 | } 94 | 95 | /** 96 | * 隐藏toast 97 | */ 98 | public static void hideToast() { 99 | if (toast != null) { 100 | toast.cancel(); 101 | } 102 | } 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ToucheCallBcak.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | import android.graphics.Canvas; 4 | import android.support.v7.widget.GridLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.support.v7.widget.StaggeredGridLayoutManager; 7 | import android.support.v7.widget.helper.ItemTouchHelper; 8 | 9 | /** 10 | * Created by 29028 on 2017/8/19. 11 | */ 12 | 13 | public class ToucheCallBcak extends ItemTouchHelper.Callback { 14 | 15 | private IItemHelper itemHelper; 16 | 17 | public ToucheCallBcak(IItemHelper itemHelper){ 18 | 19 | this.itemHelper = itemHelper; 20 | } 21 | 22 | //声明不同转台下的移动方向 23 | @Override 24 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 25 | int dragFlags; 26 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); 27 | if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager) { 28 | dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 29 | } else { 30 | dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 31 | } 32 | // 如果想支持滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END 33 | int swipeFlags = 0; 34 | return makeMovementFlags(dragFlags, swipeFlags); 35 | } 36 | 37 | // 拖动的条目从旧位置--到新位置时调用 38 | @Override 39 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 40 | // 不同Type之间不可移动 41 | if (viewHolder.getItemViewType() != target.getItemViewType()) { 42 | return false; 43 | } 44 | itemHelper.itemMoved(viewHolder.getLayoutPosition(),target.getLayoutPosition()); 45 | return false; 46 | } 47 | 48 | // 滑动到消失调用 49 | @Override 50 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 51 | itemHelper.itemDismiss(viewHolder.getLayoutPosition()); 52 | } 53 | 54 | /** 55 | * true --开启长按 56 | * false --关闭长按拖动 57 | * @return 58 | */ 59 | @Override 60 | public boolean isLongPressDragEnabled() { 61 | return false; 62 | } 63 | 64 | /** 65 | * 关闭侧滑 66 | * @return 67 | */ 68 | @Override 69 | public boolean isItemViewSwipeEnabled() { 70 | 71 | return false; 72 | } 73 | //状态改变时调用 比如正在滑动,正在拖动 74 | @Override 75 | public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { 76 | super.onSelectedChanged(viewHolder, actionState); 77 | //不是空闲状态--背景加深 78 | // if(actionState!= ItemTouchHelper.ACTION_STATE_IDLE){ 79 | // viewHolder.itemView.setBackgroundColor(Color.GRAY); 80 | // } 81 | } 82 | 83 | //滑动完,拖动完调用 84 | @Override 85 | public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 86 | super.clearView(recyclerView, viewHolder); 87 | // viewHolder.itemView.setBackgroundColor(0); 88 | } 89 | 90 | //重写这个方法修改你的UI响应 91 | @Override 92 | public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { 93 | super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); 94 | //正在拖动 95 | if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { 96 | /* final float alpha = 80 - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); 97 | viewHolder.itemView.setAlpha(alpha); 98 | viewHolder.itemView.setTranslationX(dX);*/ 99 | 100 | }else if(actionState==ItemTouchHelper.ACTION_STATE_IDLE){ 101 | //正在滑动 102 | }else if(actionState==ItemTouchHelper.ACTION_STATE_DRAG){ 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/drag_sort/dragsortrecyclerview/channel/ViewHolder.java: -------------------------------------------------------------------------------- 1 | package drag_sort.dragsortrecyclerview.channel; 2 | 3 | import android.support.annotation.DrawableRes; 4 | import android.support.annotation.IdRes; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.text.TextUtils; 7 | import android.util.SparseArray; 8 | import android.view.View; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | /** 13 | * 类描述:ViewHolder 14 | * 创建人:livesun 15 | * 创建时间:2017/8/25 16 | * 修改人: 17 | * 修改时间: 18 | * github:https://github.com/livesun 19 | */ 20 | public class ViewHolder extends RecyclerView.ViewHolder { 21 | private SparseArray mViews; 22 | public ViewHolder(View itemView) { 23 | super(itemView); 24 | mViews=new SparseArray(); 25 | } 26 | 27 | /** 28 | * 获取View方法 29 | * @param id 30 | * @param 31 | * @return 32 | */ 33 | public T getView(@IdRes int id){ 34 | View view= mViews.get(id); 35 | 36 | if(view==null){ 37 | view =itemView.findViewById(id); 38 | mViews.put(id,view); 39 | } 40 | 41 | return (T) view; 42 | } 43 | 44 | /** 45 | * 设置文本 46 | */ 47 | public ViewHolder setText(@IdRes int id, CharSequence text){ 48 | TextView textView = getView(id); 49 | if(TextUtils.isEmpty(text)){ 50 | textView.setText(""); 51 | }else{ 52 | textView.setText(text); 53 | } 54 | 55 | return this; 56 | } 57 | 58 | 59 | 60 | /** 61 | * 设置图片 62 | */ 63 | public ViewHolder setImageResource(@IdRes int id, @DrawableRes int resId){ 64 | ImageView view = getView(id); 65 | view.setImageResource(resId); 66 | return this; 67 | } 68 | 69 | /** 70 | * 设置网络图片 71 | */ 72 | 73 | public ViewHolder setImage(@IdRes int id, ImageLoader loader){ 74 | ImageView view = getView(id); 75 | loader.loadImage(view,loader.mPath); 76 | return this; 77 | } 78 | 79 | /** 80 | * 解耦图片处理 81 | */ 82 | public abstract static class ImageLoader{ 83 | String mPath; 84 | public ImageLoader(String path){ 85 | this.mPath=path; 86 | } 87 | 88 | protected abstract void loadImage(ImageView imageView,String path); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_btn.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_channel.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 |