上图
两种不同的方式实现的悬浮框效果。
先说图(1)通过重写ScrollView控件来实现
原理:通过 MyScrollView2,控制了Viewpage,和他下面的linearLayout 的高度的监听,然后再让另外的相同布局的LinearLayout(需要悬浮的)显示出来
主要代码:重写的MyScrollView:
//这个方法执行的时候所在的xml已经加载完成 获得listView顶部的两个控件的高度
@Override
protected void onFinishInflate() {
id_hint=findViewById(R.id.id_hint);
id_pager=findViewById(R.id.id_pager);
this.smoothScrollTo(mTopViewHeight+hintHeight, 0);
}
//然后获取listView的顶部控件的高度
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mTopViewHeight = id_pager.getMeasuredHeight();
hintHeight=id_hint.getMeasuredHeight();
}
//滑动监听
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//去掉这个动画感觉效果更好一些
// float scale=1-(t*1.0f/(mTopViewHeight+hintHeight))+0.1f;
// if(scale>=1){
// scale=1;
// }
// ViewHelper.setAlpha(id_pager, scale);
// ViewHelper.setAlpha(id_hint, scale);
if(t>=mTopViewHeight){
icallBack.show();
}else{
icallBack.hide();
}
}
activity中的监听:
scrollView.setIcallBack(new IcallBack() {
@Override
public void show() {
runOnUiThread(new Runnable() {
public void run() {
hint2.setVisibility(View.VISIBLE);
}
});
}
@Override
public void hide() {
hint2.setVisibility(View.GONE);
}
});
布局文件:
<FrameLayout 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:background="#ffffff"
xmlns:app="http://schemas.android.com/apk/res/com.example.suspendlayout">
<com.example.suspendlayout.util.swip.SwipyRefreshLayout
android:id="@+id/id_sr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:direction="both"
>
<com.example.suspendlayout.util.MyScrollView2
android:scrollbars="none"
android:id="@+id/sv_sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/id_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#eee" />
<LinearLayout
android:id="@+id/id_hint"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff85dd"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
</LinearLayout>
<com.example.suspendlayout.util.MyListView
android:id="@+id/id_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
</com.example.suspendlayout.util.MyScrollView2>
</com.example.suspendlayout.util.swip.SwipyRefreshLayout>
<LinearLayout
android:id="@+id/id_hint_me"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff85dd"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="时间状态"
android:textColor="#ff00ff" />
</LinearLayout>
</FrameLayout>
图(2)通过监听ListView的setOnScrollListener()方法来实现
原理:listView对setOnScrollListener()的监听,然后让悬浮宽显示隐藏
主要代码:
View headView = getLayoutInflater().inflate(R.layout.head_view, null);
mlist.addHeaderView(headView);
//悬浮框
diary_mshv = new MineStickyHeaderView(this);
mlist.addHeaderView(diary_mshv);
diary_mshv.setOnFavoriteItemClickListener(new FavoriteTabChangedListener());
stickyHeader_mshv.setOnFavoriteItemClickListener(new FavoriteTabChangedListener());
mlist.setOnScrollListener(new OnScrollListener() {
/**
* scrollState有三种状态,分别是SCROLL_STATE_IDLE、SCROLL_STATE_TOUCH_SCROLL、
* SCROLL_STATE_FLING SCROLL_STATE_IDLE是当屏幕停止滚动时
* SCROLL_STATE_TOUCH_SCROLL是当用户在以触屏方式滚动屏幕并且手指仍然还在屏幕上时
* SCROLL_STATE_FLING是当用户由于之前划动屏幕并抬起手指,屏幕产生惯性滑动时
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
/**
* firstVisibleItem 表示在当前屏幕显示的第一个listItem在整个listView里面的位置(下标从0开始)
* visibleItemCount表示在现时屏幕可以见到的ListItem(部分显示的ListItem也算)总数
* totalItemCount表示ListView的ListItem总数
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (null != diary_mshv) {
if (firstVisibleItem > 0) {
System.out.println(firstVisibleItem);
stickyHeader_mshv.setVisibility(View.VISIBLE);
} else {
stickyHeader_mshv.setVisibility(View.GONE);
}
} else {
}
}
});
布局文件:
<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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/ptrlv_fragment_mine_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.example.suspendlayout.util.MineStickyHeaderView
android:id="@+id/mshv_mine_sticky_header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
最后附上demo的现在地址:http://www.oschina.net/code/snippet_2702417_57620
看到一个挺牛的悬浮的,这里贴下链接:https://github.com/sfsheng0322/StickyHeaderListView