列表下拉刷新

本文介绍了一种在Android应用中实现Listview下拉刷新的方法。通过使用PullToRefreshListView组件,结合异步任务(AsyncTask)进行数据加载,演示了如何设置刷新监听并更新UI。同时展示了具体的代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

listview下拉刷新

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       android:id="@+id/pull_refresh_list"

       />

</RelativeLayout>

MainActivity
    // 找控件
        listView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        // 请求数据
        new GetDataTask()
                .execute("数据地址");


----------
// listview刷新监听
        listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(
                        getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
                                | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                // 再次请求数据
                new GetDataTask().execute("地址");

            }
        });


----------
//异步操作
    private class GetDataTask extends AsyncTask<String, integer, String> {

        private String str;

        @Override//运行在子线程做耗时操作
        protected String doInBackground(String... params) {

            String string = params[0];
            HttpClient httpClient = new DefaultHttpClient();
            try {
                HttpResponse response = httpClient
                        .execute(new HttpPost(string));
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    str = EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return str;
        }

        @Override//运行在主线程
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.i("Main", result);
            Gson gson = new Gson();
            MyBean myBean = gson.fromJson(result, MyBean.class);
            list = myBean.data;
            mAdapter = new MyBaseAdapter(MainActivity.this, list);
            listView.setAdapter(mAdapter);

            //每次请求完数据通知适配器数据改变
            mAdapter.notifyDataSetChanged();
            //通知listview刷新完成
            listView.onRefreshComplete();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

![下拉刷新的效果](http://img.blog.csdn.net/20160331205125714)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值