实现一个底部上滑出的ViewGroup

在supportV4中有一个侧滑的View叫做DrawerLayout,支持左右滑动。最近项目中刚好有一个下向上滑动的组件需求,所以参考了一个改实现,打造了一个上下滑动的组件,发现代码简易,值得分享出来供大家参考。

需求功能分析

需求简述:实现一个滑动底部滑块,展开的容器控件,滑块自身内容自行定制。上滑动展开,下滑动关闭,点击开关进行对应的展开关闭。关键点:

  • 控件支持内容定制
  • 上下滑动进行对应的关闭、展开

需求实现

根据需求,我们可以简易列出实现过程中的几个关键点(辅助如图)
1、ViewGroup内部进行测量子控件;
2、外部提供一个关闭后的展开高度
3、提供开启关闭的回调
4、提供绑定滑动的区域

需求示意

编码

package com.owant.space.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.owant.space.R;

/**
 * created by Kyle.Zhong on 2020-04-07
 */

public class DrawerVerticalLayout extends FrameLayout {

    public interface LayoutOpenCloseListener {

        void onClosed();

        void onOpened();
    }

    private View shadowView;
    private ViewGroup slipBoxView;
    private View touchBar;
    private int closeHeight;
    private float slipBoxViewTransY = 0;
    public int height;
    public int minTop;
    public int maxTop;

    private boolean isClose = true;

    private LayoutOpenCloseListener layoutOpenCloseListener;

    public DrawerVerticalLayout(Context context) {
        this(context, null);
    }

    public DrawerVerticalLayout(Context context,
            @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawerVerticalLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context
                .obtainStyledAttributes(attrs, R.styleable.DrawerVerticalLayout);
        closeHeight = typedArray
                .getDimensionPixelSize(R.styleable.DrawerVerticalLayout_closeHeight, 200);
        typedArray.recycle();
    }


    public void setLayoutOpenCloseListener(
            LayoutOpenCloseListener layoutOpenCloseListener) {
        this.layoutOpenCloseListener = layoutOpenCloseListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        int childCount = getChildCount();
        if (childCount != 2) {
            throw new IllegalStateException("DrawerVerticalLayout必须添加两个子View");
        }

        shadowView = getChildAt(0);
        shadowView.setVisibility(INVISIBLE);
        slipBoxView = (ViewGroup) getChildAt(1);
        touchBar = findViewById(R.id.drawer_touch_bar);
        if (touchBar == null) {
            throw new IllegalStateException(
                    "DrawerVerticalLayout必须添加一个为R.id.drawer_touch_bar的拖动View");
        }
        touchBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return onTouchBar(v, event);
            }
        });
    }

    float currentY;
    float startTouchY;

    public boolean isClose() {
        return isClose;
    }

    public void setClose(boolean close) {
        if (close == isClose) {
            return;
        }
        if (close) {
            boxClose();
        } else {
            boxOpen();
        }
        isClose = close;
    }

    private boolean onTouchBar(View v, MotionEvent event) {
        int action = event.getAction();
        float rawY = event.getRawY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                currentY = rawY;
                slipBoxViewTransY = slipBoxView.getTranslationY();
                startTouchY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                shadowView.setVisibility(VISIBLE);
                float dY = rawY - currentY;
                float resultY = slipBoxViewTransY + dY;
                if (resultY <= minTop) {
                    resultY = minTop;
                }
                if (resultY >= maxTop) {
                    resultY = maxTop;
                }
                slipBoxView.setTranslationY(resultY);
                break;
            case MotionEvent.ACTION_UP:
                currentY = rawY;
                if (currentY > startTouchY) {
                    isClose = true;
                    boxClose();
                } else {
                    isClose = false;
                    boxOpen();
                }

                break;
            default:
                break;
        }
        return true;
    }

    public void boxOpen() {
        ObjectAnimator openAnimator = ObjectAnimator
                .ofFloat(slipBoxView, "translationY", slipBoxViewTransY, minTop);
        long time = (long) ((slipBoxViewTransY - minTop) / ((maxTop - minTop) * 1.0) * 400L);
        openAnimator.setDuration(time);
        openAnimator.start();
        shadowView.setVisibility(VISIBLE);

        if (layoutOpenCloseListener != null) {
            layoutOpenCloseListener.onOpened();
        }
    }

    public void boxClose() {
        ObjectAnimator closeAnimator = ObjectAnimator
                .ofFloat(slipBoxView, "translationY", slipBoxViewTransY, maxTop);
        long time = (long) ((maxTop - slipBoxViewTransY) / ((maxTop - minTop) * 1.0) * 400L);
        closeAnimator.setDuration(time);
        closeAnimator.start();
        shadowView.setVisibility(INVISIBLE);

        if (layoutOpenCloseListener != null) {
            layoutOpenCloseListener.onClosed();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int boxViewHeight = slipBoxView.getMeasuredHeight();
        height = getMeasuredHeight();
        slipBoxViewTransY = 0;
        minTop = 0;
        maxTop = boxViewHeight - closeHeight;

        FrameLayout.LayoutParams layoutParams = (LayoutParams) slipBoxView.getLayoutParams();
        layoutParams.gravity = Gravity.BOTTOM;
        slipBoxView.setLayoutParams(layoutParams);

        if (isClose) {
            slipBoxView.setTranslationY(maxTop);
        } else {
            shadowView.setVisibility(VISIBLE);
        }
    }

}

测试布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


  <com.owant.space.view.DrawerVerticalLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/white"
      android:orientation="vertical"
      app:closeHeight="200dp"
      app:openMarginTop="20dp"
      >

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0f0f"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:orientation="vertical"
        >

      <Button
          android:id="@+id/drawer_touch_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="hallo"
          />

      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="hallo"
          />

    </LinearLayout>

  </com.owant.space.view.DrawerVerticalLayout>

</LinearLayout>

结论

主要使用了ObjectAnimator进行滑动,采用Id进行绑定滑动区域。使用起来便捷爽快。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个可拖动的ViewGroup,需要以下步骤: 1. 继承自ViewGroup类,实现onLayout方法和onTouchEvent方法。 2. 在onTouchEvent方法中,判断手势的类型,如果是按下事件,则记录下当前触摸点的坐标,如果是移动事件,则计算出移动的距离,并调用layout方法重新布局子View。 3. 在布局子View时,需要考虑到手指移动的距离,因此需要将子View的坐标加上手指移动的距离。 4. 可以通过设置子View的LayoutParams来实现View的位置移动,例如: ``` MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams(); layoutParams.leftMargin += deltaX; layoutParams.topMargin += deltaY; childView.setLayoutParams(layoutParams); ``` 5. 在onLayout方法中,需要根据子View的LayoutParams来确定子View的位置,例如: ``` for (int i = 0; i < getChildCount(); i++) { View childView = getChildAt(i); MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams(); int childLeft = layoutParams.leftMargin; int childTop = layoutParams.topMargin; int childRight = childLeft + childView.getMeasuredWidth(); int childBottom = childTop + childView.getMeasuredHeight(); childView.layout(childLeft, childTop, childRight, childBottom); } ``` 6. 如果需要支持边界限制,可以在移动子View的过程中,判断子View是否超出了ViewGroup的边界,如果超出了,则将子View的位置限制在边界内。 7. 最后,需要在ViewGroup的构造方法中将其设置为可点击,否则无法接收到手势事件,例如: ``` public MyViewGroup(Context context, AttributeSet attrs) { super(context, attrs); setClickable(true); } ``` 通过以上步骤,就可以实现一个可拖动的ViewGroup了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值