Skip to content

Commit 9316bdd

Browse files
committed
更新拖拽处理,更新例子
1 parent c3b0928 commit 9316bdd

File tree

10 files changed

+261
-34
lines changed

10 files changed

+261
-34
lines changed

dragsloplayout/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 14
1111
targetSdkVersion 23
12-
versionCode 2
13-
versionName "1.0.2"
12+
versionCode 3
13+
versionName "1.0.3"
1414
renderscriptTargetApi 23
1515
renderscriptSupportModeEnabled true
1616
}

dragsloplayout/src/main/java/com/dl7/drag/DragSlopLayout.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,23 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
186186
@Override
187187
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
188188
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
189-
190-
if (mMaxHeight == 0) {
191-
// 未设置最大高度则为布局高度的 2/3
192-
mMaxHeight = getMeasuredHeight() * 2 / 3;
193-
}
194-
View childView = getChildAt(2);
195-
MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
196-
int childWidth = childView.getMeasuredWidth();
197-
int childHeight = childView.getMeasuredHeight();
198-
// 限定视图的最大高度
199-
if (childHeight > mMaxHeight) {
200-
mMaxHeight = childHeight;
201-
MeasureSpec.makeMeasureSpec(childWidth - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
202-
childView.measure(MeasureSpec.makeMeasureSpec(childWidth - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY),
203-
MeasureSpec.makeMeasureSpec(mMaxHeight - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY));
189+
if (mMode == MODE_DRAG) {
190+
if (mMaxHeight == 0) {
191+
// 未设置最大高度则为布局高度的 2/3
192+
mMaxHeight = getMeasuredHeight() * 2 / 3;
193+
} else if (mMaxHeight > getMeasuredHeight()) {
194+
// MODE_DRAG 模式最大高度不超过布局高度
195+
mMaxHeight = getMeasuredHeight();
196+
}
197+
View childView = getChildAt(2);
198+
MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
199+
int childWidth = childView.getMeasuredWidth();
200+
int childHeight = childView.getMeasuredHeight();
201+
// 限定视图的最大高度
202+
if (childHeight > mMaxHeight) {
203+
childView.measure(MeasureSpec.makeMeasureSpec(childWidth - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY),
204+
MeasureSpec.makeMeasureSpec(mMaxHeight - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY));
205+
}
204206
}
205207
}
206208

@@ -223,7 +225,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
223225
mExpandedTop = b - childHeight;
224226
mCollapsedTop = b - mFixHeight;
225227
// 如果本身 mDragViewTop 已经有值,则直接使用,不然会出现突然闪一下的情况
226-
if (mDragViewTop == 0) {
228+
if (mDragViewTop == 0 || mMode != MODE_DRAG_OUTSIDE) {
227229
if (mDragStatus == STATUS_EXIT) {
228230
// 对于 ViewPager 换页后会回调 onLayout(),需要进行处理
229231
if (mMode == MODE_DRAG || mMode == MODE_DRAG_OUTSIDE) {
@@ -326,7 +328,6 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
326328

327329
@Override
328330
public boolean tryCaptureView(View child, int pointerId) {
329-
requestDisallowInterceptTouchEvent(true);
330331
mIsDrag = child == mDragView;
331332
return mIsDrag;
332333
}
@@ -363,7 +364,6 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {
363364
ViewCompat.postInvalidateOnAnimation(DragSlopLayout.this);
364365
}
365366
}
366-
requestDisallowInterceptTouchEvent(false);
367367
}
368368

369369
@Override

simple/src/main/java/com/dl7/simple/drag/activity/DragOutsideActivity.java

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.dl7.simple.drag.activity;
22

3+
import android.animation.Animator;
34
import android.os.Bundle;
5+
import android.support.v4.view.ViewCompat;
46
import android.support.v4.view.ViewPager;
57
import android.support.v7.widget.RecyclerView;
68
import android.support.v7.widget.Toolbar;
@@ -13,6 +15,9 @@
1315
import com.dl7.drag.DragSlopLayout;
1416
import com.dl7.simple.drag.R;
1517
import com.dl7.simple.drag.adapter.PhotoPagerAdapter;
18+
import com.dl7.simple.drag.adapter.ThumbAdapter;
19+
import com.dl7.simple.drag.utils.AnimateHelper;
20+
import com.dl7.simple.drag.utils.RecyclerViewHelper;
1621

1722
import java.util.ArrayList;
1823
import java.util.List;
@@ -23,7 +28,6 @@
2328

2429
public class DragOutsideActivity extends BaseActivity {
2530

26-
2731
@BindView(R.id.vp_photo)
2832
ViewPager mVpPhoto;
2933
@BindView(R.id.rv_relate_list)
@@ -44,25 +48,88 @@ public class DragOutsideActivity extends BaseActivity {
4448
DragSlopLayout mDragLayout;
4549

4650
private boolean mIsInteract = true;
51+
private ThumbAdapter mAdapter;
52+
private boolean mIsHideToolbar = false; // 是否隐藏 Toolbar
53+
private Animator mToolBarAnimator;
54+
private Animator mBottomBarAnimator;
4755

4856
@Override
4957
protected void onCreate(Bundle savedInstanceState) {
5058
super.onCreate(savedInstanceState);
5159
setContentView(R.layout.activity_drag_outside);
5260
ButterKnife.bind(this);
5361
initToolBar(mToolBar, true, "Drag Outside Mode");
54-
62+
// 设置 ViewPager
5563
List<Integer> imgList = new ArrayList<>();
56-
imgList.add(R.mipmap.img1);
57-
imgList.add(R.mipmap.img2);
58-
imgList.add(R.mipmap.img3);
59-
imgList.add(R.mipmap.img4);
60-
imgList.add(R.mipmap.img5);
61-
62-
PhotoPagerAdapter mPagerAdapter = new PhotoPagerAdapter(this, imgList, false);
64+
imgList.add(R.mipmap.pic1);
65+
imgList.add(R.mipmap.pic2);
66+
imgList.add(R.mipmap.pic3);
67+
imgList.add(R.mipmap.pic4);
68+
imgList.add(R.mipmap.pic5);
69+
PhotoPagerAdapter mPagerAdapter = new PhotoPagerAdapter(this, imgList, true);
6370
mVpPhoto.setAdapter(mPagerAdapter);
71+
mPagerAdapter.setListener(new PhotoPagerAdapter.OnPhotoClickListener() {
72+
@Override
73+
public void onPhotoClick() {
74+
mIsHideToolbar = !mIsHideToolbar;
75+
if (mIsHideToolbar) {
76+
AnimateHelper.stopAnimator(mToolBarAnimator);
77+
mToolBarAnimator = AnimateHelper.doMoveVertical(mToolBar, (int) mToolBar.getTranslationY(),
78+
-mToolBar.getBottom(), 300);
79+
if (mBottomBar.getTranslationY() != mBottomBar.getHeight()) {
80+
AnimateHelper.stopAnimator(mBottomBarAnimator);
81+
mBottomBarAnimator = AnimateHelper.doMoveVertical(mBottomBar, (int) mBottomBar.getTranslationY(),
82+
mBottomBar.getHeight(), 300);
83+
}
84+
} else {
85+
AnimateHelper.stopAnimator(mToolBarAnimator);
86+
mToolBarAnimator = AnimateHelper.doMoveVertical(mToolBar, (int) mToolBar.getTranslationY(),
87+
0, 300);
88+
ViewCompat.animate(mToolBar).translationY(0).setDuration(300).start();
89+
if (mBottomBar.getTranslationY() != 0) {
90+
AnimateHelper.stopAnimator(mBottomBarAnimator);
91+
mBottomBarAnimator = AnimateHelper.doMoveVertical(mBottomBar, (int) mBottomBar.getTranslationY(),
92+
0, 300);
93+
}
94+
}
95+
}
96+
});
97+
// 设置 RecyclerView
98+
List<Integer> thumbList = new ArrayList<>();
99+
thumbList.add(R.mipmap.pic1);
100+
thumbList.add(R.mipmap.pic2);
101+
thumbList.add(R.mipmap.pic3);
102+
thumbList.add(R.mipmap.pic4);
103+
thumbList.add(R.mipmap.pic5);
104+
thumbList.add(R.mipmap.pic1);
105+
thumbList.add(R.mipmap.pic2);
106+
thumbList.add(R.mipmap.pic3);
107+
thumbList.add(R.mipmap.pic4);
108+
thumbList.add(R.mipmap.pic5);
109+
thumbList.add(R.mipmap.pic1);
110+
thumbList.add(R.mipmap.pic2);
111+
thumbList.add(R.mipmap.pic3);
112+
thumbList.add(R.mipmap.pic4);
113+
thumbList.add(R.mipmap.pic5);
114+
mAdapter = new ThumbAdapter(this, thumbList);
115+
RecyclerViewHelper.initRecyclerViewH(this, mRvRelateList, mAdapter);
64116
// 和 ViewPager 联动
65117
mDragLayout.interactWithViewPager(mIsInteract);
118+
mDragLayout.setDragPositionListener(new DragSlopLayout.OnDragPositionListener() {
119+
@Override
120+
public void onDragPosition(int visibleHeight, float percent, boolean isUp) {
121+
if (AnimateHelper.isRunning(mBottomBarAnimator) || mIsHideToolbar) {
122+
return;
123+
}
124+
if (isUp && mBottomBar.getTranslationY() != mBottomBar.getHeight()) {
125+
mBottomBarAnimator = AnimateHelper.doMoveVertical(mBottomBar, (int) mBottomBar.getTranslationY(),
126+
mBottomBar.getHeight(), 300);
127+
} else if (!isUp && mBottomBar.getTranslationY() != 0) {
128+
mBottomBarAnimator = AnimateHelper.doMoveVertical(mBottomBar, (int) mBottomBar.getTranslationY(),
129+
0, 300);
130+
}
131+
}
132+
});
66133
}
67134

68135
@Override
@@ -83,6 +150,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
83150
return super.onOptionsItemSelected(item);
84151
}
85152

153+
@Override
154+
protected void onDestroy() {
155+
super.onDestroy();
156+
AnimateHelper.deleteAnimator(mToolBarAnimator);
157+
AnimateHelper.deleteAnimator(mBottomBarAnimator);
158+
}
159+
86160
@OnClick({R.id.iv_favorite, R.id.iv_download, R.id.iv_praise, R.id.iv_share})
87161
public void onClick(View view) {
88162
switch (view.getId()) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.dl7.simple.drag.adapter;
2+
3+
import android.content.Context;
4+
import android.support.v7.widget.RecyclerView;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ImageView;
9+
10+
import com.dl7.simple.drag.R;
11+
12+
import java.util.List;
13+
14+
/**
15+
* Created by long on 2017/1/6.
16+
*/
17+
public class ThumbAdapter extends BaseRecyclerAdapter<Integer> {
18+
19+
public ThumbAdapter(Context context) {
20+
super(context);
21+
}
22+
23+
public ThumbAdapter(Context context, List<Integer> datas) {
24+
super(context, datas);
25+
}
26+
27+
@Override
28+
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
29+
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_thumb, parent, false);
30+
ViewHolder viewHolder = new ViewHolder(view);
31+
return viewHolder;
32+
}
33+
34+
@Override
35+
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
36+
((ViewHolder)holder).ivPhoto.setImageResource(mDatas.get(position));
37+
}
38+
39+
public static class ViewHolder extends RecyclerView.ViewHolder{
40+
public ImageView ivPhoto;
41+
42+
public ViewHolder(View rootView) {
43+
super(rootView);
44+
this.ivPhoto = (ImageView) rootView.findViewById(R.id.iv_photo);
45+
}
46+
47+
}
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.dl7.simple.drag.utils;
2+
3+
import android.animation.Animator;
4+
import android.animation.ObjectAnimator;
5+
import android.view.View;
6+
7+
/**
8+
* Created by long on 2017/1/5.
9+
*/
10+
11+
public final class AnimateHelper {
12+
13+
private AnimateHelper() {
14+
throw new AssertionError();
15+
}
16+
17+
18+
/**
19+
* 垂直偏移动画
20+
* @param view
21+
* @param startY
22+
* @param endY
23+
* @param duration
24+
* @return
25+
*/
26+
public static Animator doMoveVertical(View view, int startY, int endY, int duration) {
27+
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", startY, endY).setDuration(duration);
28+
animator.start();
29+
return animator;
30+
}
31+
32+
33+
/**
34+
* 动画是否在运行
35+
* @param animator
36+
*/
37+
public static boolean isRunning(Animator animator) {
38+
return animator != null && animator.isRunning();
39+
}
40+
41+
/**
42+
* 启动动画
43+
* @param animator
44+
*/
45+
public static void startAnimator(Animator animator) {
46+
if (animator != null && !animator.isRunning()) {
47+
animator.start();
48+
}
49+
}
50+
51+
/**
52+
* 停止动画
53+
* @param animator
54+
*/
55+
public static void stopAnimator(Animator animator) {
56+
if (animator != null && animator.isRunning()) {
57+
animator.cancel();
58+
}
59+
}
60+
61+
/**
62+
* 删除动画
63+
* @param animator
64+
*/
65+
public static void deleteAnimator(Animator animator) {
66+
if (animator != null && animator.isRunning()) {
67+
animator.cancel();
68+
}
69+
animator = null;
70+
}
71+
}

simple/src/main/java/com/dl7/simple/drag/adapter/RecyclerViewHelper.java renamed to simple/src/main/java/com/dl7/simple/drag/utils/RecyclerViewHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dl7.simple.drag.adapter;
1+
package com.dl7.simple.drag.utils;
22

33
import android.content.Context;
44
import android.support.v7.widget.DefaultItemAnimator;
@@ -7,6 +7,9 @@
77
import android.support.v7.widget.RecyclerView;
88
import android.support.v7.widget.StaggeredGridLayoutManager;
99

10+
import com.dl7.simple.drag.adapter.DividerGridItemDecoration;
11+
import com.dl7.simple.drag.adapter.DividerItemDecoration;
12+
1013

1114
/**
1215
* Created by long on 2016/3/30.

0 commit comments

Comments
 (0)