design包的出现,Android界面发生了巨大变化,各种滑动配合的效果,下面我就粘性滑动中的一种进行自定义,效果图如下:

大家看到效果了,这里我是继承了LinerLayout,方便一点,若果是ViewGroup的话,也就复杂一点点。这里分为三部分:

1.head1,顶部可移动的Layout。

2.head2,固定的头部,不会滑动除屏幕外。

3.可滑动的Layout(这里只可以是ListView,不过也可以是任何可滑动的View,只要给出Head可滑动的时机即可)

本StickinessView的难点在于,解决滑动冲突和事件的拦截处理,接下来我一一道来。

一、首先,要确定HeadLayout什么时候可以拦截事件,那么就要确定ListView到达顶部和底部的时机。

@Override

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

View v = mListView.getChildAt(0);

//当firstItem的top为0的时候就认为已经到达ListView的顶部了

if (mListView.getChildCount() > 0 && firstVisibleItem == 0) {

//滑动到顶部

if (v.getTop() == 0) {

//滑动到顶部了

isListViewTop = true;

} else {

isListViewBottom = false;

}

}else if (mListView.getChildCount()>0&&firstVisibleItem+visibleItemCount==totalItemCount){

final View bottomChildView = mListView.getChildAt(mListView.getChildCount()-1);

//当最后一个itemView的bottom>=ListView的高度的时候,那么就认为到达底部了

if (mListView.getHeight()>=bottomChildView.getBottom()){

isListViewBottom = true;

}else {

isListViewBottom = false;

}

}else {

isListViewBottom = false;

isListViewTop = false;

}

原因很简单,因为View的getTop和getBottom方法是相对父容器的位置,熟悉Layout方法的,想必就会很明白了。

二、知道了HeadView拦截事件的时机,我们就要搞清楚在此基础之上,我们到底啥时候拦击点击事件,进行滑动。

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

touchY = ev.getRawY();

isIntercept = false;

break;

case MotionEvent.ACTION_MOVE:

float distant = ev.getRawY() - touchY;

if (isListViewTop) {

switch (mHeadPosition) {

case TOP:

if (distant > 0) isIntercept = true;

break;

case CENTER:

isIntercept = true;

break;

}

}

if (isListViewBottom){

switch (mHeadPosition) {

case CENTER:

isIntercept = true;

break;

case BOTTOM:

if (distant < 0) isIntercept = true;

break;

}

}

break;

case MotionEvent.ACTION_UP:

isIntercept = true;

break;

}

return isIntercept;

}

跟大家讲解一下onInterceptTouchEvent(MotionEvent ev),这个方法会最先调用,当一个事件序列拦截一次后,那么这个事件的后续事件动作就不会再调用该方法,也就是说,当该ViewGroup决定拦截某个事件后,那么它注定要消费后续的事件动作。这里贴出HeadView的位置状态

public static final int TOP = 0;//收缩状态

public static final int CENTER = 1;//中间状态

public static final int BOTTOM = 2;//展开状态

关于细节,想必大家画个图就可以知道了,注意一点:在拦截事件序列的时候,一般ACTION_DOWN事件不可以被拦截,因为拦截的话,没得意义了,后续事件就无法控制了,不可能继续往ChildView传递事件序列。

三、移动HeadView。

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

//获取不到的

break;

case MotionEvent.ACTION_MOVE:

int distant = (int) (touchY - event.getRawY());

if (getScrollY() + distant-1 < MAXY && getScrollY() + distant > 0) {

scrollTo(0, getScrollY() + distant);

}

break;

case MotionEvent.ACTION_UP:

if (getScrollY() == 0) mHeadPosition = BOTTOM;

if (getScrollY() == MAXY) mHeadPosition = TOP;

if (getScrollY() > 0 && getScrollY() < MAXY) mHeadPosition = CENTER;

if (getScrollY() > MAXY / 2) {

mScroll.startScroll(0, getScrollY(), 0, MAXY-getScrollY(),100);

invalidate();

mHeadPosition = TOP;

}

if (getScrollY() < MAXY / 2) {

mScroll.startScroll(0, getScrollY(),0,-getScrollY(),100);

invalidate();

mHeadPosition = BOTTOM;

}

break;

}

return super.onTouchEvent(event);

}

这里为了使得滑动跟家顺畅我使用了Scroller这个类,该类是专门处理弹性滑动的工具类,先初始化构造器,在调用startScroll()方法(其中四个参数:滑动的x,滑动的y,滑动x的偏移量,滑动y的偏移量),然后刷新视图,最后重写computeScroll()方法,

@Override

public void computeScroll() {

super.computeScroll();

if (mScroll.computeScrollOffset()){

scrollTo(mScroll.getCurrX(),mScroll.getCurrY());

postInvalidate();

}

}

好了,基本完成,我们还要第一时间获取HeadView的高度,那么在onMeasure()中获取比较好,并且只获取一次如下

if (MAXY == -1)

MAXY = mHeadSecond.getMeasuredHeight();

在onFinishInflate()方法中,该方法的执行标志着所有的View都已经add完毕,这里我们进行初始化是比较妥当的。

@Override

protected void onFinishInflate() {

super.onFinishInflate();

int count = getChildCount();

//本粘性布局只支持ListView

if (count == 3 && getChildAt(2) instanceof ListView)

init();

}

/**

* 初始化

*/

private void init() {

//获得子元素

mHeadFiest = getChildAt(0);

mHeadSecond = getChildAt(1);

mListView = (ListView) getChildAt(2);

mListView.setOnScrollListener(this);

mScroll = new Scroller(getContext());

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐