重写ScrollView实现ScrollView可以添加悬浮条
来源:互联网 发布:维控触摸屏编程教程 编辑:程序博客网 时间:2024/06/05 17:36
- 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);}}}@Overrideprotected 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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "t1选中", Toast.LENGTH_SHORT).show();}});t2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "t2选中", Toast.LENGTH_SHORT).show();}});t3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "t3选中", Toast.LENGTH_SHORT).show();}});}}
step5:
附上源码 http://download.csdn.net/detail/qq_32182845/9565656
ps
第一次写博客,如有不对的地方请大牛们指正!
0 0
- 重写ScrollView实现ScrollView可以添加悬浮条
- 自定义ScrollView,实现导航条悬浮置顶
- android Scrollview嵌套PinnedHeaderExpandableListView并且可以实现悬浮窗
- ScrollView实现悬浮标题栏效果
- 上滑ScrollView,实现控件顶部悬浮
- 重写ScrollView实现两个ScrollView的同步滚动显示
- 重写ScrollView实现两个ScrollView的同步滚动显示
- 重写ScrollView实现两个ScrollView的同步滚动显示
- 重写ScrollView实现两个ScrollView的同步滚动显示
- ScrollView中浮动条的实现
- xib拖拽scrollview实现可以滑动
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- ios xml库选择
- redis info 命令查看redis使用情况
- 深入浅出RPC(二)
- IOS上计算文件MD5和计算数据块MD5和复制文件耗时数据
- Linux内核---1.TQ2440使用总结
- 重写ScrollView实现ScrollView可以添加悬浮条
- nodejs一文件系统
- Codeforces Round #358 (Div. 2) A Alyona and Numbers(水题)
- Intellij IDEA 编译提示: "Test is already defined as object Test"
- Codeforces Round #360 (Div. 2) A. Opponents
- CSS学习day01---XHTML和HTML的重要区别
- linux服务器jre和tomcat配置
- StackExchange和它的游戏规则
- Linux内核---2.TQ2440系统移植出现的问题总结