利用第三方开源框架 PullToRefreshListView 实现下拉刷新(从网上下载图片)
2015-11-25 13:42
756 查看
基本原理同 :利用第三方开源框架 PullToRefreshListView 实现下拉刷新; 效果如下:
layout_main.xml :
为 adapter 提供的列表项:
item.xml :
layout_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android.flush.MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
为 adapter 提供的列表项:
item.xml :
<?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="horizontal" > <ImageView android:id="@+id/imageView" android:scaleType="fitCenter" android:layout_width="60dp" android:layout_height="60dp"/> <TextView android:id="@+id/textView" android:gravity="center" android:layout_width="match_parent" android:layout_height="60dp"/> </LinearLayout>
package com.android.flush; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private final String KEY_IMAGE = "image", KEY_TEXT = "text"; private int count = 0; private PullToRefreshListView listView; private ArrayAdapter<String> adapter; private ArrayList<Map<String, Object>> mDatas; private Map<String, Object> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (PullToRefreshListView)findViewById(R.id.listView); mDatas = new ArrayList<Map<String, Object>>(); adapter = new MyAdapter(this, -1); // 设置刷新模式 listView.setMode(Mode.PULL_FROM_START); listView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { new MyTask().execute(); } }); listView.setAdapter(adapter); // 设置提示信息 TextView textView = new TextView(this); textView.setText("下拉刷新"); listView.setEmptyView(textView); } private class MyAdapter extends ArrayAdapter{ private LayoutInflater inflater; public MyAdapter(Context context, int resource) { super(context, resource); inflater = getLayoutInflater(); } @Override public int getCount() { return mDatas.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) convertView = inflater.inflate(R.layout.item, null); HashMap<String, Object> map = (HashMap<String, Object>) (mDatas.get(position)); ImageView image = (ImageView) convertView .findViewById(R.id.imageView); image.setImageBitmap((Bitmap) map.get(KEY_IMAGE)); TextView text = (TextView) convertView.findViewById(R.id.textView); text.setText(map.get(KEY_TEXT) + ""); return convertView; } } private class MyTask extends AsyncTask{ @Override protected void onPreExecute() { listView.setRefreshing(); } @Override protected void onPostExecute(Object result) { mDatas.add(0, (Map<String, Object>) result); listView.setLastUpdatedLabel("更新时间:" + System.currentTimeMillis()); // 通知 adapter 更新,不能少 adapter.notifyDataSetChanged(); listView.onRefreshComplete(); } @Override protected Object doInBackground(Object... params) { try { Thread.sleep(2000); byte[] buffer = loadRawDataFromURL("http://pics.sc.chinaz.com/Files/pic/icons128/5982/s1.png"); Bitmap bm =BitmapFactory.decodeByteArray(buffer, 0, buffer.length); map = new HashMap<String, Object>(); map.put(KEY_IMAGE, bm); map.put(KEY_TEXT, count++ +""); return map; } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } } public static byte[] loadRawDataFromURL(String u) throws Exception { URL url = new URL(u); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream(); final int BUFFER_SIZE = 2048; final int EOF = -1; int c; byte[] buf = new byte[BUFFER_SIZE]; while (true) { c = bis.read(buf); if (c == EOF) break; baos.write(buf, 0, c); } conn.disconnect(); is.close(); byte[] data = baos.toByteArray(); baos.flush(); return data; } }
相关文章推荐
- java acm hdu 2083
- 服务降级
- jquery单选框radio值改变change事件
- JSP中多种include标签的区别?
- DFS&BFS
- 黑马程序员——Collection
- String - 大小比较,范围限制
- JVM垃圾收集算法
- Operating System: Three Easy Pieces --- Evaluating Spin Locks (Note)
- Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)
- 代码调用cmd中的命令
- poj-2749 Building roads
- yii2-用户登录验证
- C语言中函数调用中的传值与传址
- JVM中对象的引用类型
- vs2015管理github代码
- Ionic学习笔记五 跨域处理及HTTPS问题处理
- 震荡上行格局未改
- 虚拟机网络配置,桥接模式和NAT模式的总结,实验
- 下拉的tableview的头部拉伸效果