activity_main布局

<?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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:text="TextView" /></LinearLayout>



item布局

<RelativeLayout 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"    tools:context="com.example.listviewrefresh.MainActivity" >    <com.example.listviewrefresh.MyListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></RelativeLayout>


package com.example.listviewrefresh;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.util.Log;public class Loader {private OnUpdateProgessListener mOnUpdateProgessListener = null;public void setOnUpdateProgessListener(OnUpdateProgessListener l) {this.mOnUpdateProgessListener = l;}public interface OnUpdateProgessListener {public void onUpdateProgress(int count, int total);}public byte[] loadRawDataFromURL(String u) throws Exception {Log.d("下載數據...", u);URL url = new URL(u);HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 总的长度int total = conn.getContentLength();InputStream is = conn.getInputStream();BufferedInputStream bis = new BufferedInputStream(is);ByteArrayOutputStream baos = new ByteArrayOutputStream();final int BUFFER_SIZE = 2048;final int EOF = -1;int c;// 下载的总数计数int count = 0;byte[] buf = new byte[BUFFER_SIZE];while (true) {c = bis.read(buf);if (c == EOF)break;// 每次累加到现在为止我们下载了多少数据,以便于后面计算已经下载的数据占了总数量的百分比count = count + c;// 发布最新的数据,更新随后的进度条显示进度使用if (mOnUpdateProgessListener != null)mOnUpdateProgessListener.onUpdateProgress(count, total);baos.write(buf, 0, c);}conn.disconnect();is.close();byte[] data = baos.toByteArray();baos.flush();Log.d("下載數據完畢", u);return data;}}



package com.example.listviewrefresh;import android.content.Context;import android.util.AttributeSet;import android.widget.AbsListView;import android.widget.ListView;public class MyListView extends ListView {private OnRefreshListener mOnRefreshListener;public interface OnRefreshListener {// 表示下拉见顶刷新回调的接口public void onTop();// 表示上拉见底刷新回调的接口public void onBottom();}public void setOnRefreshListener(OnRefreshListener l) {this.mOnRefreshListener = l;this.setOnScrollListener(new OnScrollListener() {private int firstVisibleItem, visibleItemCount, totalItemCount;@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {this.firstVisibleItem = firstVisibleItem;this.visibleItemCount = visibleItemCount;this.totalItemCount = totalItemCount;}@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {// 下拉见顶刷新事件,回调if (firstVisibleItem == 0)mOnRefreshListener.onTop();// 上拉见底部的刷新事件,回调boolean b = (firstVisibleItem + visibleItemCount) == totalItemCount;if (b) {mOnRefreshListener.onBottom();}}}});}public MyListView(Context context, AttributeSet attrs) {super(context, attrs);}}

package com.example.listviewrefresh;import java.util.ArrayList;import java.util.HashMap;import com.example.listviewrefresh.Loader.OnUpdateProgessListener;import com.example.listviewrefresh.MyListView.OnRefreshListener;import android.app.Activity;import android.app.ProgressDialog;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;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 ArrayList<HashMap<String, Object>> data;privateString CACHE_URL_KEY="cache_url_key",CACHE_BITMAP="cache_bitmap";private ArrayList<HashMap<String, Object>> cache;private String IMAGE_KEY = "image", TEXT_KEY = "text";private ArrayAdapter adapter;// 加载这个链接的图片资源private String IMAGE_URL = "http://avatar.csdn.net/9/7/A/1_zhangphil.jpg";private MyListView lv;private Activity activity;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);activity = this;Resources res = getResources();cache=new ArrayList<HashMap<String,Object>>();data = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < 20; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(IMAGE_KEY,BitmapFactory.decodeResource(res, R.drawable.ic_launcher));map.put(TEXT_KEY, i);data.add(map);}lv = (MyListView) findViewById(R.id.listView);adapter = new MyAdapter(this, -1);lv.setAdapter(adapter);lv.setOnRefreshListener(new OnRefreshListener(){@Overridepublic void onTop() {loadNewData(IMAGE_URL);}@Overridepublic void onBottom() {addBottom();}});}privatevoidloadNewData(String url){Log.d("检查缓存...",url);for(int i=0;i<cache.size();i++){HashMap<String,Object> map=cache.get(i);String u=map.get(CACHE_URL_KEY)+"";if(url.equals(u)){Log.d("发现缓存,直接取出来加载!",url);Bitmap bmp=(Bitmap) map.get(CACHE_BITMAP);addTop(bmp);return;}}Log.d("没有发现缓存,开启线程下载",url);new AddTopAsyncTask().execute(url);}private class MyAdapter extends ArrayAdapter {private LayoutInflater mLayoutInflater;public MyAdapter(Context context, int resource) {super(context, resource);mLayoutInflater = LayoutInflater.from(context);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null)convertView = mLayoutInflater.inflate(R.layout.item, null);ImageView image = (ImageView) convertView.findViewById(R.id.imageView);TextView text = (TextView) convertView.findViewById(R.id.textView);HashMap<String, Object> map = getItem(position);image.setImageBitmap((Bitmap) map.get(IMAGE_KEY));text.setText(map.get(TEXT_KEY) + "");return convertView;}@Overridepublic HashMap<String, Object> getItem(int position) {return data.get(position);}@Overridepublic int getCount() {return data.size();}}private void addTop(Bitmap obj) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(IMAGE_KEY, obj);map.put(TEXT_KEY, data.size());data.add(0, map);adapter.notifyDataSetChanged();}private class AddTopAsyncTask extends AsyncTask<String,Integer,Bitmap> {private ProgressDialog pd;@Overrideprotected void onPreExecute() {pd = new ProgressDialog(activity);pd.setMessage("请稍候,正在加载。。。");pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd.show();}@Overrideprotected Bitmap doInBackground(String... params) {String url=params[0];try {Loader loader=new Loader();loader.setOnUpdateProgessListener(new OnUpdateProgessListener(){@Overridepublic void onUpdateProgress(int count, int total) {publishProgress(count,total);}});byte[] bytes=loader.loadRawDataFromURL(url);Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0,bytes.length);//将加载的数据添加到缓存队列中HashMap<String,Object> map=new HashMap<String,Object>();map.put(CACHE_URL_KEY, url);map.put(CACHE_BITMAP, bmp);cache.add(map);return bmp;} catch (Exception e) {e.printStackTrace();}return null;} @Override protected void onProgressUpdate(Integer... values) { int count = (Integer) values[0]; int total = (Integer) values[1];  pd.setMax(total); pd.setProgress(count); }@Overrideprotected void onPostExecute(Bitmap result) {pd.dismiss();addTop(result);}}// 底部刷新增加数据private void addBottom() {HashMap<String, Object> map = new HashMap<String, Object>();map.put(IMAGE_KEY, BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));map.put(TEXT_KEY, data.size());data.add(map);adapter.notifyDataSetChanged();lv.setSelection(ListView.FOCUS_DOWN);}}





更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. Android(安卓)Http网络数据传输备忘
  4. android手机端保存xml数据
  5. 复习android SQLiteOpenHelper
  6. Android使用ccache减少编译时间
  7. Android的NDK开发(4)————JNI数据结构之JNINativeMethod
  8. android中sqlite的使用
  9. Android上实现zlib解压缩的方法 Inflater用法

随机推荐

  1. Android设备电量监控
  2. 一个大型新闻app的骨架(android)
  3. Android HMAC_SHA1 算法简单实现
  4. 建立Android 1.5 application 开发环境
  5. android 发送带附件的邮件
  6. cordova 插件 开发添加 android 权限
  7. android中AlertDialog包含EditText时弹出
  8. 安卓startActivity:彻底理解startActivity
  9. android重启代码
  10. Android 基础面试题