您的位置:首页 > 产品设计 > UI/UE

XScrollView下拉刷新控件详解

2015-12-14 15:57 387 查看
前段时间写了XListView下拉刷新控件详解,今天介绍一下它的兄弟篇《XScrollView下拉刷新控件详解》。没有看过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类似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息