使用Refresh ListView 实现Android下拉刷新
翻译自http://www.androidhive.info/2015/05/android-swipe-down-to-refresh-listview-tutorial/
你或许已经发现很多安卓应用,比如说Twitter,Google+提供了下拉刷新,不管什么时候用户下拉,就会有一个加载的小圆圈在显示,当新的内容加在完了就会消失。在这一篇教程里,我们会叫你如何实现。
我们可以通过自定义空间然后去监听下拉的动作来完成它,但安卓本身提供了SwipeRefreshLayout ,这个是在android.support.v4 包引入的,让我们可以更简单的实现。
1 Android SwipeRefreshLayout
实现 Android SwipeRefreshLayout很简单,不管什么时候你想监听下拉时间,是要把view包裹在SwipeRefreshLayout 中即可,在我们的例子里我们将使用ListView。然后你需要在你的activity类中实现SwipeRefreshLayout.OnRefreshListener.借口,当下拉动工作时hi自动调用onRefresh() 方法,你需要在这个方法中采取何时的行动,比如说请求http数据。
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- place your view here -->
</android.support.v4.widget.SwipeRefreshLayout>
2 JSON例子
为了模拟数据,我打算在ListView中显示IMDB的前250名电影,我创建了一个json service,一起可以给出20部电影,你需要传递 offset参数去活的结果,默认的offset应该是0,不管什么时候下拉,我们发起http请求去获取接下来的20部电影。
URL: http://api.androidhive.info/json/imdb_top_250.php?offset=0
3 创建Android项目
我们使用volley来发起http请求,所以请在build.gradle 添加依赖
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
}
打开colors.xml,我们来添加一下item的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="movie_serial_bg">
<item>#24c6d5</item>
<item>#57dd86</item>
<item>#ad7dcf</item>
<item>#ff484d</item>
<item>#fcba59</item>
<item>#24c6d5</item>
</string-array>
</resources>
创建MyApplication.java 类,这是一个单例的MyApplication用来初始化volley 核心对象。
package info.androidhive.swiperefresh.app;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by Ravi on 13/05/15.
*/
public class MyApplication extends Application {
public static final String TAG = MyApplication.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
打开清单文件,把我们的MyApplication.java 加入到application节点下。并且加入网络权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.swiperefresh">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
穿件我们的ListView的item布局文件。
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="25dp"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/title"
android:layout_toRightOf="@id/serial"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="18dp" />
</RelativeLayout>
我们的java bean对象
Movie.java
package info.androidhive.swiperefresh.helper;
/**
* Created by Ravi on 13/05/15.
*/
public class Movie {
public int id;
public String title;
public Movie() {
}
public Movie(int id, String title) {
this.title = title;
this.id = id;
}
}
创建我们的适配器类
SwipeListAdapter.java
package info.androidhive.swiperefresh.helper;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import info.androidhive.swiperefresh.R;
/**
* Created by Ravi on 13/05/15.
*/
public class SwipeListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieList;
private String[] bgColors;
public SwipeListAdapter(Activity activity, List<Movie> movieList) {
this.activity = activity;
this.movieList = movieList;
bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
}
@Override
public int getCount() {
return movieList.size();
}
@Override
public Object getItem(int location) {
return movieList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
TextView serial = (TextView) convertView.findViewById(R.id.serial);
TextView title = (TextView) convertView.findViewById(R.id.title);
serial.setText(String.valueOf(movieList.get(position).id));
title.setText(movieList.get(position).title);
String color = bgColors[position % bgColors.length];
serial.setBackgroundColor(Color.parseColor(color));
return convertView;
}
}
好了,我们把所有需要的文件都弄好了,现在开始写真正的refresh view.。打开我们main activity的布局文件,把ListView包裹在SwipeRefreshLayout里
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
最后代开activity类,
package info.androidhive.swiperefresh.activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import info.androidhive.swiperefresh.R;
import info.androidhive.swiperefresh.app.MyApplication;
import info.androidhive.swiperefresh.helper.Movie;
import info.androidhive.swiperefresh.helper.SwipeListAdapter;
public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
private String TAG = MainActivity.class.getSimpleName();
private String URL_TOP_250 = "http://api.androidhive.info/json/imdb_top_250.php?offset=";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Movie> movieList;
// 初始化offset参数,在稍后的解析json里会进行更新
private int offSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
movieList = new ArrayList<>();
adapter = new SwipeListAdapter(this, movieList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* 显示转圈的动画,因为在onCreate不会开启动画,使用post runnable
*
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchMovies();
}
}
);
}
/**
* 下拉的时候会调用这个方法
*/
@Override
public void onRefresh() {
fetchMovies();
}
/**
* 通过http获取json
*/
private void fetchMovies() {
// 在http请求之前展示动画
swipeRefreshLayout.setRefreshing(true);
// baoffset加到url后面
String url = URL_TOP_250 + offSet;
// Volley's json array请求
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// 循环json添加movie
for (int i = 0; i < response.length(); i++) {
try {
JSONObject movieObj = response.getJSONObject(i);
int rank = movieObj.getInt("rank");
String title = movieObj.getString("title");
Movie m = new Movie(rank, title);
movieList.add(0, m);
// 更新offset
if (rank >= offSet)
offSet = rank;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// 停止刷新
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// 停止刷新
swipeRefreshLayout.setRefreshing(false);
}
});
// 把请求加入到请求队列中
MyApplication.getInstance().addToRequestQueue(req);
}
}
最终的结果