Android的ListView的下拉和上拉 [英] Android ListView Pull Down and Pull Up

查看:182
本文介绍了Android的ListView的下拉和上拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android应用程序开发的新。我想知道如何实现的下拉刷新和拉起的列表视图从服务器获得更多的项目。
我该怎么办呢?
我的code得到物品从服务器的下面。

I am new on Android Applications development. I want to know how to implement Pull down refresh and Pull up get more item from server in Listview. How can I do it? My Code get items from server is below.

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

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        // Create a progressbar
        if (!prefs) {

            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected Void doInBackground(Void... params) {

        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions.getJSONfromURL("Server URL",latitude, longitude);
        try {

            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("places");

            for (int i = 0; i < jsonarray.length(); i++) {

                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);

                double convertString = Double.parseDouble(jsonobject.getString("distance"));
                double convertKMToMile = convertString * 0.6124;
                String convertedValue = String.format("%.2f",convertKMToMile);
                // Retrive JSON Objects
                map.put("places_name", jsonobject.getString("places_name"));
                map.put("distance", convertedValue);
                map.put("places_icon", jsonobject.getString("places_icon"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {

            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        //Toast.makeText(getActivity(), getString(jsonarray.length()), 6000).show();
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(getActivity(), arraylist);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressbar
        progressBar.setVisibility(View.GONE);
        if (prefs) {

            prefs = false;
        }
    }
}

感谢您。

推荐答案

有是一个不错的库的 https://github.com/Maxwin-z/XListView-Android

有关刷新:

@Override
public void onRefresh() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            start = ++refreshCnt;
            items.clear();
            geneItems();
            // mAdapter.notifyDataSetChanged();
            mAdapter = new ArrayAdapter<String>(XListViewActivity.this, R.layout.list_item, items);
            mListView.setAdapter(mAdapter);
            onLoad();
        }
    }, 2000);
}

有关负载更多:

@Override
public void onLoadMore() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            geneItems();
            mAdapter.notifyDataSetChanged();
            onLoad();
        }
    }, 2000);
}

这篇关于Android的ListView的下拉和上拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆