Android Using GridView with OnScrollListener

后端 未结 1 636
傲寒
傲寒 2020-12-14 05:00

I\'m building a gridview that contains alot of images likely around 1000++ pictures. To reduce the loading time for the gridview, I want to implement an onScrollListener usi

相关标签:
1条回答
  • 2020-12-14 05:33

    I suppose you want to load images from Urls. For that purpose, you could use this solution: Lazy load of images in ListView

    In order to control your paging using onScrollListener, I think you want to see the Endless scrolling example. This is a simplistic example, but the only thing you have to add is a check if you have reached the last item of your xml.

    Inside your AsyncTask (called at each new loading), you would only parse a bit of data from your xml (corresponding to 20 items in your case). This is an example of use based on your code:

    public class MainGridView extends SherlockActivity implements OnScrollListener {
    
        public static final String KEY_SONG = "song"; 
        public static final String KEY_ID = "id";
        public static final String KEY_TITLE = "title";
        public static final String KEY_ARTIST = "artist";
        public static final String KEY_CAT_ARTIST = "artistcat";
        public static final String KEY_DURATION = "duration";
        public static final String KEY_THUMB_URL = "thumb_url";
        public static final String KEY_BIG_URL = "big_url";
        public static final String KEY_CAT_URL = "cat_url";
    
        private final static int ITEMS_PPAGE = 20;
    
        private ProgressDialog mDialog;
        private ArrayList<HashMap<String, String>> mSongsList;
    
        private static String IMAGE_POSITION;
        private GridView mGridView;
        //MainGridViewLazyAdapter mAdapter;
        private ArrayAdapter<String> mAdapter;
        private String cat_url;
        private String artist_url;
    
        private int mVisibleThreshold = 5;
        private int mCurrentPage = 0;
        private int mPreviousTotal = 0;
        private boolean mLoading = true;
        private boolean mLastPage = false;
    
        private String mXml;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            setTheme(SherlockBarUtils.getCurrentTheme(this));
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gridview_main);
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            //checkInternetConnection();
    
            mGridView = (GridView) findViewById(R.id.grid_view);
            mGridView.setOnScrollListener(this);
    
            mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            //mAdapter=new MainGridViewLazyAdapter(MainGridView.this);  
            mGridView.setAdapter(mAdapter);
    
            new DownloadDataAsyncTask().execute();
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
            if (mLoading) {
                if (totalItemCount > mPreviousTotal) {
    
                    mLoading = false;
                    mPreviousTotal = totalItemCount;
                    mCurrentPage++;
    
                    // Find your own condition in order to know when you 
                    // have finished displaying all items
                    if (mCurrentPage + 1 > 50) {
                        mLastPage = true;
                    }
                }
            }
            if (!mLastPage && !mLoading && 
                    (totalItemCount - visibleItemCount) <= (firstVisibleItem + mVisibleThreshold)) {
                new AddItemsAsyncTask().execute();
                mLoading = true;
            }
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
    
        }
    
        public class DownloadDataAsyncTask extends AsyncTask<String, String, String> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                mDialog = new ProgressDialog(MainGridView.this);
                mDialog.setTitle("Connect to Server");
                mDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
                mDialog.setIndeterminate(false);
                mDialog.setCancelable(false);
                mDialog.show();
            }
            @Override
            protected String doInBackground(String... args) {
    
                // Downloading XML
    
                /*
                Intent in = getIntent();
                mSongsList = new ArrayList<HashMap<String, String>>();
                cat_url = in.getStringExtra(KEY_CAT_URL);
                artist_url = in.getStringExtra(KEY_CAT_ARTIST);
    
                XMLParser parser = new XMLParser();
                mXml = parser.getXmlFromUrl(cat_url); // getting XML from URL
                */
    
                return null;
            }   
    
            @Override
            protected void onPostExecute(String args) {
    
                /*
                getSupportActionBar().setTitle(artist_url);
                */
                mDialog.dismiss();
                mLoading = false;
            }
        }
    
        public class AddItemsAsyncTask extends AsyncTask<Integer, String, String> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
            @Override
            protected String doInBackground(Integer... args) {
    
                MainGridView.this.runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
    
                        // Parsing 20 more items and adding them to the adapter
                        for (int i = mCurrentPage * ITEMS_PPAGE; i < (mCurrentPage + 1) * ITEMS_PPAGE; i++) {
                            MainGridView.this.mAdapter.add(String.valueOf(Math.random() * 5000));
                        }
                        MainGridView.this.mAdapter.notifyDataSetChanged();
                    }
                });
                return null;
            }
    
            @Override
            protected void onPostExecute(String args) {
    
            }
        }
    }
    

    For the convenience, I used texts instead of images inside the GridView. I focused on the paging system so I didn't parse XML, just added some random values on the fly.

    Also, you could consider using the same addItemsAsyncTask to download only a part (20 items) of your xml at each loading instead of downloading the whole 1000+ items before creating the GridView.

    0 讨论(0)
提交回复
热议问题