利用第三方开源框架 PullToRefreshListView 实现下拉刷新(从网上下载图片)
来源:互联网 发布:java分销微商城源码 编辑:程序博客网 时间:2024/05/22 13:39
基本原理同 :利用第三方开源框架 PullToRefreshListView 实现下拉刷新; 效果如下:
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>
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;@Overrideprotected 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>() {@Overridepublic 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();}@Overridepublic int getCount() {return mDatas.size();}@Overridepublic 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{@Overrideprotected void onPreExecute() {listView.setRefreshing();}@Overrideprotected void onPostExecute(Object result) {mDatas.add(0, (Map<String, Object>) result);listView.setLastUpdatedLabel("更新时间:" + System.currentTimeMillis());// 通知 adapter 更新,不能少adapter.notifyDataSetChanged();listView.onRefreshComplete();}@Overrideprotected 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;}}
1 0
- 利用第三方开源框架 PullToRefreshListView 实现下拉刷新(从网上下载图片)
- 利用第三方开源框架 PullToRefreshListView 实现下拉刷新
- PullToRefreshListView实现下拉刷新
- PullToRefreshListView简单实现下拉刷新、下拉加载
- 从网上下载图片
- PullToRefreshListView下拉刷新
- Android中PullToRefreshListView的下拉刷新,下拉加载的实现
- 上拉刷新 下拉下载PullToRefreshListView的使用
- 使用AsyncTask 实现网络请求及PullToRefreshListView下拉刷新
- PullToRefreshListView实现下拉刷新和上拉加载更多
- 利用swiperefreshlayout和picasso实现图片的下拉刷新
- WebClient 从网上下载图片
- android从网上下载图片
- Android从网上下载图片
- HttpURLConnection从网上下载图片
- java代码实现从网上直接下载图片或者网页
- 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)
- 利用MUI实现下拉刷新
- linux查看 php环境安装路径
- App Store IDFA被拒检查办法
- git command
- std::initializer_list in C++0x
- android编译分析之8—envsetup.mk
- 利用第三方开源框架 PullToRefreshListView 实现下拉刷新(从网上下载图片)
- 二叉树学习之二叉查找树 http://blog.csdn.net/callinglove/article/details/40707449
- java acm hdu 2083
- 【Leetcode】之Divide Two Integers
- 2015河南科技学院高校csdn俱乐部--网线制作
- Jquery插件
- 自动摘要算法
- 关于mysql函数GROUP_CONCAT 需要注意的地方
- ios初体验,终端命令