TOP

自己定制ListView,上拉刷新和下拉刷新,加载网络图片,并且添加缓存机制。(一)
2017-10-11 17:09:32 】 浏览:10111
Tags:自己 定制 ListView 新和 下拉 刷新 加载 网络 图片 并且 添加 机制

  1 package com.lixu.listviewrefresh;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Random;
  6 
  7 import com.lixu.listviewrefresh.Loadnetimage.OnLoadnetimageListener;
  8 import com.lixu.listviewrefresh.MyRefreshListview.OnRefreshListener;
  9 import android.annotation.SuppressLint;
 10 import android.app.Activity;
 11 import android.app.ProgressDialog;
 12 import android.content.Context;
 13 import android.graphics.Bitmap;
 14 import android.graphics.BitmapFactory;
 15 import android.os.AsyncTask;
 16 import android.os.Bundle;
 17 import android.util.Log;
 18 import android.view.LayoutInflater;
 19 import android.view.View;
 20 import android.view.ViewGroup;
 21 import android.widget.ArrayAdapter;
 22 import android.widget.ImageView;
 23 import android.widget.ListView;
 24 import android.widget.TextView;
 25 
 26 public class MainActivity extends Activity {
 27     private static final String KEY_IMAGE = "image_key";
 28     private static final String KEY_TEXT = "text_key";
 29 
 30     private static final String CACHE_KEY_IMAGE = "cache_image_key";
 31     private static final String CACHE_KEY_TEXT = "cache_text_key";
 32     // 原始数据
 33     private ArrayList<HashMap<String, Object>> data;
 34     // 缓存队列
 35     private ArrayList<HashMap<String, Object>> cache;
 36 
 37     private ArrayAdapter<String> mMyAdapter;
 38 
 39     private MyRefreshListview lv;
 40 
 41     private Activity activity;
 42     private Random random = new Random();
 43     // 图片网络地址
 44     private String[] image_urls = { "https://www.cppentry.com/upload_files/article/98/1_e6qcm__.jpg",
 45             "https://www.cppentry.com/upload_files/article/98/1_hlpir__.jpg",
 46             "https://www.cppentry.com/upload_files/article/98/1_gxt8k__.jpg",
 47             "https://www.cppentry.com/upload_files/article/98/1_kh6la__.jpg",
 48             "https://www.cppentry.com/upload_files/article/98/1_tb5kk__.jpg",
 49             "https://www.cppentry.com/upload_files/article/98/1_4rwi8__.jpg",
 50             "https://www.cppentry.com/upload_files/article/98/1_kh0ue__.jpg",
 51             "https://www.cppentry.com/upload_files/article/98/1_pqpdk__.jpg" };
 52 
 53     @Override
 54     protected void onCreate(Bundle savedInstanceState) {
 55         super.onCreate(savedInstanceState);
 56         setContentView(R.layout.activity_main);
 57 
 58         activity = this;
 59 
 60         data = new ArrayList<HashMap<String, Object>>();
 61         cache = new ArrayList<HashMap<String, Object>>();
 62         // 添加初始数据
 63         for (int i = 0; i < 30; i++) {
 64             HashMap<String, Object> map = new HashMap<String, Object>();
 65             map.put(KEY_IMAGE, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
 66             map.put(KEY_TEXT, i);
 67             data.add(map);
 68         }
 69 
 70         lv = (MyRefreshListview) findViewById(R.id.listview);
 71 
 72         mMyAdapter = new MyAdapter(this, -1);
 73 
 74         lv.setAdapter(mMyAdapter);
 75 
 76         lv.setOnRefreshListener(new OnRefreshListener() {
 77 
 78             @Override
 79             public void ontop() {
 80                 // 随机加入一张图片
 81                 adddata(image_urls[random.nextInt(8)]);
 82 
 83             }
 84 
 85             @Override
 86             public void onbuttom() {
 87                 // 没有设置缓存机制
 88                 new MyAsyncTaskBottom(image_urls[random.nextIn  
		
自己定制ListView,上拉刷新和下拉刷新,加载网络图片,并且添加缓存机制。(一) https://www.cppentry.com/bencandy.php?fid=98&id=141506

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇自己定制ListView,上拉刷新和下.. 下一篇Material Design:CollapsingTool..