最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解

 更新時(shí)間:2021年08月02日 10:12:11   作者:北極熊的微笑  
這篇文章主要介紹了Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

需求:想實(shí)現(xiàn)像美團(tuán)中列表下拉后出現(xiàn)懸浮窗的效果。

思路:首先對(duì)ScrollView進(jìn)行滑動(dòng)監(jiān)聽(tīng),然后在onScrollChanged()方法中獲取到滑動(dòng)的Y值,接著進(jìn)行相關(guān)操作即可。

效果一如如下:

實(shí)現(xiàn)步驟:

1、自定義MyScrollView

(1)重寫(xiě)onScrollChanged()獲取Y值。

(2)自定義滑動(dòng)監(jiān)聽(tīng)接口onScrollListener并公開(kāi)此接口。

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);
        }
    }
 
    /**
     * 接口對(duì)外公開(kāi)
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
 
    /**
     *
     * 滾動(dòng)的回調(diào)接口
     *
     * @author xiaanming
     *
     */
    public interface OnScrollListener{
        /**
         * 回調(diào)方法, 返回MyScrollView滑動(dòng)的Y方向距離
         * @param scrollY
         *              、
         */
        void onScroll(int scrollY);
    }
}

2、布局文件如下:

(主要是創(chuàng)建兩個(gè)相同的布局,頂部一個(gè),相應(yīng)的位置一個(gè),后面有用)

<?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="立即購(gòu)買"
                        android:textColor="@color/colorWhite"
                        android:background="@drawable/btn_corner"/>
 
                </LinearLayout>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="內(nèi)容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="內(nèi)容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="內(nèi)容"/>
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="內(nèi)容"/>
 
            </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="立即購(gòu)買"
                    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() {
        //當(dāng)布局的狀態(tài)或者控件的可見(jiàn)性發(fā)生改變回調(diào)的接口
        lLayoutParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //這一步很重要,使得上面的購(gòu)買布局和下面的購(gòu)買布局重合
                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, "您點(diǎn)擊了購(gòu)買按鈕", Toast.LENGTH_SHORT).show();
    }
}

其中,onScroll()接口方法中監(jiān)聽(tīng)到的是垂直方向滑動(dòng)的距離Y,可以根據(jù)自己的需要進(jìn)行布局的其他操作。

附加:

效果二如下圖所示:

(根據(jù)ScrollView下滑距離設(shè)置布局的透明度)

布局文件如下:

<?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="內(nèi)容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="內(nèi)容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="內(nèi)容"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="內(nèi)容"/>

                    </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="立即購(gòu)買"
                android:textColor="@color/colorWhite"
                android:background="@drawable/btn_corner"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

相應(yīng)的代碼和上一個(gè)樣式的代碼基本一致,只是改了接口中的實(shí)現(xiàn)方法。

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

到此這篇關(guān)于Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解的文章就介紹到這了,更多相關(guān)Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 教你如何搭建android源代碼repo倉(cāng)庫(kù)

    教你如何搭建android源代碼repo倉(cāng)庫(kù)

    這篇文章主要介紹了如何搭建android源代碼repo倉(cāng)庫(kù),如果你的開(kāi)發(fā)是基于AOSP源碼來(lái)建倉(cāng),那么搭建repo服務(wù)器和部署自己的repo倉(cāng)庫(kù)就是非常必要的工作了,本文給大家詳細(xì)介紹搭建過(guò)程,感興趣的朋友一起看看吧
    2022-07-07
  • Android應(yīng)用中仿今日頭條App制作ViewPager指示器

    Android應(yīng)用中仿今日頭條App制作ViewPager指示器

    這篇文章主要介紹了Android應(yīng)用中仿今日頭條App制作ViewPager指示器的例子,一般就是導(dǎo)航條在翻頁(yè)時(shí)的動(dòng)態(tài)字體變色效果,需要的朋友可以參考下
    2016-04-04
  • Android中極簡(jiǎn)的js與java的交互庫(kù)(SimpleJavaJsBridge)

    Android中極簡(jiǎn)的js與java的交互庫(kù)(SimpleJavaJsBridge)

    本文主要介紹了Android中極簡(jiǎn)的js與java的交互庫(kù)--SimpleJavaJsBridge,它可以讓js與java之間的通信更簡(jiǎn)單。 具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Android WebView實(shí)現(xiàn)頂部進(jìn)度條

    Android WebView實(shí)現(xiàn)頂部進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android WebView實(shí)現(xiàn)頂部進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Flutter實(shí)戰(zhàn)之自定義日志打印組件詳解

    Flutter實(shí)戰(zhàn)之自定義日志打印組件詳解

    這篇文章主要介紹了Flutter實(shí)戰(zhàn)之自定義日志打印組件詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • 詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理

    詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理

    這篇文章主要為大家介紹了詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值

    Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值

    這篇文章主要為大家詳細(xì)介紹了Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android自定義ListView實(shí)現(xiàn)下拉刷新上拉加載更多

    Android自定義ListView實(shí)現(xiàn)下拉刷新上拉加載更多

    Listview現(xiàn)在用的很少了,基本都是使用Recycleview,但是不得不說(shuō)Listview具有劃時(shí)代的意義,我們可以自己添加下拉刷新,上拉加載更多功能。本文就來(lái)利用自定義ListView實(shí)現(xiàn)下拉刷新上拉加載更多效果,需要的可以參考一下
    2022-10-10
  • 深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求

    深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求

    本篇文章主要介紹了深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Android中Fragment的加載方式與數(shù)據(jù)通信詳解

    Android中Fragment的加載方式與數(shù)據(jù)通信詳解

    本文主要介紹了Android中Fragment的加載方式與數(shù)據(jù)通信的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03

最新評(píng)論

呼和浩特市| 泉州市| 嘉鱼县| 德清县| 巴彦县| 林西县| 新疆| 会宁县| 河北省| 广安市| 修武县| 沽源县| 黔南| 沁阳市| 浦县| 彭泽县| 界首市| 北流市| 沛县| 威信县| 宁都县| 凤凰县| 保山市| 广平县| 克拉玛依市| 台江县| 克拉玛依市| 龙里县| 祥云县| 漳州市| 化德县| 射洪县| 松阳县| 敦化市| 苍溪县| 屯门区| 扶余县| 中阳县| 米林县| 望谟县| 定安县|