Android自定义ViewGroup实现受边界限制的滚动操作(3)
上一篇文章《自定义viewgroup(2)》地址:https://www.nhooo.com/article/100610.htm
代码
packagecom.example.libingyuan.horizontallistview.ScrollViewGroup;
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.util.DisplayMetrics;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.WindowManager;
importandroid.widget.Scroller;
/**
*自定义ViewGroup
*在滚动的基础上,增加了边界限制
*/
publicclassScrollViewGroupextendsViewGroup{
//滚动计算辅助类
privateScrollermScroller;
//手指落点的X坐标
privatefloatmLastMotionX=0;
//屏幕宽度
privateintscreenWidth;
/**
*使用new关键字创建对象的时候调用
*@paramcontext上下文
*/
publicScrollViewGroup(Contextcontext){
this(context,null);
}
/**
*在XML文件中使用的时候调用
*@paramcontext上下文
*@paramattrs属性:如android:layout_width="wrap_content"
*/
publicScrollViewGroup(Contextcontext,AttributeSetattrs){
this(context,attrs,0);
}
/**
*在xml文件中调用,并且使用了自定义属性的时候调用
*@paramcontext上下文
*@paramattrs属性:如android:layout_width="wrap_content"
*@paramdefStyleAttr自定义属性的id
*/
publicScrollViewGroup(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
init(context);
}
/**
*初始化方法
*初始化滚动辅助类Scroller以及计算出屏幕宽度
*@paramcontext
*/
privatevoidinit(Contextcontext){
mScroller=newScroller(context);
WindowManagermanager=(WindowManager)context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetricsoutMetrics=newDisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
screenWidth=outMetrics.widthPixels;
}
/**
*滚动时需要重写的方法,用于控制滚动
*/
@Override
publicvoidcomputeScroll(){
//判断滚动时候停止
if(mScroller.computeScrollOffset()){
//滚动到指定的位置
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
//这句话必须写,否则不能实时刷新
postInvalidate();
}
}
/**
*手指触屏事件监听
*@paramevent
*@return
*/
@Override
publicbooleanonTouchEvent(MotionEventevent){
//TODOAuto-generatedmethodstub
intaction=event.getAction();
floatx=event.getX();
switch(action){
caseMotionEvent.ACTION_DOWN:
if(!mScroller.isFinished()){
mScroller.abortAnimation();
}
mLastMotionX=event.getX();
break;
caseMotionEvent.ACTION_MOVE:
floatdelt=mLastMotionX-x;
mLastMotionX=x;
scrollBy((int)delt,0);
break;
caseMotionEvent.ACTION_UP:
ViewlastChild=getChildAt(getChildCount()-1);
intfinalyChild=(int)(lastChild.getX()+lastChild.getWidth()-screenWidth);
if(getScrollX()<0){
scrollTo(0,0);
}
if(getScrollX()>=finalyChild)
scrollTo(finalyChild,0);
invalidate();
break;
default:
break;
}
returntrue;
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
//重新设置宽高
this.setMeasuredDimension(measureWidth(widthMeasureSpec,heightMeasureSpec),measureHeight(widthMeasureSpec,heightMeasureSpec));
}
/**
*测量宽度
*/
privateintmeasureWidth(intwidthMeasureSpec,intheightMeasureSpec){
//宽度
intsizeWidth=MeasureSpec.getSize(widthMeasureSpec);
intmodeWidth=MeasureSpec.getMode(widthMeasureSpec);
//父控件的宽(wrap_content)
intwidth=0;
intchildCount=getChildCount();
//重新测量子view的宽度,以及最大高度
for(inti=0;i<childCount;i++){
Viewchild=getChildAt(i);
measureChild(child,widthMeasureSpec,heightMeasureSpec);
MarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();
intchildWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
width+=childWidth;
}
returnmodeWidth==MeasureSpec.EXACTLY?sizeWidth:width;
}
/**
*测量高度
*/
privateintmeasureHeight(intwidthMeasureSpec,intheightMeasureSpec){
//高度
intsizeHeight=MeasureSpec.getSize(heightMeasureSpec);
intmodeHeight=MeasureSpec.getMode(heightMeasureSpec);
//父控件的高(wrap_content)
intheight=0;
intchildCount=getChildCount();
//重新测量子view的宽度,以及最大高度
for(inti=0;i<childCount;i++){
Viewchild=getChildAt(i);
measureChild(child,widthMeasureSpec,heightMeasureSpec);
MarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();
intchildHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;
height+=childHeight;
}
height=height/childCount;
returnmodeHeight==MeasureSpec.EXACTLY?sizeHeight:height;
}
/**
*给子布局设定位置
*/
@Override
protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
intchildLeft=0;//子View左边的间距
intchildWidth;//子View的宽度
intheight=getHeight();//屏幕的宽度
intchildCount=getChildCount();//子View的数量
for(inti=0;i<childCount;i++){
Viewchild=getChildAt(i);
MarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();
childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
child.layout(childLeft,0,childLeft+childWidth,height);
childLeft+=childWidth;
}
}
@Override
publicLayoutParamsgenerateLayoutParams(AttributeSetattrs){
returnnewMarginLayoutParams(getContext(),attrs);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。