簡體   English   中英

如何在ListView末尾添加“更多”按鈕以加載更多項目?

[英]How to add “more” button at the end of ListView to load more items?

我有一個ListView,其中首先要加載14個項目,並在前14個項目列表的末尾創建一個“更多”鏈接,當按“更多”時我必須加載更多14個項目等等。怎么可以我在ListView的末尾添加了一個按鈕或鏈接,只有在滾動到ListView的底部后才能看到..在某處有可用的教程或請建議我出路

我會使用類似下面的代碼:

// Create the Load More... button
Button btnLoadExtra = new Button(this);
btnLoadExtra.setText("Load More...");

// Adding Load More button to lisview at bottom
lv.addFooterView(btnLoadExtra);

btnLoadExtra.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // Starting a new async task
        new loadMoreListView().execute();
    }
});

然后,您需要創建一個新類,該類創建Async任務以加載其他數據:

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(
                AndroidListViewWithLoadMoreButtonActivity.this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Void doInBackground(Void... unused) {
        runOnUiThread(new Runnable() {
            public void run() {
                // increment current page
                current_page += 1;

                // Next page request
                URL = //enter the url for the extra data here.

                xml = parser.getXmlFromUrl(URL); // getting XML
                doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_ITEM);
                // looping through all item nodes <item>
                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_NAME, parser.getValue(e, KEY_NAME));

                    // adding HashList to ArrayList
                    menuItems.add(map);
                }

                // get listview current position - used to maintain scroll position
                int currentPosition = lv.getFirstVisiblePosition();

                // Appending new data to menuItems ArrayList
                adapter = new ListViewAdapter(
                        AndroidListViewWithLoadMoreButtonActivity.this,
                        menuItems);
                lv.setAdapter(adapter);
                // Setting new scroll position
                lv.setSelectionFromTop(currentPosition + 1, 0);

            }
        });

        return (null);
    }

    protected void onPostExecute(Void unused) {
        // closing progress dialog
        pDialog.dismiss();
    }
}

希望這可以幫助 :)

您可以假裝在getCount()方法中總是再有一個項目,然后處理最后一個ListView項目的單擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM