zoukankan      html  css  js  c++  java
  • 下拉刷新--第三方开源--PullToRefresh

    效果预览图:

    下载地址:https://github.com/chrisbanes/Android-PullToRefresh

    activity_main.xml:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     tools:context="com.zzw.testpulltorefresh.MainActivity" >
     6 
     7     <com.handmark.pulltorefresh.library.PullToRefreshListView
     8         android:id="@+id/listView"
     9         android:layout_width="match_parent"
    10         android:layout_height="match_parent" />
    11 
    12 </RelativeLayout>

    MainActuvity.java:

     1 package com.zzw.testpulltorefresh;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Date;
     5 
     6 import com.handmark.pulltorefresh.library.PullToRefreshBase;
     7 import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
     8 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
     9 import com.handmark.pulltorefresh.library.PullToRefreshListView;
    10 
    11 import android.app.Activity;
    12 import android.os.AsyncTask;
    13 import android.os.Bundle;
    14 import android.os.SystemClock;
    15 import android.widget.Adapter;
    16 import android.widget.ArrayAdapter;
    17 import android.widget.ListView;
    18 import android.widget.TextView;
    19 import android.widget.Toast;
    20 
    21 public class MainActivity extends Activity {
    22 
    23     private PullToRefreshListView listView;
    24     private ArrayList<String> data;
    25     private ArrayAdapter adapter;
    26     private int count = 0;
    27 
    28     @Override
    29     protected void onCreate(Bundle savedInstanceState) {
    30         super.onCreate(savedInstanceState);
    31         setContentView(R.layout.activity_main);
    32 
    33         data = new ArrayList<String>();
    34 
    35         listView = (PullToRefreshListView) findViewById(R.id.listView);
    36         // 设置向下滑动时刷新
    37          listView.setMode(Mode.PULL_FROM_START);
    38         // 支持下拉和上拉  listView.setMode(Mode.BOTH);    
    39         // 设置监听
    40         listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
    41 
    42             @Override
    43             public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    44                 // 在这完成业务逻辑
    45                 new MyAsyncTask().execute();
    46             }
    47         });
    48 
    49         adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
    50         listView.setAdapter(adapter);
    51 
    52         // 设置如果数据为空的时候显示什么
    53         TextView textView = new TextView(this);
    54         textView.setText("请下拉刷新");
    55         listView.setEmptyView(textView);
    56     }
    57 
    58     private class MyAsyncTask extends AsyncTask {
    59 
    60         @Override
    61         protected void onPreExecute() {
    62             // 开始刷新
    63             listView.setRefreshing();
    64         }
    65 
    66         @Override
    67         protected Object doInBackground(Object... params) {
    68             // 假设耗时时间为3秒
    69             SystemClock.sleep(3000);
    70             return count++;
    71         }
    72 
    73         @Override
    74         protected void onPostExecute(Object result) {
    75 
    76             data.add(0, result + "");
    77             adapter.notifyDataSetChanged();
    78 
    79             // 设置标签
    80             listView.setLastUpdatedLabel("最后更新新的时间:" + new Date());
    81 
    82             // 刷新完成
    83             listView.onRefreshComplete();
    84             Toast.makeText(getApplicationContext(), "加载成功", 0).show();
    85         }
    86     }
    87 
    88 }
  • 相关阅读:
    【IDEA配置】web项目报错404 login.html找不到资源或无法访问
    完成一个IDEA web项目(二)登录功能实现
    完成一个IDEA web项目(一)项目搭建准备工作
    Servlet中写了注解@WebServlet但访问servlet报404错误
    Category分类测试报错:Category annotations on Parameterized classes are not supported on individual methods.
    Junit测试报错:java.lang.AssertionError at org.junit.Assert.assertTrue
    集合Set添加多个元素
    【IDEA配置】IDEA新建maven web项目
    【IDEA配置】IDEA新建web项目
    JSON
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4992194.html
Copyright © 2011-2022 走看看