Skip to content

Commit

Permalink
Merge pull request #98 from guanjianzhe/master
Browse files Browse the repository at this point in the history
嵌套滑动:优化嵌套滑动逻辑
  • Loading branch information
MagicMashRoom authored Dec 22, 2017
2 parents 3b000bd + f9fd1d6 commit 0ad4670
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion calendar/src/main/java/com/ldf/calendar/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static CalendarDate getSaturday(CalendarDate seedDate) {// TODO: 16/12/12
/**
* 判断上一次滑动改变周月日历是向下滑还是向上滑 向下滑表示切换为月日历模式 向上滑表示切换为周日历模式
*
* @return boolean 是否是在向下滑动
* @return boolean 是否是在向下滑动。(true: 已经收缩; false: 已经打开)
*/
public static boolean isScrollToBottom() {
return customScrollToBottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;
import android.widget.Toast;

import com.ldf.calendar.Utils;
import com.ldf.calendar.view.MonthPager;
Expand All @@ -19,6 +18,8 @@ public class RecyclerViewBehavior extends CoordinatorLayout.Behavior<RecyclerVie
private int minOffset = -1;
private Context context;
private boolean initiated = false;
boolean hidingTop = false;
boolean showingTop = false;

public RecyclerViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
Expand All @@ -40,7 +41,7 @@ private void initMinOffsetAndInitOffset(CoordinatorLayout parent,
initOffset = monthPager.getViewHeight();
saveTop(initOffset);
}
if(!initiated) {
if (!initiated) {
initOffset = monthPager.getViewHeight();
saveTop(initOffset);
initiated = true;
Expand All @@ -52,32 +53,37 @@ private void initMinOffsetAndInitOffset(CoordinatorLayout parent,
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, RecyclerView child,
View directTargetChild, View target, int nestedScrollAxes) {
Log.e("ldf","onStartNestedScroll");
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) child.getLayoutManager();
if (linearLayoutManager.findFirstCompletelyVisibleItemPosition() > 0) {
return false;
}
Log.e("ldf", "onStartNestedScroll");

MonthPager monthPager = (MonthPager) coordinatorLayout.getChildAt(0);
if (monthPager.getPageScrollState() != ViewPager.SCROLL_STATE_IDLE) {
return false;
}
monthPager.setScrollable(false);
boolean isVertical = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
int firstRowVerticalPosition =
(child == null || child.getChildCount() == 0) ? 0 : child.getChildAt(0).getTop();
boolean recyclerViewTopStatus = firstRowVerticalPosition >= 0;
return isVertical
&& (recyclerViewTopStatus || !Utils.isScrollToBottom())
&& child == directTargetChild;

return isVertical;
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, RecyclerView child,
View target, int dx, int dy, int[] consumed) {
Log.e("ldf","onNestedPreScroll");
Log.e("ldf", "onNestedPreScroll");
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
if (child.getTop() <= initOffset
&& child.getTop() >= getMonthPager(coordinatorLayout).getCellHeight()) {
child.setVerticalScrollBarEnabled(true);

MonthPager monthPager = (MonthPager) coordinatorLayout.getChildAt(0);
if (monthPager.getPageScrollState() != ViewPager.SCROLL_STATE_IDLE) {
consumed[1] = dy;
Log.w("ldf", "onNestedPreScroll: MonthPager dragging");
Toast.makeText(context, "loading month data", Toast.LENGTH_SHORT).show();
return;
}

// 上滑,正在隐藏顶部的日历
hidingTop = dy > 0 && child.getTop() <= initOffset
&& child.getTop() > getMonthPager(coordinatorLayout).getCellHeight();
// 下滑,正在展示顶部的日历
showingTop = dy < 0 && !ViewCompat.canScrollVertically(target, -1);

if (hidingTop || showingTop) {
consumed[1] = Utils.scroll(child, dy,
getMonthPager(coordinatorLayout).getCellHeight(),
getMonthPager(coordinatorLayout).getViewHeight());
Expand All @@ -87,25 +93,41 @@ public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, RecyclerView

@Override
public void onStopNestedScroll(final CoordinatorLayout parent, final RecyclerView child, View target) {
Log.e("ldf","onStopNestedScroll");
Log.e("ldf", "onStopNestedScroll");
super.onStopNestedScroll(parent, child, target);
MonthPager monthPager = (MonthPager) parent.getChildAt(0);
monthPager.setScrollable(true);
if (!Utils.isScrollToBottom()) {
if (initOffset - Utils.loadTop() > Utils.getTouchSlop(context)) {
Utils.scrollTo(parent, child, getMonthPager(parent).getCellHeight(), 200);
if (initOffset - Utils.loadTop() > Utils.getTouchSlop(context) && hidingTop) {
Utils.scrollTo(parent, child, getMonthPager(parent).getCellHeight(), 500);
} else {
Utils.scrollTo(parent, child, getMonthPager(parent).getViewHeight(), 80);
Utils.scrollTo(parent, child, getMonthPager(parent).getViewHeight(), 150);
}
} else {
if (Utils.loadTop() - minOffset > Utils.getTouchSlop(context)) {
Utils.scrollTo(parent, child, getMonthPager(parent).getViewHeight(), 200);
if (Utils.loadTop() - minOffset > Utils.getTouchSlop(context) && showingTop) {
Utils.scrollTo(parent, child, getMonthPager(parent).getViewHeight(), 500);
} else {
Utils.scrollTo(parent, child, getMonthPager(parent).getCellHeight(), 80);
Utils.scrollTo(parent, child, getMonthPager(parent).getCellHeight(), 150);
}
}
}

@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, RecyclerView child, View target, float velocityX, float velocityY, boolean consumed) {
Log.d("ldf", "onNestedFling: velocityY: " + velocityY);
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}

@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, RecyclerView child, View target, float velocityX, float velocityY) {
// 日历隐藏和展示过程,不允许RecyclerView进行fling
if (hidingTop || showingTop) {
return true;
} else {
return false;
}
}

private MonthPager getMonthPager(CoordinatorLayout coordinatorLayout) {
return (MonthPager) coordinatorLayout.getChildAt(0);
}
Expand Down

0 comments on commit 0ad4670

Please sign in to comment.