整个是一个scrollView,嵌套一个线性布局,下拉刷新、或者上拉加载后,通过addView()方法,加载消息体,每一个消息体是一个复杂的子view。
做一个类似qq客户端“好友动态“刷新的东西,在网上找了很多,大体有3种布局类型的下拉刷新,listView、scrollView、gridView。
下拉刷新的原理网上很详细,简单点说,,拿 "好友动态" 举例,有
1、ElasticScrollViewActivity.java(对应的xml文件是main.xml,里面嵌套com.ElasticScrollView.view.ElasticScrollView)、
2、ElasticScrollView.java(继承自scrollView,定义一个ScrollView类,然后再friendCircleActivity里定义一个scrollView,scrollView的addChild方法,或者通过activityactivity_friendCircle布局里面的LinearLayout friendList的addView方法,添加singleMessage。scrollView绑定onFreshListener。这个类里面定义刷新的各个状态,下拉刷新头的更新等,下拉刷新header各个控件:下拉箭头、相关文字提示、刷新日期提示等,里面通过inflate引入headerView、coverImageView,本身是个View,没有专门对应xml文件)、
3、SingleMessageView.java(下拉刷新后,动态添加的一条条消息体,是消息体的布局,通过inflate引入sing_msg.xml),
4、activity_friendCircle.xml 就是在你的滚动布局scrollFreshview里面,根据需要再添加一个类似 ”好友动态“里自定义封面的view视图
5、single_msg.xml,定义每个消息体的布局
框架结构如图:
动态添加view,用的是linearLayout的addView方法,这样,需要在scrollFreshview的代码中定义一个LayoutInflate innerLayout,然后在这个layout里面inflate进headerView、coverImageView:
[java] view plain copy
1. LayoutInflater inflater = LayoutInflater.from(context);
2. innerLayout = new
3. innerLayout.setLayoutParams(new
4. LinearLayout.LayoutParams.FILL_PARENT,
5. LinearLayout.LayoutParams.WRAP_CONTENT));
6. innerLayout.setOrientation(LinearLayout.VERTICAL);
7.
8. headView = (LinearLayout) inflater.inflate(R.layout.mylistview_head,
9. null);
10. coverScrollView = (ScrollView) inflater.inflate(R.layout.activity_friends_circle,
11. null);
12. innerLayout.addView(headView);
13. innerLayout.addView(coverScrollView);
14. addView(innerLayout);
来源参考:http://www.apkbus.com/android-51424-1-1.html(怎么加自定义封面,怎么定义动态添加消息的顺序,博主写的很详细,原博主用的是thread线程添加的,我的用的是异步任务)
添加上拉加载功能的时候,需要修改一下touchEvent事件,并且添加一个footView:
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (this.getScrollY() == 0 && !isRecored) {
startY = (int) ev.getY();
isRecored = true;
}
break;
case MotionEvent.ACTION_MOVE:
int tempY = (int) ev.getY();
if (!isScroll)
isScroll = true;
if (!isRecored && this.getScrollY() == 0) {
isRecored = true;
startY = tempY;
}
if (tempY - startY < 0) {
startY = tempY;
}
if (state != REFRESHING && state != LOADING && isRecored) {
if (state == RELEASE_To_REFRESH) {
if ((tempY - startY) > 0
&& (tempY - startY) / RATIO < headContentHeight + 10) {
state = PULL_To_REFRESH;
changeHeaderViewByState();
} else if (tempY - startY <= 0) {
state = DONE;
changeHeaderViewByState();
}
}
if (state == PULL_To_REFRESH) {
if ((tempY - startY) / RATIO >= headContentHeight + 10) {
state = RELEASE_To_REFRESH;
isBack = true;
changeHeaderViewByState();
} else if (tempY - startY <= 0) {
state = DONE;
changeHeaderViewByState();
}
}
if (state == DONE) {
if (tempY - startY > 0) {
state = PULL_To_REFRESH;
changeHeaderViewByState();
isSeeHead = true;
}
}
if (state == RELEASE_To_REFRESH) {
headView.setPadding(0, (tempY - startY) / RATIO
- headContentHeight, 0, 0);
headView.invalidate();
} else if (state == PULL_To_REFRESH) {
headView.setPadding(0, -1 * headContentHeight
+ (tempY - startY) / RATIO, 0, 0);
headView.invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
Message message = new Message();
message.obj = myScrollView;
handler.sendMessageDelayed(message, 5);
if (state == PULL_To_REFRESH) {
state = DONE;
changeHeaderViewByState();
innerlayout_checkVisibility();
}
if (state == RELEASE_To_REFRESH) {
state = REFRESHING;
changeHeaderViewByState();
onRefresh();
}
isRecored = false;
isSeeHead = false;
isBack = false;
viewHeight = innerLayout.getHeight();
y = this.getScrollY();
if ((viewHeight - y - this.getHeight()) == 0 && isScroll
&& state != REFRESHING && state != LOADING) {
onLoad();
}
isScroll = false;
break;
}
if (isSeeHead) {
return true;
} else {
return super.onTouchEvent(ev);
}
}
像定义刷新接口一样,这时也需要定义一个上拉加载的load接口,整个scrollView绑定上拉加载监听器,定义Onload()方法,
public void setOnLoadListener(OnLoadListener loadListener) {
this.loadListener = loadListener;
}
public interface OnLoadListener {
public void onLoad();
}
private void onLoad() {
if (loadListener != null) {
state = LOADING;
footView.setVisibility(View.VISIBLE);
moreProgressBar.setVisibility(View.VISIBLE);
loadMoreView.setText("正在加载更多...");
loadListener.onLoad();
//onRefresh();
}
}
public void onLoadComplete() {
state = DONE;
moreProgressBar.setVisibility(View.GONE);
loadMoreView.setText("上拉加载更多");
//footView.setVisibility(View.INVISIBLE);
}
然后在朋友圈界面,绑定load监听器,定义上拉加载的异步任务就可以了。下拉刷新、上拉加载的区别,就是上拉加载需要实时判定用户的动作,更改刷新header的状态,而下拉加载就是一个固定在整个view的尾部的布局,当用户手指离开屏幕,并且滑动偏移量滑到底端的时候执行onload()方法就ok了。