Android SwipeRefreshLayout下拉刷新(两种不阻碍线程的方式)
来源:互联网 发布:js弹出窗口 编辑:程序博客网 时间:2024/06/06 09:43
MainActivity需要的布局文件activity_main.xml,里面有SwipeRefreshLayout和ListView的写法。
效果如图:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" 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>
java代码,用AsyncTask实现:
package com.example.swiperefreshlayout;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private List<String> list;private ListView listview;private SwipeRefreshLayout swipeRefresh;private ArrayAdapter mAdapter;private int count = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list = new ArrayList<String>();String[] names = { "张三", "李四", "王二", "赵六", "陈五" };for (int i = 0; i < names.length; i++) {list.add(names[i]);}listview = (ListView) findViewById(R.id.list_view);mAdapter = new MyAdapter(this, -1);listview.setAdapter(mAdapter);swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);// 设置刷新动画的颜色,可以设置1或者更多.// 我们暂时使用三个Android系统自带的颜色。swipeRefresh.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_orange_light,android.R.color.holo_green_light);swipeRefresh.setOnRefreshListener(new OnRefreshListener() {// SwipeRefreshLayout接管其包裹的ListView下拉事件。// 每一次对ListView的下拉动作,将触发SwipeRefreshLayout的onRefresh()。@Overridepublic void onRefresh() {new MyAsyncTask().execute();}});}// 每一次下拉刷新将触发更新操作动作。// 这里将是比较耗时的操作:如网络请求的数据,加载一个大图片。// 简单期间,我们假设就是简单的将count数据加1,然后更新显示。//// 备注:swipeRefreshLayout.setRefreshing(true) 到// swipeRefreshLayout.setRefreshing(false)之间的这段代码 ,// 在实际的应用开发中一般就是线程化的、耗时的或者后台的操作代码。private class MyAsyncTask extends AsyncTask {@Overrideprotected void onPreExecute() {super.onPreExecute();// true,刷新开始,所以启动刷新的UI样式.swipeRefresh.setRefreshing(true);// 开始启动刷新...}@Overrideprotected Object doInBackground(Object... params) {// 在这儿放耗时操作的 AsyncTask线程、后台Service等代码。try {Thread.sleep(5000);int result = count++;return result;} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(Object result) {super.onPostExecute(result);// add(0,xxx)每次将更新的数据xxx添加到头部。list.add(0, "" + result);mAdapter.notifyDataSetChanged();swipeRefresh.setRefreshing(false); // 刷新完毕. // false,刷新完成,因此停止UI的刷新表现样式。 }}private class MyAdapter extends ArrayAdapter {private Context mContext;private LayoutInflater inflater;public MyAdapter(Context context, int textViewResourceId) {super(context, textViewResourceId);this.mContext = context;inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return list.size();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null)convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);TextView text = (TextView) convertView.findViewById(android.R.id.text1);text.setTextSize(25);text.setText(list.get(position));return convertView;}}}
用Handler实现:
package com.example.swiperefreshlayout;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private List<String> list;private ListView listview;private SwipeRefreshLayout swipeRefresh;private ArrayAdapter mAdapter;private int count = 0;Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);int a = (Integer) msg.obj;swipeRefresh.setRefreshing(true);list.add(0, "" + a);mAdapter.notifyDataSetChanged();swipeRefresh.setRefreshing(false);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list = new ArrayList<String>();String[] names = { "张三", "李四", "王二", "赵六", "陈五" };for (int i = 0; i < names.length; i++) {list.add(names[i]);}listview = (ListView) findViewById(R.id.list_view);mAdapter = new MyAdapter(this, -1);listview.setAdapter(mAdapter);swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);swipeRefresh.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_orange_light,android.R.color.holo_green_light);swipeRefresh.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh() {longTimeOperation();}});}private class MyAdapter extends ArrayAdapter {private Context mContext;private LayoutInflater inflater;public MyAdapter(Context context, int textViewResourceId) {super(context, textViewResourceId);this.mContext = context;inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return list.size();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null)convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);TextView text = (TextView) convertView.findViewById(android.R.id.text1);text.setTextSize(25);text.setText(list.get(position));return convertView;}}private void longTimeOperation() {new Thread(new Runnable() {@Overridepublic void run() {// swipeRefresh.setRefreshing(true);//// list.add(0, "" + count++);//// mAdapter.notifyDataSetChanged();//// swipeRefresh.setRefreshing(false);try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}Message msg = handler.obtainMessage();msg.obj = count++;handler.sendMessage(msg);}}).start();}}
0 0
- Android SwipeRefreshLayout下拉刷新(两种不阻碍线程的方式)
- SwipeRefreshLayout(Android的下拉刷新组件)
- Android下拉刷新-SwipeRefreshLayout
- Android下拉刷新SwipeRefreshLayout
- Android 下拉刷新SwipeRefreshLayout
- Android SwipeRefreshLayout下拉刷新
- Android SwipeRefreshLayout GMail的下拉刷新效果
- Android 下拉刷新控件SwipeRefreshLayout 的使用
- Android SwipeRefreshLayout 下拉刷新组件的使用
- Android官方的下拉刷新SwipeRefreshLayout
- android google官方的下拉刷新SwipeRefreshLayout
- Android控件(一)下拉刷新:SwipeRefreshLayout
- Android 下拉刷新控件 SwipeRefreshLayout
- Android 下拉刷新控件SwipeRefreshLayout
- android SwipeRefreshLayout下拉刷新实现
- android下拉刷新之SwipeRefreshLayout
- Android--SwipeRefreshLayout下拉刷新Demo
- Android 6.0下拉刷新,SwipeRefreshLayout
- 举例说明数据库一、二、三及BCNF范式
- GIF loading
- testng 使用ant+ttestng-xslt进行报告优化
- CSS(3)
- arcgis api for JavaScript相关
- Android SwipeRefreshLayout下拉刷新(两种不阻碍线程的方式)
- Android apk动态加载机制的研究
- C++primer plus第六版课后编程练习答案2.5
- oracle表分区详解
- Lua - 语言学习笔记
- iOS 数据缓存
- 学安卓之路
- Android UI性能优化
- 安卓手机连接蓝牙打印机实现打印功能