记录协同布局学习之路
嵌套滑动布局CoordinatorLayout,这篇文章讲得特别好,我看了那么多文章后,觉得这篇文章看完有一种豁然开朗的感觉。文档地址:https://blog.csdn.net/briblue/article/details/73076458
以下是官方文档中有关嵌套滑动主要方法说明:
onStartNestedScroll方法:该方法的返回值决定是否执行嵌套滑动
onNestedPreScroll方法:处理具体的滑动
CoordinatorLayout类实现了NestedScrollingParent2接口
RecyclerView类实现了NestedScrollingChild2和NestedScrollingChild3接口
public interface NestedScrollingChild {
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);
public boolean dispatchNestedPreFling(float velocityX, float velocityY);
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);
public boolean hasNestedScrollingParent();
public boolean isNestedScrollingEnabled();
public void setNestedScrollingEnabled(boolean enabled);
public boolean startNestedScroll(int axes);
public void stopNestedScroll();
}
NestedScrollingParent接口
public interface NestedScrollingParent {
public int getNestedScrollAxes();
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
public boolean onNestedPreFling(View target, float velocityX, float velocityY);
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
public void onNestedScroll(View target, int dxConsumed, int dyConsumed,int dxUnconsumed, int dyUnconsumed);
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
public void onStopNestedScroll(View target);
}
View类
public final int getLeft(){
return mLeft;
}
getLeft方法获取的是mLeft,也就是:从View父控件的左边距到View控件的左边距的距离(以像素为单位)
public final int getRight(){
return mRight;
}
mRight的含义是:View父控件的左边距到View控件的右边距的距离(以像素为单位)
所以控件宽度=mRight-mLeft
public final int getWidth(){
return mRight-mLeft;
}
public float getX() {
return mLeft + getTranslationX();
}
getX()获取的是mLeft+getTranslationX(),视图的可视位置,像素为单位。
getTranslationX()获取的是View的水平移动距离,水平移动向右移动值为正,垂直移动向下移动值为正,属性动画ObjectAnimator valueAnimator2 = ObjectAnimator.ofFloat(txtTitle, "translationY", -20);//
public float getTranslationX() 含义:获取View相对与自身mLeft的水平位置(以像素为单位,相对位置)。
public void setTranslationX(float translationX) 含义:设置View相对于自身mLeft的水平位置(以像素为单位,相对位置)。
public void setX(float x) {
setTranslationX(x - mLeft);
}
getTranslationX和getTranslationY移动的是View本身 水平向右值为负,水平向左值为正,垂直向下值为负,垂直向上值为正数。
滑动的方法scrollTo和scrollBy方法移动的不是View本身,是View的内容。