CoordinatorLayout滚动问题中的自定义NestedScrollingChild(抖动)

时间:2015-11-26 16:16:37

标签: android android-animation android-coordinatorlayout

根据我的要求,我编写了一个自定义的NestedScrollingChild,用于AppBarLayout。

要求是我需要自定义视图,部分显示可以滚动到其完全可见性。另一个布局将位于部分显示的视图上方,并且当我们向上滚动自定义视图时将向上滚动。工具栏向上滑动时布局将停止,之后自定义视图可以进一步滚动到其完整显示

这是我正在处理的代码,当我尝试向上移动自定义视图时,它正在摇晃,工具栏也在摇晃。

import android.content.Context;
import android.graphics.RectF;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;



public class MyCustomView extends LinearLayout
        implements NestedScrollingChild,
        GestureDetector.OnGestureListener {


    private static final String TAG = "MyCustomView";
    LinearLayout mainView;
    private GestureDetectorCompat mDetector;
    NestedScrollingChildHelper nestedScrollingChildHelper;


    public MyCustomView(Context context) {
        super(context);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.fragment_footer  , this);
        mDetector = new GestureDetectorCompat(context,this);
        nestedScrollingChildHelper = new NestedScrollingChildHelper(this);
        nestedScrollingChildHelper.setNestedScrollingEnabled(true);
    }


    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.fragment_footer  , this);

        mDetector = new GestureDetectorCompat(context,this);
        nestedScrollingChildHelper = new NestedScrollingChildHelper(this);
        nestedScrollingChildHelper.setNestedScrollingEnabled(true);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final boolean handled = mDetector.onTouchEvent(event);
        if (!handled && event.getAction() == MotionEvent.ACTION_UP) {
            nestedScrollingChildHelper.stopNestedScroll();
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(TAG,"widthMeasureSpec"+widthMeasureSpec+", heightMeasureSpec"+heightMeasureSpec );
    }

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        nestedScrollingChildHelper.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        nestedScrollingChildHelper.dispatchNestedPreScroll(0, (int) distanceY, null, null);
        nestedScrollingChildHelper.dispatchNestedScroll(0, 0, 0, 0, null);
        Log.d(TAG, "DistanceX " + distanceX + ", DistanceY" + distanceY);
        Log.d(TAG, "e1X " + e1.getX() + ", e1Y" + e1.getY());
        Log.d(TAG, "e2X " + e2.getX() + ", e2Y" + e2.getY());

        if(Math.abs(e1.getY() - e2.getY()) > 10) {
            setTop(getTop() - (int) (e1.getY() - e2.getY()));
        }

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}

布局代码

    <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
             <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="100dp"
                    android:layout_height="65dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/logo" />
            </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_main" />
    <com.sample.custom.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        />
</android.support.design.widget.CoordinatorLayout>

如果我评论setTop(getTop() - (int) (e1.getY() - e2.getY()));客户视图上的滚动条会正确隐藏/显示工具栏

0 个答案:

没有答案