Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:12:27+00:00 2026-06-09T15:12:27+00:00

This is my code. It loads more data when being out of data. I

  • 0

This is my code. It loads more data when being out of data. I want to add a footer ProgressBar to GridView when it loads more data. How to do? I’ve seen many questions on StackOverflow but there is no answer for it.

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"
/>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:12:29+00:00Added an answer on June 9, 2026 at 3:12 pm

    Here the logic i did to add load more item in GridView
    1. Create a fake item at the last of Adapter’s input data

    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. In getView method : Check if it’s the last position (is our fake item) return special layout( progress bar…) for this.

        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;
        }
    

    The last in onItemClick check tag to start getMoreAsyncTask

    if (v.getTag().equals(MediaGridAdapter.MORE_BUTTON)) {    
    
                            GetMoreItems task = new GetMoreItems();
                            task.execute(url);
                            return;
                        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this simple code to load data from other php page using get
This code works anywhere else.. form load, button click, etc. But when I add
So i have this code: foreach (string xmlfilelisted in xmlFileList) { resultXml.Root.Add(XDocument.Load(xmlfilelisted).Root.Elements()); resultXml.Save(filepath); }
When i execute this code to load a image and store in database it
I use this code to load a .Net assembly to PowerShell: [System.Reflection.Assembly]::Load(System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
I have this code to load icon and application name , but it is
This code: $(#permalink a).click(function(id){ var id = this.getAttribute('href'); $(#newPostContent).load(id, function() { $(#removeTrigger).click(function() { $(#removeThis).hideToggle();
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I trying to load and save setting with this code but when I closing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.