第一步首先创建一个class继承ScrollView:
public class MyScrollView extends ScrollView{
private OnScrollViewListener onScrollChangedListener;
public MyScrollView(Context context) {
super(context,null);
// TODO 自动生成的构造函数存根
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs,-1);
// TODO 自动生成的构造函数存根
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO 自动生成的构造函数存根
}
//写一个方法,用来初始化接口对象
public void setOnScrollViewListener(OnScrollViewListener onScrollChangedListener){
this.onScrollChangedListener=onScrollChangedListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO 自动生成的方法存根
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollChangedListener!=null) {
onScrollChangedListener.onScrollY(t);
}
}
//写一个接口,实现滑动悬停控件的高度
public interface OnScrollViewListener{
//scrollY指停止位置控件的高度
public void onScrollY(int scrollY);
}
}
第二步在xml文件中写入: com.example.fudong.MyScrollView复制以上class的限定名
include引用了一个xml文件这个就是要浮动的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:orientation="vertical" >
<com.example.fudong.MyScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/car11"
android:scaleType="centerCrop" />
<include
android:id="@+id/top"
layout="@layout/fasdf"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/car12"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/car13"
android:scaleType="centerCrop" />
</LinearLayout>
<include
android:id="@+id/di"
layout="@layout/fasdf"
/>
</FrameLayout>
</com.example.fudong.MyScrollView>
</LinearLayout>
第三步在activity中实现:
public class MainActivity extends Activity implements OnScrollViewListener{
private MyScrollView scroll;
private LinearLayout top;
private LinearLayout di;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
scroll = (MyScrollView) findViewById(R.id.scroll);
top = (LinearLayout) findViewById(R.id.top);
di = (LinearLayout) findViewById(R.id.di);
iv = (ImageView) findViewById(R.id.iv);
scroll.setOnScrollViewListener(this);
//给这个布局设置点击事件
findViewById(R.id.layout).getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO 自动生成的方法存根
onScrollY(scroll.getHeight());
}
});
}
@Override
public void onScrollY(int scrollY) {
// 得到浮动部件上面的坐标 Math.max谁最大得到谁
int max = Math.max(scrollY, iv.getHeight());
//设置浮动部件的位置坐标
di.layout(0, max, di.getWidth(),max+di.getHeight());
}
}