直播带货app开发利用ScrollView实现下拉内容刷新的效果

直播带货app开发利用ScrollView实现下拉内容刷新的效果

 

1
 <com.lucas.yanfriends.myview.smoothscroll.StretchContainer<br>            android:layout_width="match_parent"<br>            android:layout_height="match_parent"<br>            android:background="@color/white"<br>            app:ratio="0.6"><br> <br>            <com.lucas.yanfriends.myview.smoothscroll.StretchScrollView<br>                android:id="@+id/notify9999"<br>                android:layout_width="match_parent"<br>                android:layout_height="match_parent"<br>                android:background="@color/white"<br>                android:fillViewport="true"><br> <br> <br> <br> <br>           </com.lucas.yanfriends.myview.smoothscroll.StretchScrollView><br></com.lucas.yanfriends.myview.smoothscroll.StretchContainer>

StretchContainer.class

 

1
package com.lucas.yanfriends.myview.smoothscroll;<br> <br>import android.content.Context;<br>import android.content.res.TypedArray;<br>import android.support.annotation.AttrRes;<br>import android.support.annotation.NonNull;<br>import android.support.annotation.Nullable;<br>import android.util.AttributeSet;<br>import android.view.MotionEvent;<br>import android.view.ViewConfiguration;<br>import android.view.animation.DecelerateInterpolator;<br>import android.widget.FrameLayout;<br>import android.widget.Scroller;<br> <br>import com.lucas.yanfriends.R;<br> <br> <br>/**<br> * @author Gimpo create on 2017/9/1 10:54<br> * @email : jimbo922@163.com<br> */<br> <br>public class StretchContainer extends FrameLayout {<br> <br>    /**<br>     * 弹性系数  越小说明这个view越难被拉动<br>     */<br>    private float ratio;<br>    /**<br>     * view作出反应最小滑动距离<br>     */<br>    private int mTouchSlop;<br>    /**<br>     * 这个scroller可以让滑动更加的顺滑 不是很突兀的那种  写法固定<br>     */<br>    private Scroller mScroller;<br>    /**<br>     * 允许最大的下拉距离<br>     */<br>    private float maxY = 600;<br>    /**<br>     * 包裹的滚动view 可以是所有的view 这个view最好能滚动<br>     */<br>    private StretchScrollView mScrollView;<br>    /**<br>     * 最大滑动距离<br>     */<br>    private float mLastY;<br>    /**<br>     * 当前view被拖拽<br>     */<br>    private boolean mIsDraging;<br>    /**<br>     * 回弹时间<br>     */<br>    private int duration = 500;<br> <br>    public StretchContainer(@NonNull Context context) {<br>        super(context);<br>        initView(context, null);<br>    }<br> <br>    public StretchContainer(@NonNull Context context, @Nullable AttributeSet attrs) {<br>        super(context, attrs);<br>        initView(context, attrs);<br>    }<br> <br>    public StretchContainer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {<br>        super(context, attrs, defStyleAttr);<br>        initView(context, attrs);<br>    }<br> <br>    public void initView(Context context, AttributeSet attrs) {<br>        if (attrs != null) {<br>            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StretchContainer);<br>            ratio = a.getFloat(R.styleable.StretchContainer_ratio, 1.0f);<br>        }<br>        ViewConfiguration config = ViewConfiguration.get(context);<br>        mTouchSlop = config.getScaledTouchSlop();<br>        DecelerateInterpolator interpolator = new DecelerateInterpolator();<br>        mScroller = new Scroller(context, interpolator);<br>    }<br> <br>    @Override<br>    protected void onFinishInflate() {<br>        super.onFinishInflate();<br>        mScrollView = (StretchScrollView) getChildAt(0);<br>    }<br> <br>    @Override<br>    public boolean dispatchTouchEvent(MotionEvent ev) {<br>        float currentY = ev.getY();<br>        switch (ev.getAction()) {<br>            case MotionEvent.ACTION_DOWN:<br>                mLastY = currentY;<br>                break;<br>            case MotionEvent.ACTION_MOVE:<br>                float distanceY = currentY - mLastY;<br>                if (mIsDraging) {<br>                    if (distanceY <= 0) {<br>                        if(mScrollView.isScrolledToTop()) {<br>                            scrollTo(0, 0);<br>                            mIsDraging = false;<br>                            return super.dispatchTouchEvent(ev);<br>                        }<br>                    }else{<br>                        if(mScrollView.isScrolledToBottom()){<br>                            scrollTo(0, 0);<br>                            mIsDraging = false;<br>                            return super.dispatchTouchEvent(ev);<br>                        }<br>                    }<br>                    scrollTo(0, (int) (-distanceY * ratio));<br>                    return true;<br>                } else {<br>                    if (Math.abs(distanceY) > mTouchSlop) {<br>                        if (distanceY > 0) {  // 向下<br>                            if (mScrollView.isScrolledToTop()) {<br>                                mLastY = currentY;<br>                                mIsDraging = true;<br>                            }<br>                        }else{<br>                            if(mScrollView.isScrolledToBottom()){<br>                                mLastY = currentY;<br>                                mIsDraging = true;<br>                            }<br>                        }<br>                    }<br>                }<br>                break;<br>            case MotionEvent.ACTION_UP:<br>                if (mIsDraging) {<br>                    int scrollY = getScrollY();<br>                    mScroller.startScroll(0, scrollY, 0, -scrollY, duration);<br>                    mIsDraging = false;<br>                    invalidate();<br>                    return true;<br>                }<br>                break;<br>        }<br>        return super.dispatchTouchEvent(ev);<br>    }<br> <br>    @Override<br>    public void computeScroll() {<br>        //判断是否还在滚动,还在滚动为true<br>        if (mScroller.computeScrollOffset()) {<br>            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());<br>            //更新界面<br>            postInvalidate();<br>        }<br>        super.computeScroll();<br>    }<br>}

 

StretchScrollView.class

 

1
package com.lucas.yanfriends.myview.smoothscroll;<br> <br>import android.content.Context;<br>import android.util.AttributeSet;<br>import android.widget.ScrollView;<br> <br>/**<br> * @author Gimpo create on 2017/9/1 12:06<br> * @email : jimbo922@163.com<br> */<br> <br>public class StretchScrollView extends ScrollView {<br>    private boolean isScrolledToTop = true;<br>    private boolean isScrolledToBottom;<br> <br>    public StretchScrollView(Context context) {<br>        super(context);<br>    }<br> <br>    public StretchScrollView(Context context, AttributeSet attrs) {<br>        super(context, attrs);<br>    }<br> <br>    public StretchScrollView(Context context, AttributeSet attrs, int defStyleAttr) {<br>        super(context, attrs, defStyleAttr);<br>    }<br> <br>    @Override<br>    protected void onScrollChanged(int l, int t, int oldl, int oldt) {<br>        super.onScrollChanged(l, t, oldl, oldt);<br>        if (getScrollY() == 0) {    // 小心踩坑1: 这里不能是getScrollY() <= 0<br>            setScrolledToTop(true);<br>            setScrolledToBottom(false);<br>        } else if (getScrollY() + getHeight() - getPaddingTop()-getPaddingBottom() == getChildAt(0).getHeight()) {<br>            // 小心踩坑2: 这里不能是 >=<br>            // 小心踩坑3(可能忽视的细节2):这里最容易忽视的就是ScrollView上下的padding <br>            setScrolledToTop(false);<br>            setScrolledToBottom(true);<br>        } else {<br>            setScrolledToTop(false);<br>            setScrolledToBottom(false);<br>        }<br>    }<br> <br> <br>    public boolean isScrolledToTop() {<br>        return isScrolledToTop;<br>    }<br> <br>    public void setScrolledToTop(boolean scrolledToTop) {<br>        isScrolledToTop = scrolledToTop;<br>    }<br> <br>    public boolean isScrolledToBottom() {<br>        return isScrolledToBottom;<br>    }<br> <br>    public void setScrolledToBottom(boolean scrolledToBottom) {<br>        isScrolledToBottom = scrolledToBottom;<br>    }<br>}

以上就是直播带货app开发利用ScrollView实现下拉内容刷新的效果, 更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(22)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示