推荐几个ListView下拉刷新

找到了一些下拉刷新和其他的一些写好的代码,我只用到了下拉刷新一类,想分享一下。

布局文件:
eshListView
        android:id="@+id/ptrlv_main"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
Activity:
private void initListView() {
       listView = ptrListView.getRefreshableView();
       datas = new ArrayList();
       adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, datas);
       //为ListView添加若干个头部
       LayoutInflater inflater = LayoutInflater.from(this);
       View listHeaderIcons = inflater.inflate(R.layout.header_list_icons, listView, false);
       View listHeaderSquares = inflater.inflate(R.layout.header_list_square, listView, false);
       View listHeaderAds = inflater.inflate(R.layout.header_list_ads, listView, false);
       View listHeaderCategories = inflater.inflate(R.layout.header_list_categories, listView, false);
       View listHeaderRecommend = inflater.inflate(R.layout.header_list_recommend, listView, false);

       listView.addHeaderView(listHeaderIcons);
       listView.addHeaderView(listHeaderSquares);
       listView.addHeaderView(listHeaderAds);
       listView.addHeaderView(listHeaderCategories);
       listView.addHeaderView(listHeaderRecommend);
       listView.setAdapter(adapter);
       //添加下拉松手后的刷新
       ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
           @Override
           public void onRefresh(PullToRefreshBase refreshView) {

               new Handler().postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       datas.add(0, "新增内容");
                       adapter.notifyDataSetChanged();
                       ptrListView.onRefreshComplete();
                   }
               }, 1500);
           }
       });

   }

   @Override
   protected void onResume() {
       super.onResume();
       refresh();
   }

   private void refresh() {
       datas.add("aaa");
       datas.add("bbb");
       datas.add("ccc");
       datas.add("ddd");
       datas.add("eee");
       datas.add("fff");
       datas.add("ggg");
       datas.add("hhh");
       datas.add("jjj");
       adapter.notifyDataSetChanged();

   }
}

源码请点击

你可能感兴趣的:(推荐几个ListView下拉刷新)