`

NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端

阅读更多
网上有一个StickyScrollView,称之为粘性ScrollView,比如一个垂直方向的布局,依次摆放几个子View,当某一个子View滚到到顶端时候要停靠在顶部,悬停在顶部的位置不动。这很像联系人的pinned,假设联系人按照Z分组后,若滚动到顶部,Z就悬停靠在顶端不动,下面到的联系人继续滚到。当用户下拉时候,又恢复正常。

StickyScrollView原先是继承自ScrollView,然而现在ScrollView已经被新型的NestedScrollView逐渐取代了,现在我把它改写成继承自NestedScrollView后,整理出来:

package zhangphil.test;
 
import java.util.ArrayList;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
 
public class StickyScrollView extends NestedScrollView {
 
    /**
     * Tag for views that should stick and have constant drawing. e.g. TextViews, ImageViews etc
     */
    public static final String STICKY_TAG = "sticky";
 
    /**
     * Flag for views that should stick and have non-constant drawing. e.g. Buttons, ProgressBars etc
     */
    public static final String FLAG_NONCONSTANT = "-nonconstant";
 
    /**
     * Flag for views that have aren't fully opaque
     */
    public static final String FLAG_HASTRANSPARANCY = "-hastransparancy";
 
    /**
     * Default height of the shadow peeking out below the stuck view.
     */
    private static final int DEFAULT_SHADOW_HEIGHT = 10; // dp;
 
    private ArrayList<View> stickyViews;
    private View currentlyStickingView;
    private float stickyViewTopOffset;
    private int stickyViewLeftOffset;
    private boolean redirectTouchesToStickyView;
    private boolean clippingToPadding;
    private boolean clipToPaddingHasBeenSet;
 
    private int mShadowHeight;
    private Drawable mShadowDrawable;
 
    private final Runnable invalidateRunnable = new Runnable() {
 
        @Override
        public void run() {
            if (currentlyStickingView != null) {
                int l = getLeftForViewRelativeOnlyChild(currentlyStickingView);
                int t = getBottomForViewRelativeOnlyChild(currentlyStickingView);
                int r = getRightForViewRelativeOnlyChild(currentlyStickingView);
                int b = (int) (getScrollY() + (currentlyStickingView.getHeight() + stickyViewTopOffset));
                invalidate(l, t, r, b);
            }
            postDelayed(this, 16);
        }
    };
 
    public StickyScrollView(Context context) {
        this(context, null);
    }
 
    public StickyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.scrollViewStyle);
    }
 
    public StickyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup();
 
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.StickyScrollView, defStyle, 0);
 
        final float density = context.getResources().getDisplayMetrics().density;
        int defaultShadowHeightInPix = (int) (DEFAULT_SHADOW_HEIGHT * density + 0.5f);
 
        mShadowHeight = a.getDimensionPixelSize(
                R.styleable.StickyScrollView_stuckShadowHeight,
                defaultShadowHeightInPix);
 
        int shadowDrawableRes = a.getResourceId(
                R.styleable.StickyScrollView_stuckShadowDrawable, -1);
 
        if (shadowDrawableRes != -1) {
            mShadowDrawable = context.getResources().getDrawable(
                    shadowDrawableRes);
        }
 
        a.recycle();
    }
 
    /**
     * Sets the height of the shadow drawable in pixels.
     *
     * @param height
     */
    public void setShadowHeight(int height) {
        mShadowHeight = height;
    }
 
 
    public void setup() {
        stickyViews = new ArrayList<View>();
    }
 
    private int getLeftForViewRelativeOnlyChild(View v) {
        int left = v.getLeft();
        while (v.getParent() != getChildAt(0)) {
            v = (View) v.getParent();
            left += v.getLeft();
        }
        return left;
    }
 
    private int getTopForViewRelativeOnlyChild(View v) {
        int top = v.getTop();
        while (v.getParent() != getChildAt(0)) {
            v = (View) v.getParent();
            top += v.getTop();
        }
        return top;
    }
 
    private int getRightForViewRelativeOnlyChild(View v) {
        int right = v.getRight();
        while (v.getParent() != getChildAt(0)) {
            v = (View) v.getParent();
            right += v.getRight();
        }
        return right;
    }
 
    private int getBottomForViewRelativeOnlyChild(View v) {
        int bottom = v.getBottom();
        while (v.getParent() != getChildAt(0)) {
            v = (View) v.getParent();
            bottom += v.getBottom();
        }
        return bottom;
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!clipToPaddingHasBeenSet) {
            clippingToPadding = true;
        }
        notifyHierarchyChanged();
    }
 
    @Override
    public void setClipToPadding(boolean clipToPadding) {
        super.setClipToPadding(clipToPadding);
        clippingToPadding = clipToPadding;
        clipToPaddingHasBeenSet = true;
    }
 
    @Override
    public void addView(View child) {
        super.addView(child);
        findStickyViews(child);
    }
 
    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
        findStickyViews(child);
    }
 
    @Override
    public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        findStickyViews(child);
    }
 
    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        findStickyViews(child);
    }
 
    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        findStickyViews(child);
    }
 
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (currentlyStickingView != null) {
            canvas.save();
            canvas.translate(getPaddingLeft() + stickyViewLeftOffset, getScrollY() + stickyViewTopOffset + (clippingToPadding ? getPaddingTop() : 0));
 
            canvas.clipRect(0, (clippingToPadding ? -stickyViewTopOffset : 0),
                    getWidth() - stickyViewLeftOffset,
                    currentlyStickingView.getHeight() + mShadowHeight + 1);
 
            if (mShadowDrawable != null) {
                int left = 0;
                int right = currentlyStickingView.getWidth();
                int top = currentlyStickingView.getHeight();
                int bottom = currentlyStickingView.getHeight() + mShadowHeight;
                mShadowDrawable.setBounds(left, top, right, bottom);
                mShadowDrawable.draw(canvas);
            }
 
            canvas.clipRect(0, (clippingToPadding ? -stickyViewTopOffset : 0), getWidth(), currentlyStickingView.getHeight());
            if (getStringTagForView(currentlyStickingView).contains(FLAG_HASTRANSPARANCY)) {
                showView(currentlyStickingView);
                currentlyStickingView.draw(canvas);
                hideView(currentlyStickingView);
            } else {
                currentlyStickingView.draw(canvas);
            }
            canvas.restore();
        }
    }
 
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            redirectTouchesToStickyView = true;
        }
 
        if (redirectTouchesToStickyView) {
            redirectTouchesToStickyView = currentlyStickingView != null;
            if (redirectTouchesToStickyView) {
                redirectTouchesToStickyView =
                        ev.getY() <= (currentlyStickingView.getHeight() + stickyViewTopOffset) &&
                                ev.getX() >= getLeftForViewRelativeOnlyChild(currentlyStickingView) &&
                                ev.getX() <= getRightForViewRelativeOnlyChild(currentlyStickingView);
            }
        } else if (currentlyStickingView == null) {
            redirectTouchesToStickyView = false;
        }
        if (redirectTouchesToStickyView) {
            ev.offsetLocation(0, -1 * ((getScrollY() + stickyViewTopOffset) - getTopForViewRelativeOnlyChild(currentlyStickingView)));
        }
        return super.dispatchTouchEvent(ev);
    }
 
    private boolean hasNotDoneActionDown = true;
 
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (redirectTouchesToStickyView) {
            ev.offsetLocation(0, ((getScrollY() + stickyViewTopOffset) - getTopForViewRelativeOnlyChild(currentlyStickingView)));
        }
 
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            hasNotDoneActionDown = false;
        }
 
        if (hasNotDoneActionDown) {
            MotionEvent down = MotionEvent.obtain(ev);
            down.setAction(MotionEvent.ACTION_DOWN);
            super.onTouchEvent(down);
            hasNotDoneActionDown = false;
        }
 
        if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
            hasNotDoneActionDown = true;
        }
 
        return super.onTouchEvent(ev);
    }
 
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        doTheStickyThing();
    }
 
    private void doTheStickyThing() {
        View viewThatShouldStick = null;
        View approachingView = null;
        for (View v : stickyViews) {
            int viewTop = getTopForViewRelativeOnlyChild(v) - getScrollY() + (clippingToPadding ? 0 : getPaddingTop());
            if (viewTop <= 0) {
                if (viewThatShouldStick == null || viewTop > (getTopForViewRelativeOnlyChild(viewThatShouldStick) - getScrollY() + (clippingToPadding ? 0 : getPaddingTop()))) {
                    viewThatShouldStick = v;
                }
            } else {
                if (approachingView == null || viewTop < (getTopForViewRelativeOnlyChild(approachingView) - getScrollY() + (clippingToPadding ? 0 : getPaddingTop()))) {
                    approachingView = v;
                }
            }
        }
        if (viewThatShouldStick != null) {
            stickyViewTopOffset = approachingView == null ? 0 : Math.min(0, getTopForViewRelativeOnlyChild(approachingView) - getScrollY() + (clippingToPadding ? 0 : getPaddingTop()) - viewThatShouldStick.getHeight());
            if (viewThatShouldStick != currentlyStickingView) {
                if (currentlyStickingView != null) {
                    stopStickingCurrentlyStickingView();
                }
                // only compute the left offset when we start sticking.
                stickyViewLeftOffset = getLeftForViewRelativeOnlyChild(viewThatShouldStick);
                startStickingView(viewThatShouldStick);
            }
        } else if (currentlyStickingView != null) {
            stopStickingCurrentlyStickingView();
        }
    }
 
    private void startStickingView(View viewThatShouldStick) {
        currentlyStickingView = viewThatShouldStick;
        if (getStringTagForView(currentlyStickingView).contains(FLAG_HASTRANSPARANCY)) {
            hideView(currentlyStickingView);
        }
        if (((String) currentlyStickingView.getTag()).contains(FLAG_NONCONSTANT)) {
            post(invalidateRunnable);
        }
    }
 
    private void stopStickingCurrentlyStickingView() {
        if (getStringTagForView(currentlyStickingView).contains(FLAG_HASTRANSPARANCY)) {
            showView(currentlyStickingView);
        }
        currentlyStickingView = null;
        removeCallbacks(invalidateRunnable);
    }
 
    /**
     * Notify that the sticky attribute has been added or removed from one or more views in the View hierarchy
     */
    public void notifyStickyAttributeChanged() {
        notifyHierarchyChanged();
    }
 
    private void notifyHierarchyChanged() {
        if (currentlyStickingView != null) {
            stopStickingCurrentlyStickingView();
        }
        stickyViews.clear();
        findStickyViews(getChildAt(0));
        doTheStickyThing();
        invalidate();
    }
 
    private void findStickyViews(View v) {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                String tag = getStringTagForView(vg.getChildAt(i));
                if (tag != null && tag.contains(STICKY_TAG)) {
                    stickyViews.add(vg.getChildAt(i));
                } else if (vg.getChildAt(i) instanceof ViewGroup) {
                    findStickyViews(vg.getChildAt(i));
                }
            }
        } else {
            String tag = (String) v.getTag();
            if (tag != null && tag.contains(STICKY_TAG)) {
                stickyViews.add(v);
            }
        }
    }
 
    private String getStringTagForView(View v) {
        Object tagObject = v.getTag();
        return String.valueOf(tagObject);
    }
 
    private void hideView(View v) {
        if (Build.VERSION.SDK_INT >= 11) {
            v.setAlpha(0);
        } else {
            AlphaAnimation anim = new AlphaAnimation(1, 0);
            anim.setDuration(0);
            anim.setFillAfter(true);
            v.startAnimation(anim);
        }
    }
 
    private void showView(View v) {
        if (Build.VERSION.SDK_INT >= 11) {
            v.setAlpha(1);
        } else {
            AlphaAnimation anim = new AlphaAnimation(0, 1);
            anim.setDuration(0);
            anim.setFillAfter(true);
            v.startAnimation(anim);
        }
    }
}


同时需要在res/values/attr.xml加入定义的属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="StickyScrollView">
        <attr name="stuckShadowHeight" format="dimension" />
        <attr name="stuckShadowDrawable" format="reference" />
    </declare-styleable>
</resources>


使用起来很简单,在xml布局中把某一个子View增加一个属性:
android:tag="sticky"


即可实现该子View在NestedScrollView中,当滚动到顶部后悬停挂靠在顶部不动,直到下一个粘性子View滚动上来把它顶飞,或者用户下拉,拉出上面的View。如xml代码:
<?xml version="1.0" encoding="utf-8"?>
<zhangphil.test.StickyScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:tag="sticky"
            android:text="姓名"
            android:textColor="@android:color/white" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:background="@android:color/holo_blue_bright"
            android:gravity="center"
            android:text="zhang phil"
            android:textColor="@android:color/white" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:tag="sticky"
            android:text="blog"
            android:textColor="@android:color/white" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:background="@android:color/holo_orange_light"
            android:gravity="center"
            android:text="zhang phil csdn blog"
            android:textColor="@android:color/white" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:tag="sticky"
            android:text="链接"
            android:textColor="@android:color/white" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:background="@android:color/holo_purple"
            android:gravity="center"
            android:text="http://blog.csdn.net/zhangphil"
            android:textColor="@android:color/white" />
    </LinearLayout>
</zhangphil.test.StickyScrollView>



ViewPager与NestedScrollView套嵌会引起冲突,滑动不了的问题
https://blog.csdn.net/wjr1949/article/details/72479583?utm_source=itdadao&utm_medium=referral
分享到:
评论

相关推荐

    微信小程序点击item使之滚动到屏幕中间位置

    想做类似猫眼电影选场次会自动滚动到屏幕中间的效果 如图是最终效果  2.分析 实现这种效果的方法有三种: 1.直接使用原生CSS+js; 2.使用swiper,一屏显示多个item,点击item会自动滚动到中间 3.使用微信的scroll-view...

    电机定子和转子参数辨识

    首先,在电机静止状态下,通过在定子端施加电压脉冲,可以估算出泄漏电感和定子转子的直流电阻。接着,将功率转换器切换到电流控制模式,测量得到的定子电压数据用于估算定子电阻和互感。最后,在电机运行于不同速度...

    111_定子电流励磁分量_

    定子电流励磁分量是电机控制系统中的一个重要概念,尤其在交流电动机的矢量控制技术中占有核心地位。在现代工业自动化领域,精确控制电机的运行性能是至关重要的,而定子电流励磁分量的掌握对于实现这一目标至关重要...

    易语言复制组件到指定子夹

    在易语言中,“复制组件到指定子夹”是一个常见的操作,尤其在软件开发过程中,我们常常需要将组件文件复制到程序的特定目录,以便于程序的运行和更新。 首先,我们需要了解易语言中的“组件”。组件是具有特定功能...

    小型单端电机定子铁芯简易工装设备

    在介绍这种工装设备的设计思路、结构性能及工作过程之前,我们有必要对小型电机、单端电机及定子铁芯等概念进行梳理。 小型电机是指体积较小、功率较低的一类电机。它们广泛应用于家用电器、办公自动化设备、玩具...

    行业资料-电子功用-内转子式和外定子式的直流电动力机和单、双轮滚动式电刷的说明分析.rar

    总的来说,内转子式与外定子式的直流电机各有优劣,而单、双轮滚动式电刷在不同的应用场景中也有各自的优势。为了得到最佳的电机性能,设计师需要根据具体的应用场景和要求,综合考虑电机的结构设计与电刷的类型选择...

    定子叠加机原理设计说明书

    其中,Z4电机作为一种广泛应用于各类机械设备中的重要电机,其定子质量直接关系到电机运行的稳定性和可靠性。然而,传统的Z4电机定子制造工艺,依赖于手工叠压和焊接,工序繁多,流程缓慢,效率低,且产品质量难以...

    MATLAB在双作用叶片泵定子曲线中的应用

    MATLAB在叶片泵定子曲线设计中的应用 叶片泵作为一种常见的液压元件,广泛应用于工业生产及各种机械设备中。定子内表面的过渡曲线对于叶片泵的性能有着直接的影响,其中排量、输出流量脉动、冲击振动、噪声、效率和...

    UGUI循环滚动+滑动居中

    支持横向纵向循环滚动和滑动居中,支持任意锚点布局(没有锚点限制),3种居中效果(不回弹,回弹,双向回弹)Grid(布局)不依赖任何脚本可以单独使用,不锁定子物体随意排序。WrapCententy(循环+居中)

    永磁同步电机定子电流波动故障机理分析

    然而,电机在实际运行过程中,定子电流的幅值波动是一个需要重点关注的问题,因为它将直接影响到电机及驱动系统的稳定性和可靠性。 首先,我们要了解定子电流波动对电机性能的影响。电流波动会引起驱动电路产生负序...

    电子功用-定子冲片、定子铁芯、定子和电机

    总结所述,"电子功用-定子冲片、定子铁芯、定子和电机"这份资料深入剖析了电机的核心组件,从冲片设计到铁芯构造,再到定子绕组的功能,全面揭示了电机如何将电能转化为机械能的过程。这份文档对于从事电机设计、...

    感应电动机按定子磁场定向控制

    然而,矢量控制高度依赖于准确的转子参数,尤其是转子电阻,这使得其在实际应用中的鲁棒性受到限制。 - **直接转矩控制**:这种控制方法通过直接控制电机的转矩和磁链,简化了控制系统结构,提高了系统的响应速度...

    定子中国虚拟主机系统

    综合来看,定子中国虚拟主机系统提供了一套完整的网站管理解决方案,涵盖从基础的域名绑定到高级的数据库管理。对于个人用户和小型企业来说,它降低了搭建和维护网站的技术门槛;对于开发者,它提供了源码学习和二次...

    kaiyibu.rar_变频调速_定子电流_定子转速转矩_异步电机定子_电流波形

    异步电机的转速与转矩之间的关系受到定子电流的影响。在额定条件下,电机的转速由同步转速减去滑差决定,滑差又与电机的电磁转矩有关。当定子电流增大时,电机产生的电磁转矩也增大,从而提高电机的加速能力;反之,...

    平安电气培训讲义电机定子浸漆过程控制及检验验收.pptx

    电机作为现代工业生产和日常生活中不可或缺的动力来源,在众多机械设备中承担着基础而关键的作用。为了保证电机的高效运转和延长使用寿命,其制造过程中的每一个环节都必须严格控制。其中,电机定子浸漆过程是提升...

    电机定子槽源码.rar

    在描述中提到了“位图旋转”和“XFORM PlgBlt”,这暗示了源码可能涉及到图形界面或可视化模拟,用于展示电机定子槽的几何形状或者进行电机结构的动态演示。 位图旋转是一项常见的图像处理技术,它在计算机图形学中...

Global site tag (gtag.js) - Google Analytics