Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿電商 App 商品詳情頁:標題列隨滾動吸頂的懸浮效果

編輯:關於 Android 程式設計

1、效果預覽

這個效果的用戶體驗確實相當酷炫,今天就來拆解它的實作方式。

2、先拆解結構

整個頁面可以分成四塊:

• 懸浮內容 floatView
• 頂部內容 headView
• 中間內容,與懸浮內容相同 middleView
• 商品詳情展示頁 detailView

因為內容高度會超過螢幕,所以用 ScrollView 承擔滾動;懸浮的 view 與 ScrollView 同層,放在同一個 FrameLayout 或 RelativeLayout 中。

當 y 方向的滾動距離小於 middleView 到頂部的距離時,middleView 自然會隨頁面往上滑走,這時顯示懸浮 view,視覺上就變成 middleView 一直卡在頂端。

當滾動距離大於那個距離時,把懸浮 view 隱藏起來即可。

也就是說,我們只需要兩個數值:ScrollView 的滾動距離,以及 middleView 到頂部的高度。複雜的交互效果就這樣化約成兩個 API 的組合。

3、第一種實作

3.1 取得 middleView 到父容器頂部的距離

tv_title.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mTitleTopAndHeight = tv_title.getTop();
        tv_title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
});

在 Activity 的 onCreate() 中直接呼叫 getTop() 會拿到 0,因為此時 view 還沒被繪製到畫面上。所以改用上面的監聽方式來取值,而且由於繪製可能觸發多次,取到之後記得把監聽移除。

以下寫法也能拿到正確結果:

tv_title.post(new Runnable() {
    @Override
    public void run() {
        mTitleTopAndHeight = tv_title.getTop();
    }
});

利用 post() 把操作放進佇列,等系統布局完成後再執行,同樣能取到正確的 top 值。

3.2 取得垂直方向的滾動距離

ScrollView 的父類 View 有個 onScrollChanged() 方法,它虽然是 protected、外部不能直接呼叫,但 ScrollView 滾動時內部一定會執行它。因此我們自定義一個 MyScrollView,在該方法中透過回呼把距離傳出去。

public class MyScrollView extends ScrollView {

    private OnScrollListener mOnScrollListener;
    /** 是否為使用者手指觸摸滑動 */
    private boolean mIsTouch = false;

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollListener != null) {
            mOnScrollListener.onScroll(t, mIsTouch
                    ? OnScrollListener.SCROLL_STATE_TOUCH_SCROLL
                    : OnScrollListener.SCROLL_STATE_FLING);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mIsTouch = true;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIsTouch = false;
                break;
        }
        return super.onTouchEvent(ev);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        mOnScrollListener = onScrollListener;
    }

    public interface OnScrollListener {
        /** 使用者手指拖動滾動 */
        int SCROLL_STATE_TOUCH_SCROLL = 0x0;
        /** 慣性滑行滾動 */
        int SCROLL_STATE_FLING = 0x1;

        /**
         * 滾動時的回呼
         *
         * @param scrollY Y 方向滾動的距離
         * @param scroll_state 目前的滾動狀態:手勢拖動或慣性滑行
         */
        void onScroll(int scrollY, int scroll_state);
    }
}

3.3 使用方式

在 Activity 中掛上自定義的滾動監聽,依距離切換懸浮 view 的顯示:

mScrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
    @Override
    public void onScroll(int scrollY, int state) {
        Log.d("onScroll: ", scrollY + "" + "----------- state:" + state);
        if (scrollY <= mTitleTopAndHeight) {
            tv_float.setVisibility(View.INVISIBLE);
        } else {
            tv_float.setVisibility(View.VISIBLE);
        }
    }
});

接著把懸浮 view 上的觸摸事件轉發給 ScrollView,這樣使用者滑動懸浮列時,下方的內容也會一起滾動:

tv_float.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mScrollView.onTouchEvent(event);
        return false;
    }
});

版面配置如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.qike.scrolltitle.MyScrollView
        android:id="@+id/sv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:gravity="center"
                android:text="商品圖片"/>

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#a3c"
                android:gravity="center"
                android:text="標題view"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:background="#a2bb"
                android:gravity="center"
                android:text="詳情頁面"/>
        </LinearLayout>
    </com.example.qike.scrolltitle.MyScrollView>

    <TextView
        android:id="@+id/tv_float"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#a3c"
        android:gravity="center"
        android:text="標題view"
        android:visibility="invisible"/>
</RelativeLayout>

4、第二種更簡潔的做法

差別只在取得滾動位置的方式,用 ViewTreeObserver 直接監聽即可:

mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        int scrollY = mScrollView.getScrollY();
        if (scrollY <= mTitleTopAndHeight) {
            tv_float.setVisibility(View.INVISIBLE);
        } else {
            tv_float.setVisibility(View.VISIBLE);
        }
    }
});

既然有這麼簡便的寫法,為什麼還要介紹第一種?細心你可能已經發現,自定義的監聽裡多回傳了一個代表當前滾動狀態的參數 state。實務上,「使用者手動拖動」與「慣性滑行」往往需要不同處理——例如淘寶的商品詳情頁,只有當使用者手動拖動到臨界值時才會拉出下方的詳情內容,慣性滑動則會停在原地。有了這個參數,這類細節才好實作。

5、總結

以上就是讓標題列隨滾動吸頂在固定位置的兩種做法。核心只有兩個數值與一個狀態判斷,掌握之後就能延伸出更多變化,希望對你的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved