Skip to content

Commit

Permalink
更新拖拽的处理,增加拖拽监听
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukey7 committed Jan 5, 2017
1 parent 7832318 commit b89f431
Show file tree
Hide file tree
Showing 11 changed files with 647 additions and 120 deletions.
111 changes: 89 additions & 22 deletions dragsloplayout/src/main/java/com/dl7/drag/DragSlopLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
Expand All @@ -25,6 +26,7 @@
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.BounceInterpolator;
Expand Down Expand Up @@ -115,6 +117,8 @@ public class DragSlopLayout extends FrameLayout {
private View mAttachScrollView;
// 手势控制
private GestureDetector mGestureDetector;
// DragView的Top属性值
private int mDragViewTop = 0;


public DragSlopLayout(Context context) {
Expand All @@ -136,6 +140,7 @@ private void _init(Context context, AttributeSet attrs) {
mGestureDetector = new GestureDetector(context, mGestureListener);
mFallBoundScroller = ScrollerCompat.create(context, new BounceInterpolator());
mComeBackScroller = ScrollerCompat.create(context, new DecelerateInterpolator());
mMinTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DragSlopLayout, 0, 0);
mFixHeight = a.getDimensionPixelOffset(R.styleable.DragSlopLayout_fix_height, mFixHeight);
Expand Down Expand Up @@ -192,6 +197,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childHeight = childView.getMeasuredHeight();
// 限定视图的最大高度
if (childHeight > mMaxHeight) {
mMaxHeight = childHeight;
MeasureSpec.makeMeasureSpec(childWidth - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
childView.measure(MeasureSpec.makeMeasureSpec(childWidth - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mMaxHeight - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY));
Expand All @@ -201,7 +207,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);

MarginLayoutParams lp;
View childView = getChildAt(2);
lp = (MarginLayoutParams) childView.getLayoutParams();
Expand All @@ -214,27 +219,28 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 固定高度超过子视图高度则设置为子视图高度
mFixHeight = childHeight;
}
int childTop;
if (mDragStatus == STATUS_EXIT) {
// 对于 ViewPager 换页后会回调 onLayout(),需要进行处理
if (mMode == MODE_DRAG || mMode == MODE_DRAG_OUTSIDE) {
childTop = b;
} else {
childTop = b - mFixHeight;
childView.setTranslationY(childHeight);
}
} else if (mDragStatus == STATUS_COLLAPSED) {
childTop = b - mFixHeight;
} else if (mDragStatus == STATUS_EXPANDED) {
childTop = b - childHeight;
} else {
childTop = b - mFixHeight;
}
childView.layout(lp.leftMargin, childTop, lp.leftMargin + childWidth, childTop + childHeight);

mCriticalTop = b - (childHeight - mFixHeight) / 2 - mFixHeight;
mExpandedTop = b - childHeight;
mCollapsedTop = b - mFixHeight;
// 如果本身 mDragViewTop 已经有值,则直接使用,不然会出现突然闪一下的情况
if (mDragViewTop == 0) {
if (mDragStatus == STATUS_EXIT) {
// 对于 ViewPager 换页后会回调 onLayout(),需要进行处理
if (mMode == MODE_DRAG || mMode == MODE_DRAG_OUTSIDE) {
mDragViewTop = b;
} else {
mDragViewTop = b - mFixHeight;
childView.setTranslationY(childHeight);
}
} else if (mDragStatus == STATUS_COLLAPSED) {
mDragViewTop = b - mFixHeight;
} else if (mDragStatus == STATUS_EXPANDED) {
mDragViewTop = b - childHeight;
} else {
mDragViewTop = b - mFixHeight;
}
}
childView.layout(lp.leftMargin, mDragViewTop, lp.leftMargin + childWidth, mDragViewTop + childHeight);
}

@Override
Expand All @@ -254,6 +260,17 @@ protected void onDetachedFromWindow() {
* ViewDragHelper
********************************************/

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (MotionEventCompat.getActionMasked(ev)) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsDrag = false;
break;
}
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean isIntercept = mDragHelper.shouldInterceptTouchEvent(ev);
Expand All @@ -262,7 +279,8 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
} else if (mDragHelper.isViewUnder(mDragView, (int) ev.getX(), (int) ev.getY()) && mMode != MODE_ANIMATE) {
// 处于拖拽模式且点击到拖拽视图则停止滚动
_stopAllScroller();
} else if (mMode == MODE_DRAG_OUTSIDE) {
}
if (mMode == MODE_DRAG_OUTSIDE && !mIsDrag) {
mGestureDetector.onTouchEvent(ev);
}
return isIntercept;
Expand Down Expand Up @@ -292,7 +310,9 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
if (isDownTouch) {
// 如果为上下滑动则控制拖拽
if (Math.abs(distanceY) > Math.abs(distanceX)) {
_stopAllScroller();
mDragHelper.captureChildView(mDragView, 0);
mIsDrag = true;
}
isDownTouch = false;
}
Expand Down Expand Up @@ -450,7 +470,8 @@ private void _stopAllScroller() {

/**
* 滚出屏幕
* @param duration 时间
*
* @param duration 时间
*/
public void scrollOutScreen(int duration) {
mIsDoOutAnim = true;
Expand All @@ -460,7 +481,8 @@ public void scrollOutScreen(int duration) {

/**
* 滚进屏幕
* @param duration 时间
*
* @param duration 时间
*/
public void scrollInScreen(int duration) {
mIsDoOutAnim = false;
Expand Down Expand Up @@ -556,6 +578,15 @@ public void run() {
* @param percent 百分比
*/
private void _dragPositionChanged(int visibleHeight, float percent) {
if (mDragViewTop == 0) {
mLastDragViewTop = mHeight - visibleHeight;
}
mDragViewTop = mHeight - visibleHeight;
// 拖拽距离超过最小滑动距离则进行判断
if (Math.abs(mDragViewTop - mLastDragViewTop) > mMinTouchSlop) {
mIsUp = (mDragViewTop < mLastDragViewTop);
mLastDragViewTop = mDragViewTop;
}
if (mEnableBlur && mBlurDrawable != null) {
if (visibleHeight < mFixHeight) {
return;
Expand All @@ -571,6 +602,9 @@ private void _dragPositionChanged(int visibleHeight, float percent) {
if (visibleHeight >= 0) {
ViewCompat.setTranslationY(mMainView, -visibleHeight * (1 - mCollapseParallax));
}
if (mDragPositionListener != null) {
mDragPositionListener.onDragPosition(visibleHeight, percent, mIsUp);
}
}

/*********************************** ScrollView ********************************************/
Expand Down Expand Up @@ -1019,4 +1053,37 @@ public void run() {
}
}).start();
}

/** ================================ 监听器 ================================ */

// 监听器
private OnDragPositionListener mDragPositionListener;
// 是否向上拖拽
private boolean mIsUp = false;
// 上一次比较时的拖拽高度
private int mLastDragViewTop;
// 最小触摸滑动距离
private int mMinTouchSlop;

/**
* 设置监听器
* @param dragPositionListener
*/
public void setDragPositionListener(OnDragPositionListener dragPositionListener) {
mDragPositionListener = dragPositionListener;
}

/**
* 拖拽监听器
*/
public interface OnDragPositionListener {

/**
* 拖拽监听
* @param visibleHeight 可见高度
* @param percent 可见百分比
* @param isUp 是否向上拖拽
*/
void onDragPosition(int visibleHeight, float percent, boolean isUp);
}
}
2 changes: 2 additions & 0 deletions simple/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile project(':dragsloplayout')
// compile 'com.github.Rukey7:DragSlopLayout:1.0.2'
// compile 'com.github.Rukey7:DragSlopLayout:1.0.2-blur'
// photoview
compile 'com.github.chrisbanes.photoview:library:1.2.4'
// butterknife
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.dl7.drag.DragSlopLayout;
import com.dl7.drag.animate.CustomViewAnimator;
import com.dl7.simple.drag.AnimateHelper;
import com.dl7.simple.drag.PhotoPagerAdapter;
import com.dl7.simple.drag.adapter.PhotoPagerAdapter;
import com.dl7.simple.drag.R;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import android.widget.TextView;

import com.dl7.drag.DragSlopLayout;
import com.dl7.simple.drag.PhotoPagerAdapter;
import com.dl7.simple.drag.adapter.PhotoPagerAdapter;
import com.dl7.simple.drag.R;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,46 @@

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ScrollView;
import android.widget.TextView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dl7.drag.DragSlopLayout;
import com.dl7.simple.drag.PhotoPagerAdapter;
import com.dl7.simple.drag.R;
import com.dl7.simple.drag.adapter.PhotoPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DragOutsideActivity extends BaseActivity {


@BindView(R.id.vp_photo)
ViewPager mVpPhoto;
@BindView(R.id.tv_title)
TextView mTvTitle;
@BindView(R.id.tv_count)
TextView mTvCount;
@BindView(R.id.tv_index)
TextView mTvIndex;
@BindView(R.id.tv_content)
TextView mTvContent;
@BindView(R.id.sv_view)
ScrollView mSvView;
@BindView(R.id.drag_layout)
DragSlopLayout mDragLayout;
@BindView(R.id.rv_relate_list)
RecyclerView mRvRelateList;
@BindView(R.id.tool_bar)
Toolbar mToolBar;
@BindView(R.id.iv_favorite)
ImageView mIvFavorite;
@BindView(R.id.iv_download)
ImageView mIvDownload;
@BindView(R.id.iv_praise)
ImageView mIvPraise;
@BindView(R.id.iv_share)
ImageView mIvShare;
@BindView(R.id.bottom_bar)
LinearLayout mBottomBar;
@BindView(R.id.drag_layout)
DragSlopLayout mDragLayout;

private boolean mIsInteract = true;

Expand All @@ -44,18 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_outside);
ButterKnife.bind(this);
initToolBar(mToolBar, true, "");
initToolBar(mToolBar, true, "Drag Outside Mode");

final String[] titleList = new String[]{
"汤姆·哈迪", "克里斯蒂安·贝尔", "马克·沃尔伯格", "威尔·史密斯", "丹泽尔·华盛顿",
};
final String[] descList = new String[]{
getString(R.string.TomHardy),
getString(R.string.ChristianBale),
getString(R.string.MarkWahlberg),
getString(R.string.WillSmith),
getString(R.string.DenzelWashington),
};
List<Integer> imgList = new ArrayList<>();
imgList.add(R.mipmap.img1);
imgList.add(R.mipmap.img2);
Expand All @@ -65,24 +61,8 @@ protected void onCreate(Bundle savedInstanceState) {

PhotoPagerAdapter mPagerAdapter = new PhotoPagerAdapter(this, imgList, false);
mVpPhoto.setAdapter(mPagerAdapter);
// 实现 ScrollView 的平滑滚动
mDragLayout.setAttachScrollView(mSvView);
// 和 ViewPager 联动
mDragLayout.interactWithViewPager(mIsInteract);

mTvCount.setText("" + imgList.size());
mTvTitle.setText(titleList[0]);
mTvIndex.setText("" + 1);
mTvContent.setText(descList[0]);

mVpPhoto.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mTvTitle.setText(titleList[position]);
mTvIndex.setText("" + (position + 1));
mTvContent.setText(descList[position]);
}
});
}

@Override
Expand All @@ -102,4 +82,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

@OnClick({R.id.iv_favorite, R.id.iv_download, R.id.iv_praise, R.id.iv_share})
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_favorite:
break;
case R.id.iv_download:
break;
case R.id.iv_praise:
break;
case R.id.iv_share:
break;
}
}
}
Loading

0 comments on commit b89f431

Please sign in to comment.