如何在ListView末尾添加“更多”按钮以加载更多项目?

时间:2014-08-17 09:14:22

标签: android android-listview

我有一个ListView,其中首先想要加载14个项目并创建一个"更多"链接在前14个项目列表的末尾,当按下"更多"我必须加载更多14项等等.. 如何在ListView的末尾添加一个按钮或链接,只有在滚动到ListView的底部后才能看到..在某处有可用的教程或者请建议我出路

2 个答案:

答案 0 :(得分:8)

我会使用类似下面代码的东西:

// 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();
    }
}

希望这会有所帮助:)

答案 1 :(得分:1)

您可以假装在getCount()方法中总是还有一个项目,然后单击最后一个ListView项目。