-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
589 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,13 @@ | |
import android.view.animation.ScaleAnimation; | ||
|
||
/** | ||
* Desc : 可拖动recyclerView | ||
* User : Cyan([email protected]) | ||
* New : 2016/9/21 14:27 | ||
* User : Cyan([email protected]) | ||
* Date : 2016/9/28 | ||
*/ | ||
public class DragRecyclerView extends RecyclerView { | ||
|
||
/* 适配器 */ | ||
private OnItemChangeListener adapter; | ||
/* 是否可拖动 */ | ||
private boolean dragEnable; | ||
/* 是否显示拖动动画 */ | ||
private boolean showDragAnimation; | ||
|
||
|
||
|
@@ -50,9 +46,6 @@ private void init(Context context, AttributeSet attrs) { | |
} | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// 拖动监听 | ||
/////////////////////////////////////////////////////////////////////////// | ||
ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { | ||
@Override | ||
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { | ||
|
@@ -71,9 +64,7 @@ public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder v | |
|
||
@Override | ||
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | ||
// 原位置 | ||
int oldPosition = viewHolder.getAdapterPosition(); | ||
// 目标位置 | ||
int targetPosition = target.getAdapterPosition(); | ||
adapter.onItemMoved(oldPosition, targetPosition); | ||
return false; | ||
|
@@ -91,14 +82,12 @@ public boolean isItemViewSwipeEnabled() { | |
|
||
@Override | ||
public boolean isLongPressDragEnabled() { | ||
// 禁用,使用自定义触摸监听 | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { | ||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { | ||
//滑动时改变Item的透明度 | ||
final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); | ||
viewHolder.itemView.setAlpha(alpha); | ||
viewHolder.itemView.setTranslationX(dX); | ||
|
@@ -110,10 +99,7 @@ public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHold | |
@Override | ||
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { | ||
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { | ||
/** item已经被拽起(托起状态) */ | ||
// 设置背景 | ||
viewHolder.itemView.setBackgroundColor(Color.LTGRAY); | ||
// 放大动画 | ||
if (showDragAnimation) zoomView(viewHolder.itemView); | ||
} | ||
super.onSelectedChanged(viewHolder, actionState); | ||
|
@@ -122,68 +108,45 @@ public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionStat | |
@Override | ||
public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) { | ||
super.clearView(recyclerView, viewHolder); | ||
// 还原item的样式 | ||
viewHolder.itemView.setAlpha(1.0f); | ||
viewHolder.itemView.setBackgroundColor(Color.WHITE); | ||
// 缩放动画 | ||
if (showDragAnimation) revertView(viewHolder.itemView); | ||
} | ||
|
||
@Override | ||
public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, ViewHolder target) { | ||
/** 被禁用的位置不会被挤兑 */ | ||
return adapter.onItemDrag(target.getAdapterPosition()); | ||
return adapter.onItemDrop(target.getAdapterPosition()); | ||
} | ||
}); | ||
|
||
/* 放大动画 */ | ||
private ScaleAnimation zoomAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); | ||
/* 还原动画 */ | ||
private ScaleAnimation revertAnimation = new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); | ||
|
||
/** | ||
* 放大 | ||
* | ||
* @param v 视图 | ||
*/ | ||
private void zoomView(final View v) { | ||
v.setAnimation(zoomAnimation); | ||
// 动画执行完停留位置 | ||
zoomAnimation.setFillAfter(true); | ||
// 动画持续时间 | ||
zoomAnimation.setDuration(200); | ||
// 开始 | ||
zoomAnimation.start(); | ||
} | ||
|
||
/** | ||
* 还原 | ||
* | ||
* @param v 视图 | ||
*/ | ||
private void revertView(final View v) { | ||
v.setAnimation(revertAnimation); | ||
// 动画执行完停留位置 | ||
revertAnimation.setFillAfter(true); | ||
// 动画持续时间 | ||
revertAnimation.setDuration(400); | ||
// 执行监听 | ||
revertAnimation.setAnimationListener(new Animation.AnimationListener() { | ||
@Override | ||
public void onAnimationStart(Animation animation) { | ||
} | ||
|
||
@Override | ||
public void onAnimationEnd(Animation animation) { | ||
/** 动画结束后清除效果 */ | ||
v.clearAnimation(); | ||
} | ||
|
||
@Override | ||
public void onAnimationRepeat(Animation animation) { | ||
} | ||
}); | ||
// 开始 | ||
revertAnimation.start(); | ||
} | ||
|
||
|
@@ -197,17 +160,19 @@ public DragRecyclerView showDragAnimation(boolean showDragAnimation) { | |
return this; | ||
} | ||
|
||
|
||
public DragRecyclerView setDragAdapter(OnItemChangeListener dragBaseAdapter) { | ||
if (dragBaseAdapter instanceof Adapter) { | ||
this.adapter = dragBaseAdapter; | ||
touchHelper.attachToRecyclerView(this); | ||
setAdapter((Adapter) adapter); | ||
super.setAdapter((Adapter) adapter); | ||
} else { | ||
throw new IllegalArgumentException(); | ||
} | ||
return this; | ||
} | ||
|
||
|
||
public DragRecyclerView bindEvent(HoldTouchHelper.OnItemTouchEvent onItemTouchEvent) { | ||
HoldTouchHelper.bind(this, onItemTouchEvent); | ||
return this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,9 @@ | |
import android.view.View; | ||
|
||
/** | ||
* Desc : 拦截 recyclerView 单击/长按事件 | ||
* User : Cyan([email protected]) | ||
* New : 2016/11/21 13:09 | ||
* User : Cyan([email protected]) | ||
* Date : 2016/9/28 | ||
*/ | ||
|
||
public class HoldTouchHelper { | ||
|
||
private RecyclerView recyclerView; | ||
|
13 changes: 8 additions & 5 deletions
13
dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package cn.cyan.dragrecyclerview; | ||
|
||
|
||
/** | ||
* Desc : item改变 | ||
* User : Cyan([email protected]) | ||
* New : 2016/9/28 8:41 | ||
* User : Cyan([email protected]) | ||
* Date : 2016/9/28 | ||
*/ | ||
public interface OnItemChangeListener { | ||
|
||
/* 是否可拖动 */ | ||
/* item can be dragged */ | ||
boolean onItemDrag(int position); | ||
|
||
/* item移动 */ | ||
/* item moved */ | ||
void onItemMoved(int form, int target); | ||
|
||
/* item can be dropped */ | ||
boolean onItemDrop(int position); | ||
} |
2 changes: 1 addition & 1 deletion
2
...cyan/dragtab/ExampleInstrumentedTest.java → .../cyan/sample/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
sample/src/main/java/cn/cyan/dragtab/tab/FindIconHelper.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.