Android -- 固定在ScrollView顶部的View,类似于新浪微博的评论列表的顶部

来源:互联网 发布:linux系统运维面试题 编辑:程序博客网 时间:2024/05/16 11:09
现在很多App都实现了这个功能,例如新浪微博评论页面的评论、转发、赞的数字可以固定在屏幕上方。我个人很喜欢这种设计,所以利用一点空余时间简单实现了一个类似的功能。


先来看一下上面这张图的效果。

这个是新浪微博的一个页面,整体布局大致分了三块:正文内容、转发评论赞的数字条、评论列表

其中数字条是可以跟着ScrollView一起滑动,但在滑到最顶部时固定在最上面,而下面的评论内容可以继续滑动。

这种效果还是挺赞的,但一开始没有什么思路,所以就去搜了下相关技术代码,一下就恍然大悟!原来是自己想复杂了,其实原理很简单!


下面是自己实现的效果图:



实现原理:

    当滚动条划过头部时,把需要固定的头部从父布局中移除,然后添加到最外层布局的顶部。

    当滚动条返回时又把最外层的头部移然后重新添加到原来的父布局里面。

    整个实现代码,不算上布局,也就100行左右


详细实现逻辑:

首先建一个自定义View叫MyHoveringScrollView继承自FrameLayout,在布局里MyHoveringScrollView处于最外层。由于FrameLayout本身是不支持滚动条的,所以在FrameLayout内部有一个自定义的ScrollView。

在初始化的时候,通过getChildAt(0)把子布局拿到,然后清空整个布局,然后实例化一个自己的ScrollView,把之前拿到的子布局添加到ScrollView里面,

最后把ScrollView添加到MyHoveringScrollView里面。

[java] view plain copy
  1. public void init() {  
  2.         post(new Runnable() {  
  3.             @Override  
  4.             public void run() {  
  5.                 mContentView = (ViewGroup) getChildAt(0);  
  6.                 removeAllViews();  
  7.    
  8.                 MyScrollView scrollView = new MyScrollView(getContext(), MyHoveringScrollView.this);  
  9.                 scrollView.addView(mContentView);  
  10.                 addView(scrollView);  
  11.    
  12.             }  
  13.         });  
  14.     }  

?

可能注意到了两点:

1、我用了post()。因为在构造方法里面布局还没有生成,getChildAt(0)是拿不到东西的,但是post()会把动作放到队列里,等布局完成后再从队列里取出来,所以这里是个小窍门。

2、我把MyHoveringScrollView传入到了ScrollView里面,这么做其实是为了让ScrollView回调MyHoveringScrollView的方法。(比较懒,不想写接口……)


然后通过setTopView()方法,把需要固定在顶部的ID传进来:

[java] view plain copy
  1. public void setTopView(final int id) {  
  2.         post(new Runnable() {  
  3.             @Override  
  4.             public void run() {  
  5.                 mTopView = (ViewGroup) mContentView.findViewById(id);  
  6.    
  7.                 int height = mTopView.getChildAt(0).getMeasuredHeight();  
  8.                 ViewGroup.LayoutParams params = mTopView.getLayoutParams();  
  9.                 params.height = height;  
  10.                 mTopView.setLayoutParams(params);  
  11.                 mTopViewTop = mTopView.getTop();  
  12.                 mTopContent = mTopView.getChildAt(0);  
  13.    
  14.             }  
  15.         });  
  16.     }  

接下来,在ScrollView里面重写onScrollChanged()方法,并回调给MyHoveringScrollView的onScroll方法:

[java] view plain copy
  1. private static class MyScrollView extends ScrollView {  
  2.    
  3.         private MyHoveringScrollView mScrollView;  
  4.    
  5.         public MyScrollView(Context context, MyHoveringScrollView scrollView) {  
  6.             super(context);  
  7.             mScrollView = scrollView;  
  8.         }  
  9.    
  10.    
  11.         @Override  
  12.         protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  13.             super.onScrollChanged(l, t, oldl, oldt);  
  14.             mScrollView.onScroll(t);  
  15.         }  
  16.    
  17.     }  
  18. public void onScroll(final int scrollY) {  
  19.         post(new Runnable() {  
  20.             @Override  
  21.             public void run() {  
  22.                 if (mTopView == null  
  23.                         ) return;  
  24.    
  25.                 if (scrollY >= mTopViewTop  
  26.                         && mTopContent.getParent() == mTopView) {  
  27.                     mTopView.removeView(mTopContent);  
  28.                     addView(mTopContent);  
  29.                 } else if (scrollY < mTopViewTop  
  30.                         && mTopContent.getParent() == MyHoveringScrollView.this) {  
  31.                     removeView(mTopContent);  
  32.                     mTopView.addView(mTopContent);  
  33.                 }  
  34.    
  35.             }  
  36.         });  
  37.     }  


?

如果scrollY >= mTopViewTop就是头部应该被固定在顶部的时候

如果scrollY < mTopViewTop就是头部应该取消固定,还原到原来父布局的时候

至此,功能就实现了!

    

怎么使用呢?首先先写布局:

[html] view plain copy
  1. <com.hide.myhoveringscroll.app.MyHoveringScrollView  
  2.         xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:id="@+id/view_hover"  
  4.         android:layout_width="match_parent"  
  5.         android:layout_height="match_parent">  
  6.     <LinearLayout android:layout_width="match_parent"  
  7.                   android:layout_height="match_parent"  
  8.                   android:orientation="vertical"  
  9.             >  
  10.         <TextView android:layout_width="match_parent"  
  11.                   android:layout_height="300dp"  
  12.                   android:text="这是头部"  
  13.                   android:gravity="center"  
  14.                 />  
  15.    
  16.         <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"  
  17.                      android:id="@+id/top"  
  18.                 >  
  19.             <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"  
  20.                           android:padding="20dp"  
  21.                           android:background="#AAff0000"  
  22.                           android:orientation="horizontal">  
  23.                 <TextView android:layout_width="0dp" android:layout_height="wrap_content"  
  24.                           android:layout_weight="1"  
  25.                           android:gravity="center"  
  26.                           android:layout_gravity="center"  
  27.                           android:textSize="16sp"  
  28.                           android:text="这是固定部分"  
  29.                         />  
  30.                 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"  
  31.                         android:text="点我一下"  
  32.                         android:id="@+id/btn"  
  33.                         />  
  34.    
  35.             </LinearLayout>  
  36.         </FrameLayout>  
  37.    
  38.         <TextView android:layout_width="match_parent" android:layout_height="wrap_content"  
  39.                   android:paddingTop="10dp"  
  40.                   android:text="内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n内容\n"  
  41.                 />  
  42.    
  43.     </LinearLayout>  
  44. </com.hide.myhoveringscroll.app.MyHoveringScrollView>  

?

其中:MyHoveringScrollView在最外层,充当ScrollView的角色(所以子布局只能有一个)

android:id="@+id/top也就是需要固定在顶部的布局


最后回到Activity:

[java] view plain copy
  1. view_hover = (MyHoveringScrollView) findViewById(R.id.view_hover);  
  2. view_hover.setTopView(R.id.top);  

?

两句话就实现了固定头部的效果。


源代码地址:https://github.com/w9xhc/MyHoveringScroll

0 0
原创粉丝点击