Skip to content

Commit

Permalink
add list style
Browse files Browse the repository at this point in the history
  • Loading branch information
bocq committed Jan 4, 2017
1 parent 749f80b commit c07e8e8
Show file tree
Hide file tree
Showing 32 changed files with 589 additions and 323 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.cyan.dragtab;
package cn.cyan.sample;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
Expand Down
5 changes: 3 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.cyan.dragtab">
package="cn.cyan.sample">

<application
android:name=".base.SampleApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name="cn.cyan.sample.base.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
76 changes: 0 additions & 76 deletions sample/src/main/java/cn/cyan/dragtab/MainActivity.java

This file was deleted.

52 changes: 0 additions & 52 deletions sample/src/main/java/cn/cyan/dragtab/tab/FindIconHelper.java

This file was deleted.

29 changes: 0 additions & 29 deletions sample/src/main/java/cn/cyan/dragtab/tab/Tab.java

This file was deleted.

Loading

0 comments on commit c07e8e8

Please sign in to comment.