XListview刷新和加载

//继承IXListViewListener
public class MainActivity extends Activity implements OnItemClickListener,IXListViewListener

private int STATE_1 = 1;
private int STATE_2 = 2;
private int STATE_3;

lv2 = (XListView) findViewById(R.id.lv);
lv2.setPullLoadEnable(true);//上拉刷新
lv2.setXListViewListener(this);//给xListView设置监听

 

//判断,如果有数据先清空
 if (STATE_3 == STATE_1) {
news .clear();
}

 

//更新适配器
if (adapter == null) {
adapter = new MyAdapter(news,this);
lv2.setAdapter(adapter);
} else {
 adapter .notifyDataSetChanged();
 }
 load();

 

public void load(){
//停止更新
lv2.stopRefresh();
//加载更多
lv2.stopLoadMore();
//更新时间
lv2.setRefreshTime("刚刚");
}

@Override
public void onRefresh() {
STATE_3 = STATE_1;//设置判断
huodeshuju();
}

@Override
public void onLoadMore() {
STATE_3 = STATE_2;
huodeshuju();
}

 

<string name="xlistview_header_hint_normal">下拉刷新</string>
<string name="xlistview_header_hint_ready">松开刷新数据</string>
<string name="xlistview_header_hint_loading">正在加载...</string>
<string name="xlistview_header_last_time">上次更新时间:</string>
<string name="xlistview_footer_hint_normal">查看更多</string>
<string name="xlistview_footer_hint_ready">松开载入更多</string>

 

 

//XlistviewHandler中

public class XListViewHeader extends LinearLayout {
private LinearLayout mContainer;
private ImageView mArrowImageView;
private ProgressBar mProgressBar;
private TextView mHintTextView;
private int mState = STATE_NORMAL;

private Animation mRotateUpAnim;
private Animation mRotateDownAnim;

private final int ROTATE_ANIM_DURATION = 180;

public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_REFRESHING = 2;

public XListViewHeader(Context context) {
super(context);
initView(context);
}

/**
* @param context
* @param attrs
*/
public XListViewHeader(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}

private void initView(Context context) {
// 初始情况,设置下拉刷新view高度为0
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, 0);
// 将下拉刷新的view添加进来
mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.xlistview_header, null);
addView(mContainer, lp);
setGravity(Gravity.BOTTOM);

mArrowImageView = (ImageView) findViewById(R.id.xlistview_header_arrow);
mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
mProgressBar = (ProgressBar) findViewById(R.id.xlistview_header_progressbar);
// 准备向上和向下的动画
mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 设置旋转时间间隔
mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateUpAnim.setFillAfter(true);
mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateDownAnim.setFillAfter(true);
}

public void setState(int state) {
if (state == mState)
return;
// 如果当前状态是正在刷新
if (state == STATE_REFRESHING) { // 显示进度
// 移除动画
mArrowImageView.clearAnimation();
// 设置imageView不可见
mArrowImageView.setVisibility(View.INVISIBLE);
// 设置progressBar可见
mProgressBar.setVisibility(View.VISIBLE);
} else { // 显示箭头图片
// 只要不是正在刷新,进度条可见,imageView不可见
mArrowImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
}

switch (state) {
case STATE_NORMAL:
if (mState == STATE_READY) {
mArrowImageView.startAnimation(mRotateDownAnim);
}
if (mState == STATE_REFRESHING) {
mArrowImageView.clearAnimation();
}
mHintTextView.setText(R.string.xlistview_header_hint_normal);
break;
case STATE_READY:
if (mState != STATE_READY) {
mArrowImageView.clearAnimation();
mArrowImageView.startAnimation(mRotateUpAnim);
mHintTextView.setText(R.string.xlistview_header_hint_ready);
}
break;
case STATE_REFRESHING:
mHintTextView.setText(R.string.xlistview_header_hint_loading);
break;
default:
}
mState = state;
}

public void setVisiableHeight(int height) {
if (height < 0)
height = 0;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
.getLayoutParams();
lp.height = height;
mContainer.setLayoutParams(lp);
}

public int getVisiableHeight() {
return mContainer.getLayoutParams().height;
}

}

 

posted on 2016-07-28 19:25  巫山老妖  阅读(256)  评论(0编辑  收藏  举报