Android 仿美团网,百度糯米购买框悬浮效果

废话少说,直接上例子~~~~~~~~~~~~~

1、先上效果图:







2、例子详解

main.xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00ffff"
        android:text="-----title--yyy----"
        android:textColor="#ff0000" />

    <com.example.android.scrolltricks.ObservableScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >

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

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:background="@drawable/a3"
                    android:scaleType="matrix" />

                <TextView
                    android:id="@+id/placeholder"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/sticky_height"
                    android:background="#00ffff"
                    android:text="place--------yyy----"
                    android:textColor="#ff0000" />

                <TextView
                    android:textSize="100dp"
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"
                    android:background="#ff00ff"
                    android:text="content" />
            </LinearLayout>

            <!--  -->

            <LinearLayout
                android:id="@+id/sticky"
                android:layout_width="match_parent"
                android:layout_height="@dimen/sticky_height"
                android:background="#e0000000"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="-嘻嘻嘻嘻嘻嘻嘻嘻嘻惺惺惜惺惺--"
                    android:textColor="#ff0000" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="-------------嘻嘻嘻嘻嘻嘻嘻嘻惺惺惜惺------------xxx" />
            </LinearLayout>
        </FrameLayout>
    </com.example.android.scrolltricks.ObservableScrollView>

</LinearLayout>


重写Scrollview:

public class ObservableScrollView extends ScrollView {
    private Callbacks mCallbacks;

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mCallbacks != null) {
            mCallbacks.onScrollChanged(t);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mCallbacks != null) {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mCallbacks.onDownMotionEvent();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mCallbacks.onUpOrCancelMotionEvent();
                    break;
            }
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }

    public void setCallbacks(Callbacks listener) {
        mCallbacks = listener;
    }

    public static interface Callbacks {
        public void onScrollChanged(int scrollY);
        public void onDownMotionEvent();
        public void onUpOrCancelMotionEvent();
    }
}


MainActivity.java 核心代码:

mStickyView = (LinearLayout) findViewById(R.id.sticky);

        
        mPlaceholderView = rootView.findViewById(R.id.placeholder);
        mObservableScrollView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        onScrollChanged(mObservableScrollView.getScrollY());
                    }
                });

   //该方法会布局变化时候调用,初始化后mStickyView将占有mPlaceholderView的位置
    @Override
    public void onScrollChanged(int scrollY) {
    	Log.i(tag, "scrollY-->"+scrollY+";  mPlaceholderView.getTop()-->"+mPlaceholderView.getTop());
    	
        mStickyView.setTranslationY(Math.max(mPlaceholderView.getTop(), scrollY));
    }


ok,代码比较简单吧.............



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值