andorid ScrollView内layout的悬停效果

首先:上效果图,一共是两张,第一张是最开始的样子,第二张是所需要达到的效果。



先讲解一下需求与功能点。ScollView里面的一个layout,当它往上滑动到屏幕顶端时自动悬停在顶端,当往下滑动时会跟着Scrollview一起滑动。

做法:写了一个与悬停一模一样的layout先隐藏在scrollview后面,在scrollview滑动时监听里面layout的高度,当它的高度为屏幕的顶端了时,隐藏的layout显示,反之显示。其实是很简单的。

首先上main.xml:

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


    <com.stay.test.StayScrollView
        android:id="@+id/stayScrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


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


            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="203dp"
                android:src="@drawable/stay" />


            <LinearLayout
                android:id="@+id/stayScrollLayout"
                android:layout_width="fill_parent"
                android:layout_height="30dp"
                android:background="@android:color/darker_gray"
                android:gravity="center" >


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Stay"
                    android:textColor="@android:color/white" />
            </LinearLayout>


            <TextView
                android:layout_width="fill_parent"
                android:layout_height="800dp"
                android:background="@android:color/black"
                android:text="自己写点内容吧。。。"
                android:textColor="@android:color/white" />
        </LinearLayout>
    </com.stay.test.StayScrollView>


    <LinearLayout
        android:id="@+id/stayLayout"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:visibility="gone" >


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stay"
            android:textColor="@android:color/white" />
    </LinearLayout>


</RelativeLayout>


自定义ScrollView(StayScrollView),主要是为了监听滑动事件。

package com.stay.test;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class StayScrollView extends ScrollView {

	OnScrollLisener mListener;

	public StayScrollView(Context context) {
		super(context);
	}

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

	public StayScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

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

	public void setOnScrollListener(OnScrollLisener listener) {
		if (null != listener) {
			mListener = listener;
		}
	}

	public abstract interface OnScrollLisener {
		public abstract void onScroll(int l, int t, int oldl, int oldt);
	}
}

然后再是MainActivity:

package com.stay.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.stay.test.StayScrollView.OnScrollLisener;

public class MainActivity extends Activity {
	private StayScrollView stayScroll;
	private LinearLayout stayLayout;
	private LinearLayout stayScrollLayout;
	private boolean flag = true;
	private int statusBarHeight = 0;
	private int topicBarHeight = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
		
		stayScroll.setOnScrollListener(new OnScrollLisener() {
			@Override
			public void onScroll(int l, int t, int oldl, int oldt) {
				if(flag){
					flag = false;
//					statusBarHeight = DeviceUtil.getStatusBarHeight();
					topicBarHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
				}
				fun();
			}
		});
    }
    
    private void init() {
		stayScroll = (StayScrollView) findViewById(R.id.stayScrollView);
		stayScroll.setOverScrollMode(View.OVER_SCROLL_NEVER);
		stayLayout = (LinearLayout) findViewById(R.id.stayLayout);
		stayScrollLayout = (LinearLayout) findViewById(R.id.stayScrollLayout);
	}

	private void fun() {
		int[] location = new int[2];
		stayScrollLayout.getLocationOnScreen(location);
		int y = location[1]; // stayScrollLayout的高度

		int height = statusBarHeight+topicBarHeight;
		if (height > y) {
			if(stayLayout.getVisibility() == View.GONE){
				stayLayout.setVisibility(View.VISIBLE);
			}
		} else {
			if(stayLayout.getVisibility() == View.VISIBLE){
				stayLayout.setVisibility(View.GONE);
			}
		}
	}
}
代码很简单,就不解释了。

OK,class dismiss。

资源,我上传了n次,但csdn....

好了,不说也罢~

但是这个fun()方法的处理有点问题,如果在一些差一点的手机上会卡,因为它一直在计算其中一个控件的位置。暂时没找到好的解决方法。如果有再更新,也可以指教!谢谢

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值