需求:想实现像美团中列表下拉后出现悬浮窗的效果。

思路:首先对ScrollView进行滑动监听,然后在onScrollChanged()方法中获取到滑动的Y值,接着进行相关操作即可。

效果一如如下:

实现步骤:

1、自定义MyScrollView

(1)重写onScrollChanged()获取Y值。

(2)自定义滑动监听接口onScrollListener并公开此接口。

public class MyScrollView extends ScrollView {
 
    private OnScrollListener onScrollListener;
 
    public MyScrollView(Context context) {
        super(context);
    }
 
    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }
 
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollListener != null) {
            onScrollListener.onScroll(t);
        }
    }
 
    /**
     * 接口对外公开
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
 
    /**
     *
     * 滚动的回调接口
     *
     * @author xiaanming
     *
     */
    public interface OnScrollListener{
        /**
         * 回调方法, 返回MyScrollView滑动的Y方向距离
         * @param scrollY
         *              、
         */
        void onScroll(int scrollY);
    }
}

2、布局文件如下:

(主要是创建两个相同的布局,顶部一个,相应的位置一个,后面有用)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:id="@ id/Main_lLayoutParent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
 
    <com.deepreality.scrollviewscrollpositiondemo.MyScrollView
        android:id="@ id/Main_myScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
 
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="260dp"
                    android:src="@mipmap/icon_product"
                    android:scaleType="fitXY"/>
 
                <LinearLayout
                    android:id="@ id/Main_lLayoutViewTemp"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:orientation="horizontal"
                    android:background="@color/colorRed"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp">
 
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="¥139"
                        android:textSize="24dp"
                        android:textColor="@color/colorWhite"/>
 
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:gravity="bottom"
                        android:paddingLeft="10dp"
                        android:paddingBottom="10dp">
 
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="已抢900件"
                            android:textSize="11dp"
                            android:textColor="@color/colorWhite"/>
 
                    </LinearLayout>
 
                    <Button
                        android:layout_width="100dp"
                        android:layout_height="35dp"
                        android:text="立即购买"
                        android:textColor="@color/colorWhite"
                        android:background="@drawable/btn_corner"/>
 
                </LinearLayout>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="内容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="内容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="内容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="内容"/>
 
            </LinearLayout>
 
            <LinearLayout
                android:id="@ id/Main_lLayoutView"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:orientation="horizontal"
                android:background="@color/colorRed"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¥139"
                    android:textSize="24dp"
                    android:textColor="@color/colorWhite"/>
 
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="bottom"
                    android:paddingLeft="10dp"
                    android:paddingBottom="10dp">
 
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="已抢900件"
                        android:textSize="11dp"
                        android:textColor="@color/colorWhite"/>
 
                </LinearLayout>
 
                <Button
                    android:id="@ id/Main_btnBuy"
                    android:layout_width="100dp"
                    android:layout_height="35dp"
                    android:text="立即购买"
                    android:textColor="@color/colorWhite"
                    android:background="@drawable/btn_corner"/>
 
            </LinearLayout>
 
        </FrameLayout>
 
    </com.deepreality.scrollviewscrollpositiondemo.MyScrollView>
 
</LinearLayout>

3、MainActivity.java的代码如下:

public class MainActivity extends AppCompatActivity implements MyScrollView.OnScrollListener, View.OnClickListener {
 
    private Context mContext;
 
    private LinearLayout lLayoutParent, lLayoutTemp, lLayoutView;
    private Button btnBuy;
    private MyScrollView myScrollView;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        baseDataInit();
        bindViews();
        viewsAddListener();
        viewsDataInit();
    }
 
    private void baseDataInit() {
        mContext = this;
    }
 
    private void bindViews() {
        lLayoutParent = findViewById(R.id.Main_lLayoutParent);
        lLayoutTemp = findViewById(R.id.Main_lLayoutViewTemp);
        lLayoutView = findViewById(R.id.Main_lLayoutView);
        btnBuy = findViewById(R.id.Main_btnBuy);
        myScrollView = findViewById(R.id.Main_myScrollView);
    }
 
    private void viewsAddListener() {
        //当布局的状态或者控件的可见性发生改变回调的接口
        lLayoutParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //这一步很重要,使得上面的购买布局和下面的购买布局重合
                onScroll(myScrollView.getScrollY());
            }
        });
        myScrollView.setOnScrollListener(this);
        btnBuy.setOnClickListener(this);
    }
 
    private void viewsDataInit() {
 
    }
 
    @Override
    public void onScroll(int scrollY) {
        int mBuyLayout2ParentTop = Math.max(scrollY, lLayoutTemp.getTop());
        lLayoutView.layout(0, mBuyLayout2ParentTop, lLayoutView.getWidth(), mBuyLayout2ParentTop   lLayoutView.getHeight());
    }
 
    @Override
    public void onClick(View v) {
        Toast.makeText(mContext, "您点击了购买按钮", Toast.LENGTH_SHORT).show();
    }
}

其中,onScroll()接口方法中监听到的是垂直方向滑动的距离Y,可以根据自己的需要进行布局的其他操作。

附加:

效果二如下图所示:

(根据ScrollView下滑距离设置布局的透明度)

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:id="@ id/Main_lLayoutParent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.deepreality.scrollviewscrollpositiondemo.MyScrollView
            android:id="@ id/Main_myScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="260dp"
                            android:src="@mipmap/icon_product"
                            android:scaleType="fitXY"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="内容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="内容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="内容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="内容"/>

                    </LinearLayout>

                </LinearLayout>

            </FrameLayout>

        </com.deepreality.scrollviewscrollpositiondemo.MyScrollView>

        <LinearLayout
            android:id="@ id/Main_lLayoutViewTemp1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="@color/colorRed"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:layout_alignParentTop="true"
            android:paddingRight="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥139"
                android:textSize="24dp"
                android:textColor="@color/colorWhite"/>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="bottom"
                android:paddingLeft="10dp"
                android:paddingBottom="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="已抢900件"
                    android:textSize="11dp"
                    android:textColor="@color/colorWhite"/>

            </LinearLayout>

            <Button
                android:id="@ id/Second_btnBuy"
                android:layout_width="100dp"
                android:layout_height="35dp"
                android:text="立即购买"
                android:textColor="@color/colorWhite"
                android:background="@drawable/btn_corner"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

相应的代码和上一个样式的代码基本一致,只是改了接口中的实现方法。

@Override
public void onScroll(int scrollY) {
    if (scrollY >= 225) {
        scrollY = 225;
    }
    lLayoutViewTemp1.getBackground().setAlpha(scrollY);
}

到此这篇关于Android中ScrollView监听滑动距离案例讲解的文章就介绍到这了,更多相关Android中ScrollView监听滑动距离内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Android中ScrollView监听滑动距离案例讲解的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  4. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  5. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  6. ios – 无法启动iPhone模拟器

    /Library/Developer/CoreSimulator/Devices/530A44CB-5978-4926-9E91-E9DBD5BFB105/data/Containers/Bundle/Application/07612A5C-659D-4C04-ACD3-D211D2830E17/ProductName.app/ProductName然后,如果您在Xcode构建设置中选择标准体系结构并再次构建和运行,则会产生以下结果:dyld:lazysymbolbindingFailed:Symbol

  7. iOS将UIView转换为ScrollView而不破坏布局?

    是否有可能在不破坏所有约束和放置的情况下从UIView移动到UIScrollView.问题是我构建整个UI而不在iPhone4上测试它,现在我看到一些视图应该在ScrollView中工作.我尝试了一些技巧,但没有任何作用.约束被删除.以下是示例的示例图片:现在我希望test1UIView是ScrollView,我试图将ScrollView放在test1View中,然后在滚动视图中递归复制test1

  8. ios – 放大故事板中的任何视图时,Xcode 8.2和8.1崩溃

    当我单击视图框并拖动以放大视图时,视图不会放大.但相反,鼠标等待指示器将持续一秒钟,然后整个xcode将崩溃.这是在我的代码8.2更新后发生的.所以我尝试安装xcode8.1,问题仍然存在于一个特定项目中.所有其他项目都运作良好.故事板中没有警告或冲突.我不记得改变任何设置.附加崩溃日志:CRASH_LOG解决方法修正了问题:在我将ScrollView添加到ViewController并更改了Vi

  9. Xamarin iOS图像在Grid内部重叠

    heyo,所以在Xamarin我有一个使用并在其中包含一对,所有这些都包含在内.这在Xamarin.Android中看起来完全没问题,但是在Xamarin.iOS中,图像与标签重叠.我不确定它的区别是什么–为什么它在Xamarin.Android中看起来不错但在iOS中它的全部都不稳定?

  10. ios – UIButton在uiscrollView中不起作用

    我有一个将UIView作为子视图的scrollView.这有UIView子视图UIButton.只有scrollView连接到插座,其余全部都是代码.按钮不响应触摸,触摸时不变蓝.我能做些什么才能让它发挥作用?这是代码:解决方法您必须设置视图的内容大小.它必须大于或等于scrollView的内容大小.因为您的视图的默认大小是320*480和320*568.因此,增加视野的高度–self.view.frame=CGRectMake;然后将其添加为scrollView的子视图.将帮助您解决问题.

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部