XScrollView下拉刷新控件详解
2015-12-14 15:57
387 查看
前段时间写了XListView下拉刷新控件详解,今天介绍一下它的兄弟篇《XScrollView下拉刷新控件详解》。没有看过XListView下拉刷新控件详解的,可以先去看一下,本文在这里就不重复相同部分了,原理和功能样式都是类似的。
XScrollView在initWithContext()这个方法中有所区别。
这里主要的区别在于ScrollView需要自己定义HeaderView和FooterView.
XScrollView提供了两种方法来把用户的页面添加到下拉刷新控件中去。一种是添加ViewGroup,另一种是添加View。
在ScrollView中判断滚动到顶部或是底部与ListView不同。
其他都是与XListView类似。
XScrollView在initWithContext()这个方法中有所区别。
private void initWithContext(Context context) { //XScrollView自定义分为三部分:header_layout,content_layout, footer_layout mLayout = (LinearLayout) View.inflate(context, R.layout.vw_xscrollview_layout, null); //content_layout原来显示用户自己定义的页面,后面有方法提供,可以添加进来 mContentLayout = (LinearLayout) mLayout .findViewById(R.id.content_layout); mScroller = new Scroller(context, new DecelerateInterpolator()); // XScrollView need the scroll event, and it will dispatch the event to // user's listener (as a proxy). this.setOnScrollListener(this); // init header view //XHeaderView,实例化一个XHeaderView出来,并且添加到headerLayout中,当然默认的高度设置为0 mHeader = new XHeaderView(context); mHeaderContent = (RelativeLayout) mHeader .findViewById(R.id.header_content); // mHeaderTime = (TextView) mHeader.findViewById(R.id.header_hint_time); LinearLayout headerLayout = (LinearLayout) mLayout .findViewById(R.id.header_layout); headerLayout.addView(mHeader); // init footer view //XFooterView,实例化一个XFooterView,设置一些参数后添加到footLayout中, mFooterView = new XFooterView(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); params.gravity = Gravity.CENTER; LinearLayout footLayout = (LinearLayout) mLayout .findViewById(R.id.footer_layout); footLayout.addView(mFooterView, params); // init header height ViewTreeObserver observer = mHeader.getViewTreeObserver(); if (null != observer) { observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { //得到XHeaderView的正常高度,对后面下拉刷新高度判断很有用 mHeaderHeight = mHeaderContent.getHeight(); ViewTreeObserver observer = getViewTreeObserver(); if (null != observer) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { observer.removeGlobalOnLayoutListener(this); } else { observer.removeOnGlobalLayoutListener(this); } } } }); } this.addView(mLayout); }
这里主要的区别在于ScrollView需要自己定义HeaderView和FooterView.
XScrollView提供了两种方法来把用户的页面添加到下拉刷新控件中去。一种是添加ViewGroup,另一种是添加View。
public void setContentView(ViewGroup content) { //添加ViewGroup类的布局到ContentLayout中,在这之前会把ContentLayout中的其他View先移除掉 if (mLayout == null) { return; } if (mContentLayout == null) { mContentLayout = (LinearLayout) mLayout .findViewById(R.id.content_layout); } if (mContentLayout.getChildCount() > 0) { mContentLayout.removeAllViews(); } mContentLayout.addView(content); } public void setView(View content) { //将用户定义的布局,添加到content_layout中去 if (mLayout == null) { return; } if (mContentLayout == null) { mContentLayout = (LinearLayout) mLayout .findViewById(R.id.content_layout); } mContentLayout.addView(content); }
在ScrollView中判断滚动到顶部或是底部与ListView不同。
private boolean isTop() { //判断是否滚动到顶部 return getScrollY() <= 0 || mHeader.getVisibleHeight() > mHeaderHeight || mContentLayout.getTop() > 0; } private boolean isBottom() { //是否滚动到底部 return Math.abs(getScrollY() + getHeight() - computeVerticalScrollRange()) <= 5 || (getScrollY() > 0 && null != mFooterView && mFooterView .getBottomMargin() > 0); }
其他都是与XListView类似。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories