Android实战简易教程<十九>(SwipeRefreshLayout下拉刷新使用实例)
2015-09-25 11:11
806 查看
我们来看SwipeRefreshLayout的具体用法,顾名思义此组件就是一个布局,只不过要注意的是此布局内只能有一个直接子View。其实通过文档我们可以知道SwipeRefreshLayout只不过是继承了ViewGroup。
查看文档,我们可以知道,在SwipRefreshLayout中存在一个接口,通过此接口我们可以监听滑动手势,其实使用此组件最重要的步骤就是实现此接口的onRefresh方法,在此方法中实现数据的更新操作。如下:
接口中的方法:
除了OnRefreshListener接口外,SwipRefreshLayout中还有一些其他重要的方法,具体如下:
1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器。
2、setProgressBackgroundColor(int colorRes):设置进度圈的背景色。
3、setColorSchemeResources(int… colorResIds):设置进度动画的颜色。
4、setRefreshing(Boolean refreshing):设置组件的刷新状态。
5、setSize(int size):设置进度圈的大小,只有两个值:DEFAULT、LARGE。
下面我们通过一个实例来看一下具体怎么用
1.main.xml:
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
2.list_item.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center"
android:singleLine="true"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
3.ItemInfo.java:
[java] view
plaincopy
package com.demo.downrefresh;
/**
* ListView中item属性
*
* @author w.w
*/
public class ItemInfo {
/**
* id
*/
private int id;
/**
* name
*/
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.ListViewAdapter.java:
[java] view
plaincopy
package com.demo.downrefresh;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* ListView适配器
* @author w.w
*/
public class ListViewAdapter extends ArrayAdapter<ItemInfo> {
private LayoutInflater inflater;
public ListViewAdapter(Context context, List<ItemInfo> list) {
super(context, 0, list);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemInfo info = getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, null);
}
TextView name = (TextView) convertView.findViewById(R.id.item_name);
name.setText(info.getName());
return convertView;
}
}
5.MainActivity.java:
[java] view
plaincopy
package com.demo.downrefresh;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
/**
* 主页
* @author w.w
*/
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
/**
* 给ListView添加下拉刷新
*/
private SwipeRefreshLayout swipeLayout;
/**
* ListView
*/
private ListView listView;
/**
* ListView适配器
*/
private ListViewAdapter adapter;
private List<ItemInfo> infoList;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);
swipeLayout.setOnRefreshListener(this);
// 顶部刷新的样式
swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,
android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
infoList = new ArrayList<ItemInfo>();
ItemInfo info = new ItemInfo();
info.setName("coin");
infoList.add(info);
listView = (ListView) this.findViewById(R.id.listview);
adapter = new ListViewAdapter(this, infoList);
listView.setAdapter(adapter);
}
/**
* 下拉刷新
*/
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
swipeLayout.setRefreshing(false);
ItemInfo info = new ItemInfo();
info.setName("coin-refresh"+i);
infoList.add(info);
i++;
adapter.notifyDataSetChanged();//刷新ListView
}
}, 500);
}
}
6.运行实例如下:
总结
1.此布局内只能有一个直接子View;
2.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器;
3.覆写public void onRefresh()。
查看文档,我们可以知道,在SwipRefreshLayout中存在一个接口,通过此接口我们可以监听滑动手势,其实使用此组件最重要的步骤就是实现此接口的onRefresh方法,在此方法中实现数据的更新操作。如下:
接口中的方法:
除了OnRefreshListener接口外,SwipRefreshLayout中还有一些其他重要的方法,具体如下:
1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器。
2、setProgressBackgroundColor(int colorRes):设置进度圈的背景色。
3、setColorSchemeResources(int… colorResIds):设置进度动画的颜色。
4、setRefreshing(Boolean refreshing):设置组件的刷新状态。
5、setSize(int size):设置进度圈的大小,只有两个值:DEFAULT、LARGE。
下面我们通过一个实例来看一下具体怎么用
1.main.xml:
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
2.list_item.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center"
android:singleLine="true"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
3.ItemInfo.java:
[java] view
plaincopy
package com.demo.downrefresh;
/**
* ListView中item属性
*
* @author w.w
*/
public class ItemInfo {
/**
* id
*/
private int id;
/**
* name
*/
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.ListViewAdapter.java:
[java] view
plaincopy
package com.demo.downrefresh;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* ListView适配器
* @author w.w
*/
public class ListViewAdapter extends ArrayAdapter<ItemInfo> {
private LayoutInflater inflater;
public ListViewAdapter(Context context, List<ItemInfo> list) {
super(context, 0, list);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemInfo info = getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, null);
}
TextView name = (TextView) convertView.findViewById(R.id.item_name);
name.setText(info.getName());
return convertView;
}
}
5.MainActivity.java:
[java] view
plaincopy
package com.demo.downrefresh;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
/**
* 主页
* @author w.w
*/
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
/**
* 给ListView添加下拉刷新
*/
private SwipeRefreshLayout swipeLayout;
/**
* ListView
*/
private ListView listView;
/**
* ListView适配器
*/
private ListViewAdapter adapter;
private List<ItemInfo> infoList;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);
swipeLayout.setOnRefreshListener(this);
// 顶部刷新的样式
swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,
android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
infoList = new ArrayList<ItemInfo>();
ItemInfo info = new ItemInfo();
info.setName("coin");
infoList.add(info);
listView = (ListView) this.findViewById(R.id.listview);
adapter = new ListViewAdapter(this, infoList);
listView.setAdapter(adapter);
}
/**
* 下拉刷新
*/
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
swipeLayout.setRefreshing(false);
ItemInfo info = new ItemInfo();
info.setName("coin-refresh"+i);
infoList.add(info);
i++;
adapter.notifyDataSetChanged();//刷新ListView
}
}, 500);
}
}
6.运行实例如下:
总结
1.此布局内只能有一个直接子View;
2.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器;
3.覆写public void onRefresh()。
相关文章推荐
- Android实战简易教程<十八>(ViewPager组件详解)
- Android实战简易教程<十七>(LayoutAnimation布局动画)
- Android Studio利用Gradle删除没有使用到的资源文件
- Android实战简易教程<十六>(LineChart实现数据趋势展示)
- 记录 iOS 与 Android 生命周期对比
- Android实战简易教程<十五>(实现ListView中Button点击事件监听)
- android.os.NetworkOnMainThreadException异常的处理
- Android实战简易教程<十四>(介绍SwipeMenuListView-简单实现类似QQ的滑动删除效果)
- android手势事件传递(实例:下拉刷新)
- Android实战简易教程<十三>(五大布局研究)
- android 电容屏(一):电容屏基本原理篇
- Android程序开发中Handler需要复写…
- Android BLE开发笔记
- mac 搭建android开发环境
- Android学习【5】自定义ArrayAdapter
- android 四大组件之 BroadCastReceiver
- android 风格和主题
- Android应用Context详解及源码解析
- Android总结(更新中...)
- Android、Android Studio 操作记录