700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 自定义控件ViewGroup上下滑动

自定义控件ViewGroup上下滑动

时间:2022-07-24 21:05:11

相关推荐

自定义控件ViewGroup上下滑动

自定义ViewGroup,就是组合型控件,ViewGroup不需要调用ONDraw()方法,因为ViewGroup就是View的容器,每一个View是其的子类,只需要将创建好的View添加到ViewGroup控件当中即可。

在自定义ViewGroup控件,首先确定是否从Layout(布局文件中)调用,就一定要调用该方法,用于实例化从layout布局文件中加载组件。

public MyViewGroup(Context context, AttributeSet attrs) {super(context, attrs);}

如果在子布局文件中添加有新的组件,比如Button,ImageView,ListView 等组件,必然自定义类会调用该方法,该方法用于测绘在子控件中子组件的位置。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);

ViewGroup添加到组件,就会在该方法中确定组件的位置,该方法描述的就是子控件在父控件中的位置大小,信息等。

protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

效果图:实现上下滑动效果

说了那么多,下面直接上代码:

这是向ViewGroup类对象中添加对象ImageView

package com.example.demod3;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.widget.ImageView;public class MainActivity extends Activity {//图片资源文件private int[] imgs = new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);MyViewGroup root = (MyViewGroup) findViewById(R.id.myViewGroup);for(int i =0; i < imgs.length; i++){ImageView img = new ImageView(this);img.setImageResource(imgs[i]);root.addView(img);}//测试//给自定义ViewGroup添加测试布局// View temp = getLayoutInflater().inflate(R.layout.item_layout,null); //线性布局View temp = View.inflate(this,R.layout.item_layout,null);root.addView(temp);}}

新建自定义的MyViewGroup.class类,继承ViewGroup类

package com.example.demod3;import android.content.Context;import android.os.Build;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Scroller;public class MyViewGroup extends ViewGroup {private Scroller scroller;private int mScreenHeight; //屏幕的高度public MyViewGroup(Context context, AttributeSet attrs) {super(context, attrs);scroller = new Scroller(context);}//确定在父类中的位置@Overrideprotected void onLayout(boolean b, int i, int i1, int i2, int i3) {int count = getChildCount();MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams(); //设置viewGroup的高度mScreenHeight = mlp.height =getHeight()*count;setLayoutParams(mlp);for(int index =0 ; index< count; index++){View childView = getChildAt(index);if(childView.getVisibility() != GONE){childView.layout(i,index*getHeight(),i2,(index+1)*getHeight());}}//View firstView = getChildAt(0);// Log.i("GO","--------->"+firstView.getClass().getSimpleName());//firstView.layout(i,i1,i2,i3);}//测量控件的位置@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int count = getChildCount();for(int i =0; i < count; i++){View childView = getChildAt(i);measureChild(childView,widthMeasureSpec,heightMeasureSpec);//childView.measure(widthMeasureSpec,heightMeasureSpec);//Log.i("GO","--------->"+childView.getClass().getSimpleName());}}private int mLastY = 0;private int mStart = 0;private int mEnd = 0;@Overridepublic boolean onTouchEvent(MotionEvent event) {int y = (int) event.getY();switch (event.getAction()){case MotionEvent.ACTION_DOWN:mLastY = y;mStart = getScrollY(); //记录开始位置break;case MotionEvent.ACTION_MOVE:if(!scroller.isFinished()){scroller.abortAnimation();}int dy = mLastY - y;if(getScaleY() < 0){dy = 0;}if(getScaleY()>getHeight()){dy = 0;}// Log.i("GO","dy ------->"+dy+" Y "+y+" t " +t+" getHeight "+getHeight()+" ===height "+mScreenHeight);scrollBy(0,dy);mLastY = y;break;case MotionEvent.ACTION_UP:mEnd = getScrollY();int dScrollY = mEnd - mStart;if(dScrollY >0){if(dScrollY < getHeight()/3){scroller.startScroll(0,getScrollY(),0,-dScrollY);}else {scroller.startScroll(0,getScrollY(),0,getHeight()-dScrollY);}}else {if(-dScrollY < getHeight()/3){scroller.startScroll(0,getScrollY(),0,-dScrollY);}else {scroller.startScroll(0,getScrollY(),0,-getHeight()-dScrollY);}}break;}postInvalidate();return true;}@Overridepublic void computeScroll() {puteScroll();if(puteScrollOffset()){scrollTo(0,scroller.getCurrY());Log.i("GO","dy ------->"+scroller.getCurrY()+" "+scroller.getCurrX());postInvalidate();}}}

布局文件就不在展示了,在你能看懂这样的程序之前,那么你一定会有一定的基础与能力知识。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。