您的位置:首页 > 其它

加载更多和刷新加载问题总结

2014-02-21 11:36 465 查看
对于加载更多问题可以有很多种,目前我们前面的平台采用的是以下方式加载更多,话不多说,直接附上代码……


public class CompanyView {

private ArrayList<Shop> shops, addShops;
private DataParser parser;
private ListView companyList;
private ShopListAdapter adapter;
private Context ctx;
private int page = 1;// 页码
private View moreView;// 加载更多
private TextView moreText;
private int visibleLastIndex;// 可见的最后一项
private int visibleCount;// 可见的数目

public void setContent(Context ctx, View lay) {
this.ctx = ctx;
companyList = (ListView) lay.findViewById(R.id.category_shop_list);
if (CheckNet.netIsAvaliable(ctx)) {
new AsynLoad(ctx).execute(Config.CATEGORYACTIVITY_MSG);
}
companyList.setOnItemClickListener(itemListener);
moreView = ((CategoryActivity) ctx).getLayoutInflater().inflate(
R.layout.add_more_layout, null);
companyList.addFooterView(moreView);
moreText = (TextView) moreView.findViewById(R.id.add_more_text);
moreView.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
page++;
addMore();
}
});
companyList.setOnScrollListener(listScrollListener);
}

// 从网络上获取数据
public void initData() {
parser = new DataParser();
shops = parser.parserShops(Constant.replacePage(1));
}

// 为Listview装载数据
public void setData() {
if (shops != null) {
adapter = new ShopListAdapter(ctx, shops);
companyList.setAdapter(adapter);
}
}

private AdapterView.OnItemClickListener itemListener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent i = new Intent(ctx, ShopActivity.class);
Bundle data = new Bundle();
data.putSerializable("shop", shops.get(position));
i.putExtras(data);
ctx.startActivity(i);
}
};

// 加载更多
private void addMore() {
moreText.setText("加载中......");
new Handler().postDelayed(new Runnable() {

public void run() {
// TODO Auto-generated method stub
boolean flag = loadMore();
adapter.notifyDataSetChanged();
companyList.setSelection(visibleLastIndex - visibleCount + 1);
if (flag) {
moreText.setText("更多");
} else {
moreText.setText("加载完毕了");
}
}
}, 2000);
}

// 加载数据
private boolean loadMore() {
String url;
parser = new DataParser();
url = Constant.replacePage(page);// 页码替换
Log.i("more", url);
addShops = parser.parserShops(url);
Log.i("page===============>", "" + page);
if (addShops == null || addShops.size() == 0) {
return false;
} else {
for (Shop shop : addShops) {
adapter.addShop(shop);
}
return true;
}
}

private OnScrollListener listScrollListener = new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
visibleCount = visibleItemCount;
visibleLastIndex = visibleItemCount + firstVisibleItem - 1;
}
};

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: