android群英传笔记--第三章--自定义控件五粘性上下滑动scrollview

这次自定义的是ViewGroup 因此需要在onMeasure onLayout方法中为孩子测量大小 和指定位置。

测量每个孩子
@Override
    protected 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);
        }
    }
为每个孩子指定位置
@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(l, i * mScreenHeight,
                        r, (i + 1) * mScreenHeight);
            }
        }
    }
在onTouchEvent move 中根据scrollBy 移动 回弹动画 由 scrollTo(0, mScroller.getCurrY()) 控制
 @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        }
    }
最后是最难写的
@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:
                int dScrollY = checkAlignment();
                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;
    }
粘性控件,其任意一个子控件都可滑动停留,无论是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邮箱:1535514884@qq.com163邮箱:15889686524@163.comGmail邮箱:wkp15889686524@gmail.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值