这是我的代码。它在数据不足时加载更多数据。我想在加载更多数据时向 GridView 添加页脚 ProgressBar 。怎么做?我在 StackOverflow 上看到了很多问题,但没有答案。
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private final List<Integer> commonImageList = new ArrayList<Integer>();
private int index = 0;
private boolean isLoading = false;
private static final int NUM = 18;
private ImageAdapter adapter;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Task task = new Task(mThumbIds, index);
task.setOnPostExecuteListener(new OnPostExecuteListener<List<Integer>>() {
@Override
public void onPostExecute(final List<Integer> result) {
System.out.println("RESULT: " + result);
if(result.isEmpty()) {
return;
}
// Store it.
commonImageList.addAll(result);
// Prepare gridView.
final GridView gridview = (GridView) findViewById(R.id.gridview);
adapter = new ImageAdapter(MainActivity.this, commonImageList);
gridview.setAdapter(adapter);
gridview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount) {
System.out.println("firstVisibleItem: " + firstVisibleItem);
System.out.println("visibleItemCount: " + visibleItemCount);
System.out.println("totalItemCount: " + totalItemCount);
final boolean loadMore = (firstVisibleItem + visibleItemCount >= totalItemCount);
System.out.println("loadMore: " + loadMore);
System.out.println("isLoading: " + isLoading);
if(loadMore && !isLoading) {
// TODO Show footer here.
ShowLog.showLogInfo(TAG, "============= LOAD MORE =============");
// Get more images.
index = index + NUM;
final Task task = new Task(mThumbIds, index);
task.setOnPreExecuteListener(new OnPreExecuteListener() {
@Override
public void onPreExecute() {
isLoading = true;
}
});
task.setOnPostExecuteListener(new OnPostExecuteListener<List<Integer>>() {
@Override
public void onPostExecute(final List<Integer> result) {
System.out.println("RESULT222: " + result);
if(result.isEmpty()) {
return;
}
System.out.println("HIEU THONG MINH");
// Update common list.
commonImageList.addAll(result);
// Update adapter.
adapter.notifyDataSetChanged();
isLoading = false;
}
});
task.execute();
}
}
});
}
});
task.execute();
}
private class Task extends AbstractWorkerTask<Void, Void, List<Integer>> {
private final Integer []thumbs;
private final int index;
public Task(final Integer []thumbs, final int index) {
this.thumbs = thumbs;
this.index = index;
}
@Override
protected List<Integer> doInBackground(final Void... params) {
final List<Integer> list = new ArrayList<Integer>();
for(int i = index; i < index + NUM && i < thumbs.length; ++i) {
list.add(thumbs[i]);
}
return list;
}
}
public static class ImageAdapter extends BaseAdapter {
private final Context context;
private final List<Integer> imageList;
public ImageAdapter(final Context c, final List<Integer> imageList) {
context = c;
this.imageList = imageList;
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public Object getItem(final int position) {
return null;
}
@Override
public long getItemId(final int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageList.get(position));
return imageView;
}
}
// references to our images
private static final Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
};
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="150dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>