如何使用分页显示大量数据,我尝试使用滚动视图,但我认为显示大量数据并不正确。
答案 0 :(得分:0)
<强> 1。无尽的ScrollView类
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold =4;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
// System.out.println("totalItemCount test" +totalItemCount);
// System.out.println("previousTotalItemCount test" +previousTotalItemCount);
// System.out.println("loading test" +loading);
// System.out.println("currentPage test" +currentPage);
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it's still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn't currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
loading = onLoadMore(currentPage, totalItemCount);
}
}
// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
<强> 2。适配器类
public class OpenJobAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
protected int serverListSize = -1;
public static final int VIEW_TYPE_LOADING = 0;
public static final int VIEW_TYPE_ACTIVITY = 1;
public OpenJobAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return mOpenJobList.size() + 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
System.out.println("position :" + position);
System.out.println("mSpottedDishList.size() :" + mOpenJobList.size());
return (position >= mOpenJobList.size()) ? VIEW_TYPE_LOADING
: VIEW_TYPE_ACTIVITY;
}
@Override
public Object getItem(int position) {
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? mOpenJobList
.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
return getFooterView(position, convertView, parent);
}
View dataRow = convertView;
dataRow = getDataRow(position, convertView, parent);
return dataRow;
}
public View getFooterView(int position, View convertView,
ViewGroup parent) {
System.out.println("loading view :" + mLoadingFinished);
if (position >= serverListSize && mLoadingFinished) {
View view = new View(context);
return view;
}
View row = convertView;
if (row == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_footer, null);
}
return row;
}
public View getDataRow(final int position, View convertView,
ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final viewHold viewholder;
if (convertView == null) {
viewholder = new viewHold();
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.openjobs_items, null);
viewholder.mCustomerName = (TextView) convertView
.findViewById(R.id.customername);
viewholder.mDateAndTime = (TextView) convertView
.findViewById(R.id.dateandtime);
viewholder.mCategoryName = (TextView) convertView
.findViewById(R.id.categoryname);
viewholder.mIssue = (TextView) convertView
.findViewById(R.id.issue);
viewholder.mRatings = (TextView) convertView
.findViewById(R.id.ratings);
convertView.setTag(viewholder);
} else {
viewholder = (viewHold) convertView.getTag();
}
try {
viewholder.mCustomerName.setText(mOpenJobList.get(position).get(
"serviceCategory"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return convertView;
}
class viewHold {
public TextView mCustomerName, mDateAndTime, mCategoryName, mIssue, mRatings;
}
}
第3。 Listview滚动侦听器
mListOpenJob.setOnScrollListener(new EndlessScrollListener(0, 0) {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
if (Utils.isOnline(OpenJobsActivity.this)) {
//Calling api
new OpenJobData().execute(page);
} else {
Utils.displayToast(OpenJobsActivity.this, getString(R.string.nonet));
}
return true;
}
});
4.Calling Api并确保所有页面都已完成或未使用布尔标志
private boolean getopenjoblist(int skip) {
String url;
url = "Your url";
try {
JSONObject json = new JSONObject(HttpUtil.apiGETResponseWithHeaders(url, getApplicationContext()));
if (json.getString("http_code").equals("400")) {
mLoadingFinished = true;
}
//do whatever here
} catch (Exception e) {
}
return true;
}