onTouchEvent(二) 使用Scroller实现黏性滑动的ScrollView

本文为楼主原创,转载请表明出处:http://blog.csdn.net/suma_sun/article/details/52900476
上一篇博文介绍了onTouchEvent()手势控制相关的一些坐标概念,这篇文章结合上一篇内容加上Scroller实现一个简单的带黏性滑动的ScrollView。

这里写图片描述

思路

  1. 滑动的时候判断最终偏移量是不是超出了子view(ViewGroup内容)的范围,如果超出了就限制不让其超出,这样就可以做出一个滑动的ScrollView并且不会滑出界
  2. 当松手(action_up)的时候判断当前偏移量在该子view的什么位置,如果小于三分之一就滑动回该view的头部,如果超出三分之二就滑动到下个view

首先看看我们需要做些什么,自定义View要复写的方法有好几个,我们要复写的有:
1. onMeasure()
2. onLayout()
3. onTouchEvent()
4. computeScroll()

因为是ViewGroup需要计算、定位子View所以需要复写1和2两个函数。
3不用说了,要实现滑动功能必须要实现的函数。
4这是配合Scroller使用的一个函数。

首先介绍下Scroller这个类,这个类是一个工具,并不是实际UI操作。这个工具提供的功能是提供差值计算,就像属性动画一样,要做一个平滑过渡的动画需要用到差值器来辅助提供偏移差值。

下面是一个不使用Scroller的Demo。
这里写图片描述

差别很明显。

onMeasure

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //遍历子View,测量子View
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

onMeasure()没什么好说的,遍历子view,测量子view。

onLayout

 @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        这里的四个坐标都是以ViewGroup来参照的,而不是屏幕的原点 
//        top默认0
//        left默认0
//        right默认屏幕宽度,ViewGroup的布局mactch_parent
//        bottom默认屏幕高度,ViewGroup的布局mactch_parent
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            //这个demo模仿垂直的ScrollView所以就没有累计宽度
            int right = view.getMeasuredWidth();
            //设置子View摆放位置
            //mMaxHeight初始化为0
            view.layout(l , mMaxHeight, right, mMaxHeight + view.getMeasuredHeight());
            mMaxHeight += view.getMeasuredHeight();
            //mHeights是一个集合,用于保存每个自view的高度,用于粘性滑动判断
            mHeights.add(mMaxHeight);
            if(right > mMaxWidth){
                mMaxWidth = right;
            }
        }
    }

要说明的都已经在注释上了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
粘性控件,其任意一个子控件都可滑动停留,无论是View,还是ViewGroup;用该控件可以轻松实现支付宝"全部应用"界面。演示图  Note:图1为设置属性wkp_canScrollToEndViewTop=true,图2没有;图3为设置滑动改变监听。Gradle集成dependencies{       compile 'com.wkp:StickLayout:1.0.6'       //Android Studio3.0 可用以下方式       //implementation 'com.wkp:StickLayout:1.0.6' } //如不愿意等待,请加上我的maven仓库地址 maven { url "https://dl.bintray.com/wkp/maven" }Note:可能存在Jcenter还在审核阶段,这时会集成失败!注意SDK版本targetSdkVersion >= 26.使用详解属性讲解<!--是否粘性停留(用于直接子控件)-->         <attr name="wkp_stick" format="boolean"/>         <!--是否开启滑动到最后一个控件的顶部,默认不开启(用于控件本身),注意最后一个子控件如果为条目控件时,如ListView,请不要开启-->         <attr name="wkp_canScrollToEndViewTop" format="boolean"/>Note:每个属性都有对应的java设置代码!布局<?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"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <TextView         android:clickable="true"         android:onClick="addView"         android:gravity="center"         android:padding="5dp"         android:text="添加条目"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>         <!--app:wkp_canScrollToEndViewTop="true"-->     <com.wkp.sticklayout_lib.widget.StickLayout         android:id="@ id/sl"         android:layout_width="match_parent"         android:layout_height="wrap_content">         <TextView             android: id/tv1"             android:text="第1行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <LinearLayout             app:wkp_stick="true"             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="40dp">             <TextView                 android:onClick="scrollTo"                 android:background="@android:color/holo_blue_light"                 android:text="NUM2"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo3"                 android:background="@android:color/holo_green_light"                 android:text="NUM3"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo4"                 android:background="@android:color/holo_red_light"                 android:text="NUM4"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo7"                 android:background="@android:color/holo_orange_light"                 android:text="NUM7"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>         </LinearLayout>         <TextView             android:id="@ id/tv2"             android:text="第2行"             android:background="@android:color/holo_blue_light"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv3"             app:wkp_stick="true"             android:text="第3行"             android:background="@android:color/holo_green_light"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:background="@android:color/holo_red_light"             android:id="@ id/tv4"             android:text="第4行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv5"             android:text="第5行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv6"             android:text="第6行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:background="@android:color/holo_orange_light"             android:id="@ id/tv7"             android:text="第7行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>     </com.wkp.sticklayout_lib.widget.StickLayout> </LinearLayout>Note:ScrollView嵌套StickLayout时事件被拦截,无效果!StickLayout嵌套如ListView的条目控件时会只显示第一行,注意解决!代码示例public class MainActivity extends AppCompatActivity {     private StickLayout mSl;     private TextView mTv2;     private View mTv3;     private View mTv7;     private View mTv4;     private int currentPosition = -1;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         mSl = findViewById(R.id.sl);         mTv2 = findViewById(R.id.tv2);         mTv3 = findViewById(R.id.tv3);         mTv4 = findViewById(R.id.tv4);         mTv7 = findViewById(R.id.tv7); //        mSl.setStickView(findViewById(R.id.tv2)); //设置粘性控件 //        mSl.setStickView(findViewById(R.id.tv3)); //        mSl.canScrollToEndViewTop(true);      //设置是否开启最后控件滑动到顶部         //设置滑动改变监听(一滑动就会有回调)         mSl.setOnScrollChangeListener(new StickLayout.OnScrollChangeListener() {             @Override             public void onScrollChange(StickLayout v, View currentView, int position, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {                 //直到当前控件改变在做事情                 if (currentPosition != position) {                     Toast.makeText(v.getContext(), ((TextView) currentView).getText().toString(), Toast.LENGTH_SHORT).show();                     currentPosition = position;                 }             }         });     }     public void addView(View view) {         TextView textView = new TextView(view.getContext());         textView.setGravity(Gravity.CENTER);         textView.setPadding(10, 10, 10, 10);         textView.setText("新条目");         mSl.addView(textView, 0);     }     public void scrollTo2(View view) {         //滑动到指定子控件         mSl.scrollToView(mTv2);     }     public void scrollTo3(View view) {         mSl.scrollToView(mTv3);     }     public void scrollTo4(View view) {         mSl.scrollToView(mTv4);     }     public void scrollTo7(View view) {         mSl.scrollToView(mTv7);     } }Note:还有其他API请根据需要自行参考!寄语控件支持直接代码创建,还有更多API请观看StickLayout.java内的注释说明。欢迎大家使用,感觉好用请给个Star鼓励一下,谢谢!大家如果有更好的意见或建议以及好的灵感,请邮箱作者,谢谢!QQ邮箱:[email protected]邮箱:[email protected]邮箱:[email protected]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值