美文网首页Android潜修者第三方控件Android UI的收集
向左拖拽跳转至“更多页面”的通用控件

向左拖拽跳转至“更多页面”的通用控件

作者: xufang2 | 来源:发表于2016-12-03 00:22 被阅读2109次

Github

https://github.com/uin3566/DragFooterView

效果图

douban.png
这种体验中规中矩,但我还是觉得向左拖拽比较。。于是就写下了本项目,为这种向左拖拽的交互方式提供一种通用的解决方案

介绍

作为一种library,怎么可以只有以上这一种效果呢。因此,可插拔的Footer View是必须的。
下面是我定制的另外两种效果,当然,你也可以定制任意你想要的效果。


上图中的字符串转Path功能用的是秋百万大神的android-Ultra-Pull-To-Refresh下拉刷新库中的类StoreHousePath,只不过将字符串从横向改为了竖向,在此深表感谢,顺便膜拜下。

涉及的知识点

本质上说,这个控件是一个自定义的ViewGroup,需要支持左滑,需要支持可插拔化的Footer View,所以View的事件分发,View的绘制这两个知识点都必须用到。
View的绘制就不多说了,无非是Canvas,Paint,Path结合动画的用法,GcsSloop魔法师的文章讲解的又好逼格又高,推荐下,具体的绘制可以看我的代码,自以为写的还是蛮清晰的e。
这里看下事件分发,由于DragContainer支持放置了各种子View,比如Button,RecyclerView,HorizontalScrollView等。那么,有时候需要子控件响应事件,有时候需要DragContainer响应拖拽。本项目通过重写dispatchTouchEvent实现需求,代码分析详见代码中的注释

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //若复位动画正在执行,则直接return
        if (resetAnimator != null && resetAnimator.isRunning()) {
            return super.dispatchTouchEvent(event);
        }

        //调用ViewGroup的dispatchTouchEvent方法,将
        //事件分发给子View。
        super.dispatchTouchEvent(event);

        //调用IDragChecker接口的canDrag方法判断此时DragContainer 
        //的可拖拽状态,本项目提供了默认实现类DefaultDragChecker 
        //将根据子View的类型来返回一个boolean。若返回false,
        //则该方法直接返回,返回true才能执行后续的拖拽操作。
        //以RecyclerView为例,若RecyclerView没有滑动到最底部
        //则返回false,否则返回true,DragContainer将会对后续事件
        //进行处理。
        if (!dragChecker.canDrag(contentView)) {
            return true;
        }

        //DragContainer处理拖拽事件
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dragDx = 0;
                downX = event.getX();
                downY = event.getY();
                lastMoveX = downX;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(event.getX() - downX);
                float dy = Math.abs(event.getY() - downY);
                //dx >= dy时判定为横向拖动,不允许parentViewGroup
                //拦截事件,否则使之拦截事件。
                if (dx >= dy) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }

                //若此时往左拖动,则开始处理ACTION_MOVE事件
                if (dragDx <= 0 && dragChecker.canDrag(contentView)) {
                    //这里更新拖拽状态,Footer View的绘制依赖于该状态
                    //该函数判断此时是往左拖拽还是往右拖拽。
                    updateDragState(event);
                    if (dragDx != 0) {
                        //拖拽时发送ACTION_CANCEL给子View,取消View的事件序列,否则View将与DragContainer产生事件冲突。
                        sendCancelEvent(event);
                    }
                    dragDx = event.getX() - downX;
                    //dragDamp是拖拽阻尼,取值范围是(0,1]。值越小,阻尼越大
                    float realDragDistance = dragDx * dragDamp;
                    //更新子View的位置。
                    setContentView((int) realDragDistance, 0, containerWidth + (int) realDragDistance, containerHeight);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                //手指抬起时或接收到ACTION_CANCEL时子View恢复原位。
                resetContentView();
                break;
        }
        return true;
    }

用法

1、添加依赖

  • step1:Add it in your root build.gradle at the end of repositories:
    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
  • step2:Add the dependency:
    dependencies {
        compile 'com.github.uin3566:DragFooterView:v1.0.2'
    }

2、在xml中配置如下 (注意:DragContainer只能有一个子View)

    <com.fangxu.library.DragContainer
        android:id="@+id/drag_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white" />
    </com.fangxu.library.DragContainer>

3、在java类中添加事件监听器DragListener

    DragContainer dragContainer = (DragContainer) findViewById(R.id.drag_image_view);
    
    //若需使用自己定制的footer,需要调用DragContainer的setFooterDrawer方法设置定制的footer类,如下
    //其中ArrowPathFooterDrawer继承于BaseFooterDrawer
    dragContainer.setFooterDrawer(new ArrowPathFooterDrawer.Builder(this, 0xff444444).setPathColor(0xffffffff).build());
    
    dragContainer.setDragListener(new DragListener() {
        @Override
        public void onDragEvent() {
            //do whatever you want,for example skip to the load more Activity.
            Intent intent = new Intent(HomeActivity.this, ShowMoreActivity.class);
            startActivity(intent);
        }
    });

可自定义属性

attribute value type defalut value description
dc_footer_color color 0xffcdcdcd footer view的背景颜色
dc_reset_animator_duration integer 700 松开拖拽后复位动画的时长
dc_drag_damp float 0.5f 拖拽阻尼系数,取值在(0,1]之间,取值越小,阻尼越大

其余footer view的属性参数交给BaseFooterDrawer的实现类自己设置。

小结

木有小结,我就是来打广告的,哈哈,如果你觉得本项目对你的学习有帮助,具有一定的实用价值,那就多多star,多多follow吧!
最后再厚颜无耻地再贴一次github地址:https://github.com/uin3566/DragFooterView

相关文章

  • 向左拖拽跳转至“更多页面”的通用控件

    Github https://github.com/uin3566/DragFooterView 效果图 项目来源...

  • iOS中xib文件维护使用小结

    最近一直在做项目维护,由于项目比较大,开发时间比较早,早期的很多页面都是用xib拖拽页面控件。简单的页面还好,详情...

  • iOS xib报错 "could not insert new

    我们在使用xib编辑页面的时候,通过Ctrl+拖拽的方式拖住控件常会遇到以下错误:Could not insert...

  • Flutter-26- Draggable控件

    提供了强大的拖拽控件,可以灵活定制 Draggable Widget Draggable控件负责就是拖拽,父层使用...

  • 3

    结构 1:定义一个半透明的Activity 2:使控件可以拖拽,并记录下拖拽后控件的位置

  • 控件拖拽

    JavaScript实现最简单的拖拽效果 HTML5 drag & drop 拖拽与拖放简介 基于HTML5 dr...

  • 拖拽控件

    为了复习View的位置坐标,今天做一个可以在父布局内随意拖拽的小控件,值得一提的是,这个可以根据父布局来调整整体的...

  • React-Navigation(二),goBack的使用

    假设 假设有三个页面A、B、C,栈中是A->B->C,即A页面跳转至B页面,B页面跳转至C页面;(现实场景中的例子...

  • HTML5 input 新增的表单控件

    必填项验证 颜色选择控件 日期选择控件 时间选择控件 电子邮件控件:提交表单时有格式验证 数字控件 文件控件 拖拽...

  • iOS 侧滑返回到rootViewController

    有时候的需求是从rootController跳转至页面A再跳转至页面B, 然后从B返回时直接返回到rootCont...

网友评论

  • Yang_Bob:拉拽的时候“释放查看”“查看更多”文案哪里可以自定义?
  • Jlanglang:可以试试用viewdragheple写。
    xufang2:@Jlanglang 嗯,后续研究下ViewDragHelper,下拉刷新库貌似都用的这个。
  • uncochen:思路不错,star一下
  • a0a42f8c37f5:看起来蛮不错的,但想问下,为什么不用recycleview呢。。。
    a0a42f8c37f5:@晓月久群 错了,是recycleview,Xrecycleview是一个下拉和上滑控件
    a0a42f8c37f5:@xufang2 是啊,而且xrecycleview有很多下拉和上拉加载更多的组件,设置recycleview水平方向就行了
    xufang2:@晓月久群 你是说直接用一个recyclerview来做这种效果么。
  • 1cfacec6fd5b:虽然我不会安卓,但感觉很厉害
  • 9b9f1d688a7f:👏👏👏
  • 291572210874:安卓的?
    xufang2:@cgzo 对的

本文标题:向左拖拽跳转至“更多页面”的通用控件

本文链接:https://www.haomeiwen.com/subject/fxdqmttx.html