昨天,测试人员提了一个Bug,说是只要来回的点击同一个Tab按钮,加载的数据就会重复,检查代码检查了好长时间也没有发现有啥问题,于是就打断点调试,发现本来是执行的pullToRefreshListView.setRefreshing(); 自以为是执行的刷新操作(其实也就是刷新操作,只不过上拉是刷新,下拉也是刷新,我们做的时候是把上拉刷新改成加载更多了),其实该控件并不能判断出来目前执行的操作是下拉刷新还是上拉加载更多,这就需要自定义一个布尔变量isAutoRefresh,通过它来识别是不是下拉刷新的动作,下面给出实现的源码:
public class MyTopicFragment extends Fragment implements MyTopicObserver,
OnItemClickListener, OnRefreshListener,
OnLastItemVisibleListener {
private int mType = 0;
private PullToRefreshListView pullToRefreshListView;
private ListView actualListView;
private MyTopicListAdapter myTopicListAdapter;
private Context mContext;
private MyTopicLogic logic;
private int currentPageCount = 0; // 记录当前为第几页
private int totalPageCount; // 记录总页数
private boolean isRefreshAction;// 标志是否是下拉刷新
private MyTopicEntity mMyTopicEntity;
private LinearLayout promptView;// 无任何话题的时候显示的提示语
private LinearLayout mLoadingHint = null;
private LinearLayout mAfterLodingHint = null;
private ImageView mHintImage = null;
private TextView mHintTxt = null;
private boolean isAutoRefresh = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = this.getArguments();
mType = args.getInt("type");
mContext = this.getActivity();
logic = new MyTopicLogic();
logic.addObserver(this);
View rootView = inflater.inflate(
R.layout.fragment_pulltorefresh_listview, container, false);
initialize(rootView);
return rootView;
}
private void initialize(View rootView) {
pullToRefreshListView = (PullToRefreshListView) rootView
.findViewById(R.id.fragment_pulltorefresh_listview);
myTopicListAdapter = new MyTopicListAdapter();
actualListView = pullToRefreshListView.getRefreshableView();
registerForContextMenu(actualListView);
actualListView.setAdapter(myTopicListAdapter);
pullToRefreshListView.setMode(Mode.BOTH);
pullToRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel(
getString(R.string.poll_refresh_down_loadmore));
pullToRefreshListView.getLoadingLayoutProxy(false, true)
.setRefreshingLabel(
getString(R.string.poll_refresh_down_refreshing));
pullToRefreshListView.getLoadingLayoutProxy(false, true)
.setReleaseLabel(getString(R.string.poll_refresh_down_release));
pullToRefreshListView.getLoadingLayoutProxy(false, true)
.setLastUpdatedLabel("");
pullToRefreshListView.setOnRefreshListener(this);
pullToRefreshListView.setOnLastItemVisibleListener(this);
pullToRefreshListView.setOnItemClickListener(this);
mMyTopicEntity = HealthApplication.mAPPCache.getMyTopic(mType);
if (mMyTopicEntity != null && mMyTopicEntity.getData() != null) {
myTopicListAdapter.refresh(mMyTopicEntity.getData().getData());
currentPageCount = 1;
}
promptView = (LinearLayout) LayoutInflater.from(mContext).inflate(
R.layout.net_request_prompt, null);
pullToRefreshListView.setEmptyView(promptView);
if (mType == 1) {// 默认全部显示我发布的话题
mHandler.sendEmptyMessageDelayed(1, 500);
//logic.getMyTopic(10, 1, mType, "time");
}/*
* else if(mType == 2){ logic.getMyTopic(10, 1, mType, "time"); }else
* if(mType == 3){ logic.getMyTopic(10, 1, mType, "time"); }
*/
// mType = 1;//设置为1,只要是进入我发布的,还没有点击过我回复的和我收藏的,刷新操作将会执行我发布的话题
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
//pullToRefreshListView.setCurrentModeRefreash();
isAutoRefresh = true;
pullToRefreshListView.setRefreshing();
break;
default:
break;
}
}
};
@Override
public void onPause() {
logic.removeObserver(this);
super.onPause();
}
@Override
public void onResume() {
if (logic.getObservers().size() == 0) {
logic.addObserver(this);
}
super.onResume();
}
@Override
public void onItemClick(AdapterView> listView, View itemView,
int position, long itemViewId) {
MyTopicDetail item = (MyTopicDetail) myTopicListAdapter
.getItem(position - 1);
Intent intent = new Intent(mContext, TopicDetailActivity.class);
intent.putExtra("topicId", item.getId());
startActivityForResult(intent, 18);
}
@Override
public void onLastItemVisible() {
if (pullToRefreshListView.getMode() == Mode.PULL_FROM_START) {
// Toast.makeText(mContext, "没有更多话题了", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRefresh(PullToRefreshBase refreshView) {
if (refreshView.isHeaderShown() || isAutoRefresh) {
isAutoRefresh = false;
isRefreshAction = true;
logic.getMyTopic(10, 1, mType, "time");
} else {
isRefreshAction = false;
logic.getMyTopic(10, currentPageCount + 1, mType, "time");
}
}
public void refresh(int type) {
isRefreshAction = true;
mType = type;
mHandler.sendEmptyMessageDelayed(1, 500);
//logic.getMyTopic(10, 1, mType, "time");
}
private class MyTopicListAdapter extends BaseAdapter {
private ArrayList mList;
public MyTopicListAdapter() {
mList = new ArrayList();
}
public void refresh(ArrayList arrayList) {
mList.clear();
mList = arrayList;
notifyDataSetChanged();
}
public void add(ArrayList arrayList) {
mList.addAll(arrayList);
notifyDataSetChanged();
}
public void clear() {
mList.clear();
notifyDataSetChanged();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater
.inflate(R.layout.mytopic_list_item, null);
viewHolder = new ViewHolder();
viewHolder.topicName = (TextView) convertView
.findViewById(R.id.tv_topic_name);
viewHolder.topicContent = (TextView) convertView
.findViewById(R.id.tv_topic_content);
viewHolder.coloumnName = (TextView) convertView
.findViewById(R.id.column);
viewHolder.topicTime = (TextView) convertView
.findViewById(R.id.sendtime);
viewHolder.topicGoodCnt = (TextView) convertView
.findViewById(R.id.tv_good);
viewHolder.topicReplyCnt = (TextView) convertView
.findViewById(R.id.tv_replay);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
MyTopicDetail item = (MyTopicDetail) getItem(position);
if (item.getTitle().length() > 30) {
String str = item.getTitle().substring(0, 30);
viewHolder.topicName.setText(str + "...");
} else {
viewHolder.topicName.setText(item.getTitle());
}
if (item.getContent().length() > 80) {
String temp = (item.getContent().substring(0, 80));
viewHolder.topicContent.setText(temp + "...");
} else {
viewHolder.topicContent.setText(item.getContent());
}
if (item.getSectname().length() > 8) {
String temp = (item.getSectname().substring(0, 6));
viewHolder.coloumnName.setText(temp + "...");
} else {
viewHolder.coloumnName.setText(item.getSectname());
}
viewHolder.topicTime.setText(item.getCreate_time());
viewHolder.topicGoodCnt.setText(item.getGoodcnt() + "");
viewHolder.topicReplyCnt.setText(item.getDiscusscnt() + "");
return convertView;
}
private class ViewHolder {
public TextView topicName;// 话题名称
public TextView topicContent;// 话题内容
public TextView coloumnName;// 专栏名字
public TextView topicTime;// 话题时间
public TextView topicGoodCnt;// 赞的个数
public TextView topicReplyCnt;// 回复的个数
}
}
@Override
public void onGetMyTopicSuccess(MyTopicEntity result) {
pullToRefreshListView.onRefreshComplete();
if (result.getData() == null) {
showPromptView(false, getString(R.string.topic_nodata), 1);
myTopicListAdapter.clear();
pullToRefreshListView.setMode(Mode.BOTH);
currentPageCount = 0;
HealthApplication.mAPPCache.setMyTopic(mType, "");
showPromptView(false, getResources().getString(R.string.temp_no_data), 1);
}
totalPageCount = result.getData().getPagecount();
if (isRefreshAction) {
myTopicListAdapter.refresh(result.getData().getData());
currentPageCount = 1;
String label = DateUtils.formatDateTime(mContext,
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
pullToRefreshListView.getLoadingLayoutProxy(true, false)
.setLastUpdatedLabel(label);
if (1 == totalPageCount) {
pullToRefreshListView.setMode(Mode.PULL_FROM_START);
} else {
pullToRefreshListView.setMode(Mode.BOTH);
}
} else {
myTopicListAdapter.add(result.getData().getData());
currentPageCount++;
if (currentPageCount >= totalPageCount) {
pullToRefreshListView.setMode(Mode.PULL_FROM_START);
}
}
}
@Override
public void onGetMyTopicFailed(int errorCode, String msg) {
pullToRefreshListView.onRefreshComplete();
if(errorCode == 17){
showPromptView(false, getString(R.string.topic_nodata), 1);
myTopicListAdapter.clear();
pullToRefreshListView.setMode(Mode.BOTH);
currentPageCount = 0;
HealthApplication.mAPPCache.setMyTopic(mType, "");
showPromptView(false, getResources().getString(R.string.temp_no_data), 1);
}else {
HaloToast.show(mContext, msg);
showPromptView(false, getResources().getString(R.string.req_fail_click_refresh), 2);
}
}
/**
* 加载及加载之后的提示语
*
* @param isProgressShow
* 是否显示进度框
* @param text
* 显示的文字
* @param type
* 控制显示哪一个布局:0标识显示正在加载,1标识请求成功,但是暂无数据,2标识请求失败
*/
private void showPromptView(boolean isProgressShow, String text, int type) {
mAfterLodingHint = (LinearLayout) promptView
.findViewById(R.id.after_loding_hint_layout);
mLoadingHint = (LinearLayout) promptView.findViewById(R.id.loding_hint);
mHintImage = (ImageView) promptView.findViewById(R.id.hint_image);
mHintTxt = (TextView) promptView.findViewById(R.id.hint_txt);
View progressBar = promptView
.findViewById(R.id.net_request_prompt_progressBar);
TextView promptTextView = (TextView) promptView
.findViewById(R.id.net_request_prompt_textView);
promptView.setClickable(false);
if (type == 0) {
mLoadingHint.setVisibility(View.VISIBLE);
mAfterLodingHint.setVisibility(View.GONE);
if (isProgressShow) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
promptTextView.setText(text);
} else if (type == 1) {
mLoadingHint.setVisibility(View.GONE);
mHintImage.setClickable(false);
mHintImage.setImageResource(R.drawable.no_data);
mHintTxt.setText(text);
mAfterLodingHint.setVisibility(View.VISIBLE);
} else if (type == 2) {
promptView.setClickable(true);
mLoadingHint.setVisibility(View.GONE);
mHintImage.setImageResource(R.drawable.click_refresh_selector);
mHintTxt.setText(text);
mHintImage.setClickable(true);
mHintImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
logic.getMyTopic(10, 1, mType, "time");
}
});
mAfterLodingHint.setVisibility(View.VISIBLE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 18 && resultCode == getActivity().RESULT_OK){
isRefreshAction = true;
logic.getMyTopic(10, 1, mType, "time");
}
super.onActivityResult(requestCode, resultCode, data);
}
}