对于标题栏随手势变化透明度应该是一个非常常见的功能了,包括京东和淘宝都有类似的功能.
最近工作里面需要就去网上查了查,发现都是对listview基础上写的,那么现在写一个基于scrollView的.
使用listView的原文链接http://blog.csdn.net/zhaokaiqiang1992/article/details/35237467
1,首先遇到的第一个问题是scroll没有onScrollChangedLinister,先写一个有onScrollChangedLinister监听的自定义scrollView
package com.teekart.view;
/**
* Created by king on 2015/11/6.
*/
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* 定义一个有onScrollChanged监听的ScrollView
* User: Jerry
* Date: 13-3-11
* Time: 下午6:27
*/
public class ObservableScrollView extends ScrollView{
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setScrollViewListener(ScrollViewListener listener) {
this.scrollViewListener = listener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
2,把我们的scrollView替换掉
<com.teekart.view.ObservableScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/include1"
android:scrollbars="none">
3,使用它
scrollView1 = (ObservableScrollView) findViewById(R.id.scrollView1);
scrollView1.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
// 获取当前位置占头布局高度的百分比
int height = scrollView.getChildAt(0).getMeasuredHeight()/3;
if (y<0){y=0;}
float f = (float) y / (float) height;
if (f>1){f=1;}
Log.i("scrollview","x="+x+" y="+y+" oldx="+oldx+" oldy="+oldy + "height" +height+"f"+f);
rl_travel_title.getBackground().setAlpha((int) (f * 255));
// 通知标题栏刷新显示
rl_travel_title.invalidate();
}
});
东西比较简单,我就不上源码了.效果图