控件随ScrollView滚动到某位置会停靠的效果

之前因为项目需要做了一个效果,就是当某个操作按钮滑动顶部是固定在顶部,不让其隐藏,让用户无时无刻都可以实现这个操作。

这个效果不是很神奇,就个人来说呢,很有用。无形间增加了用户体验鸟  源代码:http://download.csdn.net/detail/hcb1230/6593367

直接上图吧,效果如下:



效果不错吧,哈哈


一开始 中间深绿色的一条 可以随着整一块拖动。

当它到达顶部的时候,能够靠着状态栏停留。

往下拖动回到原来位置的时候 , 又可以向下拖走。

有点仿美团团商品页面那个价格一栏的效果。

因为自己做项目需要,查了好久 ,基本没有找到想要的, 就发出来。

想法很简单。

两个TextView换成想要固定的控件,就能看上去实现这个效果。


源代码:http://download.csdn.net/detail/hcb1230/6593367


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

        private TextView tvdong;
        private TextView tvjing;
        private ScrollView sv;
        int[] location = new int[2];
        int[] location2 = new int[2];

        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                initview();
        }

        private void initview() {
                //在滚动的一个TextView
                tvdong = (TextView) findViewById(R.id.dong);
                //一开始是View.GONE的一个静止的TextView
                tvjing = (TextView) findViewById(R.id.jing);
                sv = (ScrollView) findViewById(R.id.eee);
                tvjing.setVisibility(View.GONE);

                sv.setOnTouchListener(new OnTouchListener() {
                        private int lastY = 0;
                        private int touchEventId = -9983761;
                        Handler handler = new Handler() {
                                public void handleMessage(Message msg) {
                                        super.handleMessage(msg);
                                        if (msg.what == touchEventId) {
                                                if (lastY != sv.getScrollY()) {
                                                         //scrollview一直在滚动,会触发
                                                        handler.sendMessageDelayed(
                                                                        handler.obtainMessage(touchEventId, sv), 5);
                                                        lastY = sv.getScrollY();
                                                        tvdong.getLocationOnScreen(location);
                                                        tvjing.getLocationOnScreen(location2);
                                                        //动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个
                                                        if (location[1] <= location2[1]) {
                                                                tvjing.setVisibility(View.VISIBLE);
                                                        } else {
                                                                //静止的隐藏了
                                                                tvjing.setVisibility(View.GONE);
                                                        }
                                                }
                                        }
                                }
                        };

                        public boolean onTouch(View v, MotionEvent event) {
                                 //必须两个都搞上,不然会有瑕疵。
                                //没有这段,手指按住拖动的时候没有效果
                                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                        handler.sendMessageDelayed(
                                                        handler.obtainMessage(touchEventId, v), 5);
                                }
                                //没有这段,手指松开scroll继续滚动的时候,没有效果
                                if (event.getAction() == MotionEvent.ACTION_UP) {
                                        handler.sendMessageDelayed(
                                                        handler.obtainMessage(touchEventId, v), 5);
                                }
                                return false;
                        }
                });
        }
}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/eee"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FF8800"
                android:text="\n这  \n 是 \n   展   \n 示\n图\n片\n\n" />

            <TextView
                android:id="@+id/dong"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#008833"
                android:text="\n\n这  是   滚  动    的     等  一  下  固  定  在 上  面\n\n" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00ff33"
                android:text="~~~~~~\n\n\n\n这\n\n\n\n\n\n是\n\n\n\n\n\n其\n\n\n\n\n他\n\n\n\n\n\n\n的\n\n\n~~~~~~~" />
        </LinearLayout>
    </ScrollView>

    <TextView
        android:id="@+id/jing"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#008800"
        android:text="\n\n这    是   固     定     的\n\n" />
</FrameLayout>

可以把上面的TextView换成一个LinearLayout神马的,然后把你想实现的控件全部贴上去。还尅有改变静止的TextView实现想要停靠的位置。不只是顶部。比如说 ,购物App,把立即购买放在上面,看起来就很有感觉呀。之前一直在想怎么实现这个,网上找了好多demo都不是自己想要的。后来,突然灵感来了。很简单的就能实现这个方法了。现在看来效果也还算不错的。相信你看了代码就知道是如何实现的了、


源代码:http://download.csdn.net/detail/hcb1230/6593367

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值