package com.wasu.widgets.nestedscrollingwidgets;

import android.content.Context;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.NestedScrollingParentHelper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

import com.wasu.module.log.WLog;

/**
 * Created by chenming on 2016/11/7.
 *
 * @Description: 能够嵌套滑动的scrollview
 * @email chenming@wasu.com
 */

public class NestedScrollingView extends ScrollView implements NestedScrollingParent {
	public static final String TAG = "NestedScrollingView";
	private NestedScrollingParentHelper mParentHelper;

	public NestedScrollingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mParentHelper = new NestedScrollingParentHelper(this);
	}

	public NestedScrollingView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mParentHelper = new NestedScrollingParentHelper(this);

	}

	public NestedScrollingView(Context context) {
		super(context);
		mParentHelper = new NestedScrollingParentHelper(this);
	}

	/**
	 * 回调开始滑动
	 *
	 * @param child            该父VIew 的子View
	 * @param target           支持嵌套滑动的 VIew
	 * @param nestedScrollAxes 滑动方向
	 * @return 是否支持 嵌套滑动
	 */
	@Override
	public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
		return true;
	}

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

	@Override
	public void onStopNestedScroll(View target) {
		mParentHelper.onStopNestedScroll(target);
	}

	/**
	 * 这里 主要处理 dyUnconsumed dxUnconsumed 这两个值对应的数据
	 *
	 * @param target
	 * @param dxConsumed
	 * @param dyConsumed
	 * @param dxUnconsumed
	 * @param dyUnconsumed
	 */
	@Override
	public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
	                           int dxUnconsumed, int dyUnconsumed) {
		WLog.e("chenming", "dxUnconsumed:" + dxUnconsumed + ",dyUnconsumed:" + dyUnconsumed);

	}

	/**
	 * 这里 传来了 x y 方向上的滑动距离
	 * 并且 先与 子VIew  处理滑动,  并且 consumed  中可以设置相应的 除了的距离
	 * 然后 子View  需要更具这感觉, 来处理自己滑动
	 *
	 * @param target
	 * @param dx
	 * @param dy
	 * @param consumed
	 */
	@Override
	public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
		WLog.e("chenming", "dxUnconsumed:" + dx + ",dyUnconsumed:" + dy+"consumed:"+consumed.toString());

		//consumed[1] = dy;
	}

}
