Android使用GridView和OnScrollListener

时间:2012-08-29 03:24:52

标签: android gridview android-asynctask onscroll

我正在构建一个包含大量1000张图片的图片的网格视图。为了减少gridview的加载时间,我想使用AbsListView实现onScrollListener。我将限制每个视图20个图像,然后当滚动到达底部时,它将加载另外20个图像。这将重复,直到所有图像都已加载。

我一直在谷歌搜索样本,但似乎找不到适合我的代码。谁能指导我怎么做?我目前正在使用图像加载器Lazylist作为我的图像。我也在使用AsyncTask。

MainGridView.class

    public class MainGridView extends SherlockActivity {

    private ProgressDialog pDialog;
    ArrayList<HashMap<String, String>> songsList;
    static final String KEY_SONG = "song"; 
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_CAT_ARTIST = "artistcat";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String KEY_BIG_URL = "big_url";
    static final String KEY_CAT_URL = "cat_url";
    static String IMAGE_POSITION;
    GridView grid;
    MainGridViewLazyAdapter adapter;
    String cat_url;
    String artist_url;
    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_main);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        checkInternetConnection();

        grid = (GridView) findViewById(R.id.grid_view);




    grid.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        if (loading) {
            if (visibleItemCount > totalItemCount) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // I load the next page of gigs using a background task,
            // but you can call any function here.
            new loadGridView().execute(currentPage + 1);
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }
    });


}

    public class loadGridView extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainGridView.this);
            pDialog.setTitle("Connect to Server");
            pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(Integer... args) {
            // updating UI from Background Thread
                    Intent in = getIntent();
                    songsList = new ArrayList<HashMap<String, String>>();
                    cat_url = in.getStringExtra(KEY_CAT_URL);
                    artist_url = in.getStringExtra(KEY_CAT_ARTIST);


                    XMLParser parser = new XMLParser();
                    String xml = parser.getXmlFromUrl(cat_url); // getting XML from URL
                    Document doc = parser.getDomElement(xml); // getting DOM element

                    NodeList nl = doc.getElementsByTagName(KEY_SONG);
                    // looping through all song nodes <song>
                    for (int i = 0; i < nl.getLength(); i++) {
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        Element e = (Element) nl.item(i);
                        // adding each child node to HashMap key => value
                        map.put(KEY_ID, parser.getValue(e, KEY_ID));
                        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                        map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                        // adding HashList to ArrayList
                        songsList.add(map);

                    }
                    return null;
                }   


        @Override
        protected void onPostExecute(String args) {
            ActionBar   ab = getSupportActionBar();
                        ab.setTitle(artist_url);

            adapter=new MainGridViewLazyAdapter(MainGridView.this, songsList);   
            grid.setAdapter(adapter);
            pDialog.dismiss();
            }
        }

1 个答案:

答案 0 :(得分:5)

我想你想从Urls加载图片。为此,您可以使用此解决方案:Lazy load of images in ListView

为了使用onScrollListener控制您的分页,我想您希望看到Endless scrolling example。这是一个简单的示例,但您必须添加的唯一内容是检查是否已到达xml的最后一项。

AsyncTask(在每次新加载时调用)中,您只需解析xml中的一些数据(对应于您的情况下的20个项目)。这是基于您的代码的使用示例:

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) {

        }
    }
}

为方便起见,我在GridView内使用了文本而不是图像。我专注于分页系统所以我没有解析XML,只是动态添加了一些随机值。

此外,您可以考虑使用相同的addItemsAsyncTask在每次加载时仅下载xml的一部分(20个项目),而不是在创建GridView之前下载整个1000多个项目。