Android嵌套滚动Demo-NestedScrollingChild实现
2016-11-10 17:09
453 查看
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>
已测试 可以实现嵌套滚动~
如果有问题可以留言~~~
相关文章推荐
- 实现嵌套滑动——NestedScrollingParent与NestedScrollingChild接口及其实现
- Android NestedScrolling全面解析 - 带你实现一个支持嵌套滑动的下拉刷新(上篇)
- Android NestedScrolling嵌套滚动的示例代码
- Android-NestedScrollingParent, NestedScrollingChild父子View 间 的 嵌套滑动
- NestedScrollingParent 和NestedScrollingChild 实现嵌套滑动
- Android 自定义WebView 实现 NestedScrollingChild
- Android 自定义WebView 实现 NestedScrollingChild
- Android NestedScrolling帮你实现一个简单的嵌套滑动
- android NestedScrolling嵌套滑动实战之联合滚动fling效果
- Android NestedScrolling机制解析 带你玩爆嵌套滑动
- Android ViewPager嵌套GridView实现滚动菜单(demo)
- Android NestedScrolling机制完全解析 带你玩转嵌套滑动
- WebView 实现 NestedScrollingChild
- 详解:Android嵌套滑动机制 (NestedScrolling)
- 详解:Android嵌套滑动机制 (NestedScrolling)
- Android中的NestedScrollingParent和NestedScrollingChild分析
- Android 5.0 嵌套滑动NestedScrolling 源码解析
- Android 嵌套滑动机制(NestedScrolling)
- Android NestedScrolling机制完全解析 带你玩转嵌套滑动
- Android 嵌套滑动机制(NestedScrolling)