Skip to content

Commit

Permalink
嵌套滚动处理惯性滑动
Browse files Browse the repository at this point in the history
  • Loading branch information
mahao committed Apr 7, 2017
1 parent fc5cdc3 commit cbf0c9f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand All @@ -26,7 +27,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nest_scroll);
mRecycleView = ((RecyclerView) findViewById(R.id.recycle));

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecycleView.setLayoutManager(layoutManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.OverScroller;
import android.widget.Scroller;

/**
* Created by mahao on 17-3-29.
*/

public class NestScrollLinearLayout extends LinearLayout {

private OverScroller mScroller;

public void log(String log) {
Log.i("NestScrollLinearLayout", log);
}
Expand All @@ -31,11 +37,12 @@ public NestScrollLinearLayout(Context context) {
}

public NestScrollLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
this(context, attrs, 0);
}

public NestScrollLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mScroller = new OverScroller(context);
}

@Override
Expand All @@ -46,12 +53,49 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

@Override
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
return super.onNestedFling(target, velocityX, velocityY, consumed);
log("onNestedFling velocityY=" + velocityY);
// 子类滑动后
/* if (getScrollY() > 0 && velocityY < 0) {
// 向下滑动且header 隐藏,父类处理滑动
fling((int) velocityY);
return true;
}*/
return false;
}

@Override
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
return super.onNestedPreFling(target, velocityX, velocityY);
return false;
}

/**
* 处理惯性滑动
*
* @param velocityY
*/
private void fling(int velocityY) {
mScroller.fling(0, getScrollY(), 0, velocityY, 0, 0, 0, mFirstChildHeight);
invalidate();
}

public void flingTo(int x, int y) {
if (y < 0) {
y = 0;
}
if (y > mFirstChildHeight) {
y = mFirstChildHeight;
}
if (y != getScrollY()) {
scrollTo(x, y);
}
}

@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
flingTo(0, mScroller.getCurrY());
invalidate();
}
}

@Override
Expand All @@ -72,32 +116,45 @@ public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
mConsumeHeight = mFirstChildHeight;
}
scrollTo(0, mConsumeHeight);
ViewGroup.LayoutParams params = getChildAt(2).getLayoutParams();
params.height = getChildAt(2).getMeasuredHeight() + dy;
log("recycleView ++++++++++++++++++++++height = " + params.height);
getChildAt(2).setLayoutParams(params);
consumed[1] = dy;
}
}
log("dx=" + dx + " dy=" + dy + " consumed=" + consumed[0] + " " + consumed[1]);
//log("dx=" + dx + " dy=" + dy + " consumed=" + consumed[0] + " " + consumed[1]);
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// 未被消费的
if (dyUnconsumed < 0 && mConsumeHeight > 0){
if (dyUnconsumed < 0 && mConsumeHeight > 0) {
mConsumeHeight = mConsumeHeight + dyUnconsumed;
if (mConsumeHeight < 0) {
dyUnconsumed = 0 - mConsumeHeight;
mConsumeHeight = 0;
}
scrollTo(0, mConsumeHeight);
ViewGroup.LayoutParams params = getChildAt(2).getLayoutParams();
params.height = getChildAt(2).getMeasuredHeight() + dyUnconsumed;
log("recycleView -------------------height = " + params.height);
getChildAt(2).setLayoutParams(params);
}

// 消费的和未被消息的


// 滚动之后的回调 ,子类违背消费的
log("dxConsumed=" + dxConsumed + " dyConsumed=" + dyConsumed + " dxUnconsumed=" + dxUnconsumed + " dyUnconsumed=" + dyUnconsumed);
//log("dxConsumed=" + dxConsumed + " dyConsumed=" + dyConsumed + " dxUnconsumed=" + dxUnconsumed + " dyUnconsumed=" + dyUnconsumed);
}

@Override
public void onNestedScrollAccepted(View child, View target, int axes) {
super.onNestedScrollAccepted(child, target, axes);
}


@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="wrap_content"/>

</com.mahao.alex.customviewdemo.nestscroll.NestScrollLinearLayout>

0 comments on commit cbf0c9f

Please sign in to comment.