Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解
需求:想實(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)買(mǎi)" 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)買(mǎi)" 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)買(mǎi)布局和下面的購(gòu)買(mǎi)布局重合 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)買(mǎi)按鈕", 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)買(mǎi)" 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編程實(shí)現(xiàn)應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法,涉及Android中屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Jetpack?Compose?Canvas繪制超詳細(xì)介紹
Canvas?是允許您在屏幕上指定區(qū)域并在此區(qū)域上執(zhí)行繪制的組件。您必須使用修飾符指定尺寸,無(wú)論是通過(guò)Modifier.size修飾符指定確切尺寸,還是通過(guò)Modifier.fillMaxSize,ColumnScope.weight等相對(duì)于父級(jí)指定精確尺寸。如果父級(jí)包裝了此子級(jí),則僅必須指定確切尺寸2022-10-10Android使用TextView實(shí)現(xiàn)無(wú)下劃線(xiàn)超鏈接的方法
這篇文章主要介紹了Android使用TextView實(shí)現(xiàn)無(wú)下劃線(xiàn)超鏈接的方法,結(jié)合實(shí)例形式分析了Android中TextView超鏈接去除下劃線(xiàn)的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2016-08-08Kotlin?LinearLayout與RelativeLayout布局使用詳解
Kotlin?的基本特性就先寫(xiě)到這里,我們這個(gè)系列的定位是基礎(chǔ),也就是能用就好,夠用就好,我們不會(huì)舉太多的例子,但是這些都是最經(jīng)常用到的特性。從這節(jié)開(kāi)始就是Kotlin和android?進(jìn)行結(jié)合,使用Kotlin進(jìn)行安卓應(yīng)用的開(kāi)發(fā)了2022-12-12Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-08-08