首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >android-自定义控件-自定义ViewGroup

android-自定义控件-自定义ViewGroup

作者头像
圆号本昊
发布2021-09-24 12:04:10
发布2021-09-24 12:04:10
5160
举报
文章被收录于专栏:github@hornhuanggithub@hornhuang

参考自--> Android群英传

项目地址-->书中自定义 view 汇总:https://github.com/FishInWater-1999/DesighViewText


确定 ViewGroup 的高度,并生成 Scroller 对象

代码语言:javascript
复制
    private void initView(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mScreenHeight = dm.heightPixels;
        mScroller = new Scroller(context);
    }

确定子 View 的位置

代码语言:javascript
复制
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        // 设置 ViewGroup 的高度
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mScreenHeight * childCount;
        setLayoutParams(mlp);
        for (int i = 0 ; i < childCount ; i++ ){
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE){
                child.layout(1, i * mScreenHeight,
                        r, (i + 1) * mScreenHeight);
            }
        }
    }

粘性实现滑动

代码语言:javascript
复制
    @Override
    public 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 (!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if (getScrollY() < 0){
                    dy = 0;
                }
                if (getScrollY() > getHeight() - mScreenHeight){
                    dy = 0;
                }
                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if (dScrollY > 0){
                    if (dScrollY < mScreenHeight / 3){
                        mScroller.startScroll(
                                0 , getScrollY(),
                                0, -dScrollY
                        );
                    }else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, mScreenHeight - dScrollY
                        );
                    }
                }else {
                    if (-dScrollY < mScreenHeight / 3){
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY
                        );
                    }else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -mScreenHeight - dScrollY
                        );
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

完整代码如下:

代码语言:javascript
复制
/**
 * 定义 ViewGroup 实现类似:ScrollView 的功能
 */
public class ViewGroup_3_7 extends ViewGroup {
    /*
    数据成员
     */
    private int mScreenHeight = 0;
    private int childCount = 0;
    private int mLastY = 0;
    private int mStart = 0;
    private int mEnd = 0;
    private Scroller mScroller ;


    /*
    构造方法
     */
    public ViewGroup_3_7(Context context) {
        this(context, null);
    }

    public ViewGroup_3_7(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewGroup_3_7(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    /*
    确定 ViewGroup 的高度
     */
    private void initView(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mScreenHeight = dm.heightPixels;
        mScroller = new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for ( int i = 0 ; i < count ; i++ ){
            View childViews = getChildAt(i);
            measureChild(childViews, widthMeasureSpec, heightMeasureSpec);
        }
    }

    /*
    确定子 View 的位置
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        // 设置 ViewGroup 的高度
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mScreenHeight * childCount;
        setLayoutParams(mlp);
        for (int i = 0 ; i < childCount ; i++ ){
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE){
                child.layout(1, i * mScreenHeight,
                        r, (i + 1) * mScreenHeight);
            }
        }
    }

    /*
    粘性实现滑动
     */
    @Override
    public 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 (!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if (getScrollY() < 0){
                    dy = 0;
                }
                if (getScrollY() > getHeight() - mScreenHeight){
                    dy = 0;
                }
                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if (dScrollY > 0){
                    if (dScrollY < mScreenHeight / 3){
                        mScroller.startScroll(
                                0 , getScrollY(),
                                0, -dScrollY
                        );
                    }else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, mScreenHeight - dScrollY
                        );
                    }
                }else {
                    if (-dScrollY < mScreenHeight / 3){
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY
                        );
                    }else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -mScreenHeight - dScrollY
                        );
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    /*

     */
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()){
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考自--> Android群英传
    • 确定 ViewGroup 的高度,并生成 Scroller 对象
    • 确定子 View 的位置
    • 粘性实现滑动
    • 完整代码如下:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档