这是我的代码.数据不足时,它将加载更多数据.我想在加载更多数据时向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"/>
解决方法:
这是我在GrIDVIEw中添加加载更多项目的逻辑
1.在适配器的输入数据的最后创建一个伪造的物品
public class MediaGrIDAdapter extends BaseAdapter { private ArrayList<Media> List; private final Media special = new Media("-1", "", "", "", ""); public MediaGrIDAdapter(Context context, int imageID, ArrayList<Media> array, int type) { List = array; if(List != null) { List.add(special); } }public voID appendDataList(ArrayList<Media> appendedList, boolean isEnd) { //called in postExecute to append new data //remove special element in original List List.remove(List.size() - 1); //append collection of media to List List.addAll(appendedList); //check to add special element if(!isEnd) { List.add(special); }} }
2.在getVIEw方法中:检查它是否是最后一个位置(是我们的假货),为此返回特殊布局(进度条…).
if(position == (List.size() - 1)) { Context context = parent.getContext(); item = (relativeLayout) LayoutInflater.from(context).inflate(R.layout.item_special_more, null); item.setTag(MORE_button); return item; }
最后一个onItemClick检查标记以启动getMoreAsyncTask
if (v.getTag().equals(MediaGrIDAdapter.MORE_button)) { GetMoreItems task = new GetMoreItems(); task.execute(url); return; }
总结 以上是内存溢出为你收集整理的android-如何在GridView的最后一项之后添加页脚ProgressBar全部内容,希望文章能够帮你解决android-如何在GridView的最后一项之后添加页脚ProgressBar所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)