PullToRefreshListView实现根据拖拉的方向进行在顶部或者底部加载数据。

1、导入library这个工程,然后右击需要使用到PullToRefreshListView的工程,选择Properties,选择android,在右下角点击Add,添加library这个工程。(关于library这个工程可以到网搜:PullToRefresh 。如果真找不到,给我留言,我发给你。)

2、在显示列表的xml配置:

         <com.handmark.pulltorefresh.library.PullToRefreshListView

           android:id="@+id/showContent"

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:cacheColorHint="#00000000"

           android:divider="#19000000"

           android:dividerHeight="4dp"

           android:fadingEdge="none"

           android:fastScrollEnabled="false"

           android:footerDividersEnabled="false"

           android:headerDividersEnabled="false"

            android:smoothScrollbar="true" />

3、在Activity中的配置:

         a.声明

private List<Map<String, Object>> list = newArrayList<Map<String, Object>>();

        private PullToRefreshListView mPullRefreshListView;

        private boolean isStart = true;//是否从上往下来

    b. mPullRefreshListView =(PullToRefreshListView) findViewById(R.id.showContent);

    c.设置下来监听:

    // 设定下拉监听函数

        mPullRefreshListView

                .setOnRefreshListener(newOnRefreshListener<ListView>() {

                    @Override

                    public void onRefresh(

                            PullToRefreshBase<ListView>refreshView) {

                        Stringlabel = DateUtils.formatDateTime(

                                getApplicationContext(),

                                System.currentTimeMillis(),

                                DateUtils.FORMAT_SHOW_TIME

                                        |DateUtils.FORMAT_SHOW_DATE

                                        |DateUtils.FORMAT_ABBREV_ALL);

 

                        // Update the LastUpdatedLabel

                       

                        refreshView.getLoadingLayoutProxy()

                                .setLastUpdatedLabel(label);

                        //refreshView.getCurrentMode()判断上下拖动的方向

                        if(refreshView.getCurrentMode().equals(Mode.PULL_FROM_START)){

                            isStart = true;

                        }

                        else{

                            isStart = false;

                        }

                        // Do work to refresh the list here.

                        newGetDataTask().execute();

                       

                    }

                });     mPullRefreshListView.setMode(Mode.BOTH);// 设置底部下拉刷新模式

 

        //listItem = getData();// 获取LIST数据

        adapter = new MyAdapter(MainActivity.this,list);

 

        // 设置适配器

        mPullRefreshListView.getRefreshableView().setAdapter(adapter);

    d.后台处理数据,动态获取数据

    private class GetDataTask extends AsyncTask<Void,Void, HashMap<String, Object>> {

 

        //后台处理部分

        @Override

        protectedHashMap<String, Object> doInBackground(Void... params) {

            // Simulates a background job.

            try {

                Thread.sleep(1000);

            }catch(InterruptedException e) {

            }

            List<NameValuePair>params1 = newArrayList<NameValuePair>();

            params1.add(new BasicNameValuePair("id", "1"));

            params1.add(new BasicNameValuePair("user_id","90"));

            HttpPosthttpPost = newHttpPost(PATH);

            try {

                httpPost.setEntity(newUrlEncodedFormEntity(params1));

                HttpResponsehttpResponse = newDefaultHttpClient().execute(httpPost);

                int response =httpResponse.getStatusLine().getStatusCode();

                data = EntityUtils.toString(httpResponse.getEntity());

            }catch(Exception e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }

            HashMap<String,Object> map = newHashMap<String, Object>();

            try {

                num = num + 1;

                map= newHashMap<String, Object>();

                map.put("num", "德玛西亚");

                map.put("content",data+(num));

               

            }catch(Exception e) {

                // TODO: handle exception

                setTitle("map出错了");

                return null;

            }

           

            return map;

        }

 

        //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView

        //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值

        @Override

        protected voidonPostExecute(HashMap<String, Object> result) {

            //在头部增加新添内容

           

            try {

                List<Map<String,Object>> list1 = new ArrayList<Map<String,Object>>();

                list1.add(result);

                if(isStart){

                    adapter.addListTop(list1);

                }

                else{

                    adapter.addListBottom(list1);

                }

                //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合

                adapter.notifyDataSetChanged();

                // Call onRefreshComplete when the list has beenrefreshed.

                mPullRefreshListView.onRefreshComplete();

            }catch(Exception e) {

                // TODO: handle exception

                setTitle(e.getMessage());

            }

            super.onPostExecute(result);

        }

    }

4、在自定义适配器中添加

public voidaddListTop(List<Map<String, Object>> list1){

        list1.addAll(list);

        list.removeAll(list);

        list.addAll(list1);

    }

    public voidaddListBottom(List<Map<String, Object>> list1){

        list.addAll(list1);

    }


以上是参考别人的PullToRefreshListView 实现再根据自己的需要进行修改的,实现了从上往下拖在最顶部加载,从下往上拉在最底部加载。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值