欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中ScrollView監(jiān)聽滑動距離案例講解

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

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

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

效果一如如下:

實現(xiàn)步驟:

1、自定義MyScrollView

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

(2)自定義滑動監(jiān)聽接口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、布局文件如下:

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

<?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() {
        //當布局的狀態(tài)或者控件的可見性發(fā)生改變回調的接口
        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()接口方法中監(jiān)聽到的是垂直方向滑動的距離Y,可以根據(jù)自己的需要進行布局的其他操作。

附加:

效果二如下圖所示:

(根據(jù)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>

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

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

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

相關文章

  • Android編程實現(xiàn)應用強制安裝到手機內存的方法

    Android編程實現(xiàn)應用強制安裝到手機內存的方法

    這篇文章主要介紹了Android編程實現(xiàn)應用強制安裝到手機內存的方法,涉及Android中屬性設置的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Jetpack?Compose?Canvas繪制超詳細介紹

    Jetpack?Compose?Canvas繪制超詳細介紹

    Canvas?是允許您在屏幕上指定區(qū)域并在此區(qū)域上執(zhí)行繪制的組件。您必須使用修飾符指定尺寸,無論是通過Modifier.size修飾符指定確切尺寸,還是通過Modifier.fillMaxSize,ColumnScope.weight等相對于父級指定精確尺寸。如果父級包裝了此子級,則僅必須指定確切尺寸
    2022-10-10
  • Android init.rc文件簡單介紹

    Android init.rc文件簡單介紹

    這篇文章主要介紹了Android init.rc文件簡單介紹的相關資料,需要的朋友可以參考下
    2016-12-12
  • Android使用TextView實現(xiàn)無下劃線超鏈接的方法

    Android使用TextView實現(xiàn)無下劃線超鏈接的方法

    這篇文章主要介紹了Android使用TextView實現(xiàn)無下劃線超鏈接的方法,結合實例形式分析了Android中TextView超鏈接去除下劃線的相關實現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2016-08-08
  • Android Studio調試功能使用匯總

    Android Studio調試功能使用匯總

    這篇文章主要為大家詳細介紹了Android Studio調試功能使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 淺析Android 的 MediaPlayer類

    淺析Android 的 MediaPlayer類

    本文主要介紹了Android的mediaplayer類作用和用法,并附上了關鍵代碼,有需要的朋友可以參考下
    2014-10-10
  • Kotlin?LinearLayout與RelativeLayout布局使用詳解

    Kotlin?LinearLayout與RelativeLayout布局使用詳解

    Kotlin?的基本特性就先寫到這里,我們這個系列的定位是基礎,也就是能用就好,夠用就好,我們不會舉太多的例子,但是這些都是最經(jīng)常用到的特性。從這節(jié)開始就是Kotlin和android?進行結合,使用Kotlin進行安卓應用的開發(fā)了
    2022-12-12
  • Android so的熱升級嘗試

    Android so的熱升級嘗試

    這篇文章主要介紹了Android so的熱升級嘗試的相關知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-11-11
  • Android EventBus(普通事件/粘性事件)詳解

    Android EventBus(普通事件/粘性事件)詳解

    這篇文章主要為大家詳細介紹了Android EventBus 普通事件/粘性事件的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法

    Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法

    這篇文章主要介紹了Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法,結合實例形式分析了Android視頻播放事件響應及相關屬性設置操作技巧,需要的朋友可以參考下
    2017-08-08

最新評論