自定義滾動(dòng)的線(xiàn)性布局主要需要完成下面3個(gè)功能
1 計(jì)算子view及本身的尺寸
2 把子view布局到指定的位置
3 添加滑動(dòng)事件
1 計(jì)算尺寸
需要重寫(xiě)下面這個(gè)方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
參數(shù)中的widthMeasureSpec
和heightMeasureSpec
是包含寬和高的信息课梳。里面放了測(cè)量模式和尺寸大小但惶,具體獲取方法
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
MeasureSpec.AT_MOST
對(duì)應(yīng)布局中的WRAP_CONTENT
,MeasureSpec.EXACTLY
對(duì)應(yīng)MATCH_PARENT
和固定的尺寸,MeasureSpec.UNSPECIFIED
指未指定尺寸棘幸,這種情況很少
如果寬高是自適應(yīng)的秸弛、就需要我們自己來(lái)計(jì)算實(shí)際尺寸大小
要實(shí)現(xiàn)可以滾動(dòng)的水平線(xiàn)性布局viewGroup的最大寬度實(shí)際上就是所有子view中最寬的view的寬度裁厅,最大高度是所有子view高度的和
private int getMaxChildWidth() {
int childCount = getChildCount();
int maxWidth = 0;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getMeasuredWidth() > maxWidth)
maxWidth = childView.getMeasuredWidth();
}
return maxWidth
}
private int getTotleHeight() {
int childCount = getChildCount();
int height = 0;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
height += childView.getMeasuredHeight();
}
return height;
}
重寫(xiě)onMeasure
方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//如果寬高都是包裹內(nèi)容
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
//我們將高度設(shè)置為所有子View的高度相加柔昼,寬度設(shè)為子View中最大的寬度
int height = getTotleHeight();
int width = getMaxChildWidth();
setMeasuredDimension(width, height);
} else if (heightMode == MeasureSpec.AT_MOST) {//如果只有高度是包裹內(nèi)容
//寬度設(shè)置為ViewGroup自己的測(cè)量寬度,高度設(shè)置為所有子View的高度總和
setMeasuredDimension(widthSize, getTotleHeight());
} else if (widthMode == MeasureSpec.AT_MOST) {//如果只有寬度是包裹內(nèi)容
//寬度設(shè)置為子View中寬度最大的值布近,高度設(shè)置為ViewGroup自己的測(cè)量值
setMeasuredDimension(getMaxChildWidth(), heightSize);
} else {
setMeasuredDimension(widthSize, heightSize);
}
}
尺寸計(jì)算好了垫释,下面就要布局子view了
2 布局子view
實(shí)現(xiàn)起來(lái)很簡(jiǎn)單
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int top = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int measuredHeight = child.getMeasuredHeight();
int measuredWidth = child.getMeasuredWidth();
child.layout(0, top, measuredWidth, top + measuredHeight);
top += measuredHeight;
}
}
3 添加滑動(dòng)事件
添加滑動(dòng)事件就要重寫(xiě)onTouchEvent
方法了,可以用手勢(shì)幫助類(lèi)GestureDetector來(lái)實(shí)現(xiàn)
@Override
public boolean onTouchEvent(MotionEvent event) {
mGesture.onTouchEvent(event);
return true;
}
private void init() {
mGesture = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollBy(0, (int) distanceY);
return true;
}
});
}
到此吊输,可以滑動(dòng)的線(xiàn)性布局就基本完成了饶号,但是還存在一些缺陷铁追,滑動(dòng)會(huì)超過(guò)邊界季蚂、沒(méi)有慣性滑動(dòng)
首先先來(lái)解決滑動(dòng)超過(guò)邊界的問(wèn)題,思路是在手指抬起的時(shí)候如果滑動(dòng)超過(guò)邊界,就重新滾動(dòng)到邊界扭屁,為了使滑動(dòng)流暢算谈,使用OverScroller實(shí)現(xiàn),修改之前的onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
mGesture.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
if (getScrollY() < 0) {
mScroller.startScroll(0, getScrollY(), 0, -getScrollY());
invalidate();
}
View lastChild = getChildAt(getChildCount() - 1);
int bottomY = (int) (lastChild.getY() + lastChild.getMeasuredHeight() - screenHeight);
if (getScrollY() > bottomY) {
mScroller.startScroll(0, getScrollY(), 0, bottomY - getScrollY());
invalidate();
}
}
return true;
}
另外不要忘了重寫(xiě)computeScroll
方法
@Override
public void computeScroll() {
//判斷滾動(dòng)時(shí)候停止
if (mScroller.computeScrollOffset()) {
//滾動(dòng)到指定的位置
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
//這句話(huà)必須寫(xiě),否則不能實(shí)時(shí)刷新
postInvalidate();
}
}
剩下的問(wèn)題就是慣性滑動(dòng)的添加了料滥,這里需要用到VelocityTracker
這個(gè)類(lèi)來(lái)計(jì)算速度然眼,然后表現(xiàn)到滑動(dòng)上面,再修改onTouchEvent方法
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
mGesture.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
mVelocityTracker.computeCurrentVelocity(1000, maxFlingVelocity);
float velocityX = mVelocityTracker.getXVelocity(event.getPointerId(0));
float velocityY = mVelocityTracker.getYVelocity(event.getPointerId(0));
completeMove(-velocityX, -velocityY);
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
if (getScrollY() < 0) {
mScroller.startScroll(0, getScrollY(), 0, -getScrollY());
invalidate();
}
View lastChild = getChildAt(getChildCount() - 1);
int bottomY = (int) (lastChild.getY() + lastChild.getMeasuredHeight() - screenHeight);
if (getScrollY() > bottomY) {
mScroller.startScroll(0, getScrollY(), 0, bottomY - getScrollY());
invalidate();
}
}
return true;
}
最后完成慣性滑動(dòng)的方法封裝到completeMove方法中
private void completeMove(float v, float velocityY) {
View lastChild = getChildAt(getChildCount() - 1);
int bottomY = (int) (lastChild.getY() + lastChild.getMeasuredHeight() - screenHeight);
mScroller.fling(0, getScrollY(), 0, (int) (velocityY ), 0, getMeasuredWidth(), 0, bottomY);
invalidate();
}