- step1:
step2:
View
/**@author by LinWill at 2016/7/2
* @version 0.1
* @category 可以设置悬浮窗口的ScrollView
*/
public class FloatScrollView extends ScrollView {
LinearLayout layoutNormal,layoutFloat;
View floatView;
public FloatScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public FloatScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FloatScrollView(Context context) {
super(context);
}
/**
* 设置浮动窗口。
* @param layoutNormal 正常状态下悬浮窗的父容器
* @param layoutFloat 置顶状态下悬浮车的父容器
* @param floatView 需要悬浮的窗口
* */
public void setFloatView(LinearLayout layoutNormal,LinearLayout layoutFloat,View floatView){
this.layoutNormal=layoutNormal;
this.layoutFloat =layoutFloat ;
this.floatView =floatView ;
}
/**检查状态,执行对应改变。
* @param scrollY scrollView当前scroll位置
* @param isUp 当前是否向上滚动
* */
private void changFloatView(int scrollY,boolean isUp){
//检查是否执行过setFloatView方法
if(null==floatView||null==layoutNormal||null==layoutFloat){
return;
}
//改变状态的位置。
int scrollChang=layoutNormal.getTop();
if(isUp){
//scrollView向上滚动,scrollY小于改变位置是设置FloatView置顶
if(!(scrollY <=scrollChang)){
setFloatView(true);
}
}else{
//scrollView向下滚动,scrollY小于改变位置是设置FloatView置顶
if((scrollY <=scrollChang)){
setFloatView(false);
}
}
}
/**
* 改变悬浮窗的状态
* @param isFloat 是否悬浮
* */
private void setFloatView(boolean isFloat){
if(isFloat){
//悬浮置顶
if (floatView.getParent()!=layoutFloat) {
layoutNormal.removeView(floatView);
layoutFloat .addView (floatView);
}
}else{
//正常
if (floatView.getParent()!=layoutNormal) {
layoutFloat .removeView(floatView);
layoutNormal.addView (floatView);
}
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//判断ScrollView是否向上移动
if(t>oldt){
changFloatView(t,true);
}else{
changFloatView(t,false);
}
}
}
step3:
Xml
<RelativeLayout 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"
tools:context="xyz.linwill.ScrollViewDemo.MainActivity" >
<xyz.linwill.ScrollViewDemo.View.FloatScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="1\n2\n3\n4\n5\n上\n山\n打\n老\n虎"/>
<LinearLayout
android:id="@+id/layout_normal"
android:layout_width="match_parent"
android:layout_height="30dip"
android:orientation="vertical">
<!-- 正常状态下悬浮窗口的容器 -->
<LinearLayout
android:id="@+id/floatview"
android:layout_width="match_parent"
android:layout_height="30dip"
android:gravity="center"
android:background="#fae">
<TextView
android:id="@+id/text_1"
android:layout_width="0dp"
android:layout_height="30dip"
android:layout_weight="1"
android:text="选项卡1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#99f1a441"/>
<TextView
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="30dip"
android:layout_weight="1"
android:text="选项卡2"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#99f1a441"/>
<TextView
android:id="@+id/text_3"
android:layout_width="0dp"
android:layout_height="30dip"
android:layout_weight="1"
android:text="选项卡3"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#99f1a441"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="楼\n主\n已\n经\n被\n老\n虎\n咬\n死\n了\n,\n大\n家\n头\n七\n再\n来\n看\n楼\n主\n!"/>
</LinearLayout>
</xyz.linwill.ScrollViewDemo.View.FloatScrollView>
<LinearLayout
android:id="@+id/layout_float"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 置顶状态下悬浮窗口的容器 -->
</LinearLayout>
</RelativeLayout>
step4:
Activity
public class MainActivity extends Activity {
LinearLayout layoutNormal,layoutFloat;
LinearLayout floatView;
FloatScrollView ScrollView;
TextView t1,t2,t3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView =(FloatScrollView) findViewById(R.id.scrollview );
layoutNormal =(LinearLayout) findViewById(R.id.layout_normal);
layoutFloat =(LinearLayout) findViewById(R.id.layout_float );
floatView =(LinearLayout) findViewById(R.id.floatview );
t1 =(TextView) findViewById(R.id.text_1 );
t2 =(TextView) findViewById(R.id.text_2 );
t3 =(TextView) findViewById(R.id.text_3 );
ScrollView.setFloatView(layoutNormal, layoutFloat, floatView);
t1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "t1选中", Toast.LENGTH_SHORT).show();
}
});
t2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "t2选中", Toast.LENGTH_SHORT).show();
}
});
t3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "t3选中", Toast.LENGTH_SHORT).show();
}
});
}
}
step5:
附上源码 http://download.csdn.net/detail/qq_32182845/9565656
ps
第一次写博客,如有不对的地方请大牛们指正!