public class TestNestedChildView extends LinearLayout implements NestedScrollingChild {
private NestedScrollingChildHelper helper;
public TestNestedChildView(Context context) {
super(context);
init();
}
public TestNestedChildView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
helper = new NestedScrollingChildHelper(this);
setNestedScrollingEnabled(true);
}
public void setNestedScrollingEnabled(boolean enabled) {
helper.setNestedScrollingEnabled(enabled);
}
public boolean isNestedScrollingEnabled() {
return true;
}
public boolean startNestedScroll(int axes) {
return helper.startNestedScroll(axes);
}
public void stopNestedScroll() {
helper.stopNestedScroll();
}
public boolean hasNestedScrollingParent() {
return helper.hasNestedScrollingParent();
}
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
return helper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
return helper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return helper.dispatchNestedFling(velocityX, velocityY, consumed);
}
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
return helper.dispatchNestedPreFling(velocityX, velocityY);
}
private final int[] mNestedOffsets = new int[2];
private final int[] mScrollOffset = new int[2];
private final int[] mScrollConsumed = new int[2];
private int mLastTouchX;
private int mLastTouchY;
@Override
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mNestedOffsets[0] = mNestedOffsets[1] = 0;
}
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: {
mLastTouchX = (int) (e.getX() + 0.5f);
mLastTouchY = (int) (e.getY() + 0.5f);
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
}
break;
case MotionEvent.ACTION_MOVE: {
Log.e("touch ",""+e.getY());
final int x = (int) e.getX();
final int y = (int) e.getY();
int dx = mLastTouchX - x;
int dy = mLastTouchY - y;
if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
dx -= mScrollConsumed[0];
dy -= mScrollConsumed[1];
mNestedOffsets[0] += mScrollOffset[0];
mNestedOffsets[1] += mScrollOffset[1];
}
if(getScrollY()+dy>0) {
scrollTo(0, getScrollY()+dy);
dispatchNestedScroll(0,dy,dx,0,mNestedOffsets);
}else{
if(getScrollY()>0){
scrollBy(0,-getScrollY());
dispatchNestedScroll(0,getScrollY(),dx,dy-getScrollY(),mNestedOffsets);
}
scrollTo(0, 0);
}
mLastTouchX = x - mScrollOffset[0];
mLastTouchY = y - mScrollOffset[1];
}
break;
case MotionEvent.ACTION_UP: {
stopNestedScroll();
}
break;
}
return true;
}
}
这里只有NestedScrollingChild
的实现,外面可以包个CoordinateLayout
来查看下效果.
下面是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="enterAlways|scroll"
app:title="测试"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<com.example.javalong.myapplication.nestedscroll.TestNestedChildView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="bbbbbbbbbbbbb"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="bbbbbbbbbbbbb"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="bbbbbbbbbbbbb"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="bbbbbbbbbbbbb"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="bbbbbbbbbbbbb"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="aaaaaaaaaaaaaaa"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="aaaaaaaaaaaaaaa"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="aaaaaaaaaaaaaaa"
/>
</LinearLayout>
</com.example.javalong.myapplication.nestedscroll.TestNestedChildView>
</android.support.design.widget.CoordinatorLayout>
已测试 可以实现嵌套滚动~
如果有问题可以留言~~~