I wanted to know is there any way to limit the amount of images display in gridview. I created a gridview and I might need display a lot of images. Therefore, I wanted to implement the system that only display 30 images by default and there will be a "Load more" button for the user to load more content into the gridview. But I'm kinda lost here and I got no idea how to progress or what to look for. I need some pointers regarding to this issue. Any comments will be appreciated. Thanks in advance.
Asked
Active
Viewed 1,144 times
1 Answers
3
The GridView uses a BaseAdapter to get its items, so if you want to change the amount to display you'll have to change it in the Adapter. Like This:
public class ImagePageAdapter extends BaseAdapter {
private boolean loadAll = false;
public void setTrue(){
loadAll = true;
}
@Override
public int getCount() {
if(loadAll) return getAmount();
return 30;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null) return getImage(position);
else{
//reuse convertview
}
}
So you just have to call setTrue() and let the GridView now it needs to reload.

Erik N
- 314
- 1
- 3
-
Thank you for the reply. So based on the answer that you provided it only will load 30 images by default and once you call setTrue() it loads all the images? And after it loads all the images I need to reload the GridView in order for the images to appear? – IssacZH. Dec 11 '12 at 08:23
-
for precision : GridView use ListAdapter. BaseAdapter happens to be one of them, not the only one, nor the simplest one to use. Your getView implementation looks weird, too. And `setTrue`? what kind of a method name is that? – njzk2 Dec 11 '12 at 08:31
-
I know It's just an example. As programmer I just assumed that you'd know method names don't matter for the functionality. Yes it loads 30 by default and you'll have to test weither it updates automatically or you have to call gridView.invalidate() or gridView.postInvalidate(). – Erik N Dec 11 '12 at 08:39