我创建了一个实现NestedScrollingParent的LinearLayout,然后在其中放置了一些视图(ImageView,View,RecyclerView),但是fling操作有一些问题。 视频:https://youtu.be/3liTZtKKcew
<com.lsl.md.nested.MyNestedLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@mipmap/material_flat" />
<View
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent" />
</com.lsl.md.nested.MyNestedLinearLayout>
这里我创建了一个扩展LinearLayout并实现NestedScrollingParent的类,问题是当惯性滑动到RecyclerView的顶部时,上面的ImageView没有响应滑动
public class MyNestedLinearLayout extends LinearLayout implements NestedScrollingParent {
private static final String TAG = "MyNestedLinearLayout";
private NestedScrollingParentHelper parentHelper;
private ImageView imageView;
private int imageHeight;
private OverScroller mScroller;
public MyNestedLinearLayout(Context context) {
this(context, null);
}
public MyNestedLinearLayout(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyNestedLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
parentHelper = new NestedScrollingParentHelper(this);
mScroller = new OverScroller(context);
}
/*NestedScrollingParent*/
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
// return target instanceof RecyclerView;
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
@Override
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
Log.i(TAG, "onNestedScrollAccepted: ...");
parentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
}
@Override
public void onStopNestedScroll(View target) {
Log.i(TAG, "onStopNestedScroll: ...");
parentHelper.onStopNestedScroll(target);
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
Log.i(TAG, "onNestedScroll: ...");
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (dy > 0) {
if (getScrollY() < imageHeight) {
scrollBy(0, dy);
consumed[1] = dy;
}
}
if (dy < 0 && !ViewCompat.canScrollVertically(target, -1)) {
if (getScrollY() > 0) {
scrollBy(0, dy);
consumed[1] = dy;
}
}
Log.i(TAG, "onNestedPreScroll: ...dy:");
}
@Override
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Log.i(TAG, "onNestedFling: ..." + velocityY);
return false;
}
@Override
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
if (getScrollY() >= imageHeight) return false;
fling((int) velocityY);
return true;
}
@Override
public int getNestedScrollAxes() {
Log.i(TAG, "getNestedScrollAxes: ...");
return parentHelper.getNestedScrollAxes();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
imageView = (ImageView) getChildAt(0);
}
@Override
public void scrollTo(@Px int x, @Px int y) {
if (y < 0) {
y = 0;
}
if (y > imageHeight) {
y = imageHeight;
}
super.scrollTo(x, y);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
imageView = (ImageView) getChildAt(0);
measureChildWithMargins(imageView, widthMeasureSpec, 0, MeasureSpec.UNSPECIFIED, 0);
imageHeight = imageView.getMeasuredHeight();
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) + imageHeight,
MeasureSpec.EXACTLY));
}
public void fling(int velocityY) {
mScroller.fling(0, getScrollY(), 0, velocityY, 0, 0, 0, imageHeight);
invalidate();
}
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
scrollTo(0, mScroller.getCurrY());
invalidate();
}
}
}