Use custom items, adapters, AsyncTask, and third-party open-source framework PullToRefresh to implement a custom drop-down list (load images from the network and display the ImageView in the item ),

Source: Internet
Author: User

Use custom items, adapters, AsyncTask, and third-party open-source framework PullToRefresh to implement a custom drop-down list (load images from the network and display the ImageView in the item ),

AsyncTask Usage Details: http://www.cnblogs.com/zzw1994/p/4959949.html

Drop-down open-source framework PullToRefresh usage and download details: http://www.cnblogs.com/zzw1994/p/4992194.html

The specific implementation code is as follows:

Item. xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5> 6 7 <ImageView 8 android: id = "@ + id/imageView1" 9 android: layout_width = "50dp" 10 android: layout_height = "50dp" 11 android: layout_alignParentLeft = "true" 12 android: src = "@ drawable/ic_launcher"/> 13 14 <TextView15 android: id = "@ + id/textView" 16 android: layout_width = "wrap_content" 17 android: layout_height = "wrap_content" 18 android: layout_alignBottom = "@ + id/imageView1" 19 android: Photo = "true" 20 android: layout_toLeftOf = "@ + id/imageView2" 21 android: layout_toRightOf = "@ + id/imageView1" 22 android: gravity = "center" 23 android: text = "TextView" 24 android: textColor = "@ android: color/holo_red_light "25 android: textSize =" 30sp "/> 26 27 <ImageView28 android: id =" @ + id/imageView2 "29 android: layout_width =" 50dp "30 android: layout_height = "50dp" 31 android: layout_alignParentRight = "true" 32 android: layout_alignParentTop = "true" 33 android: src = "@ drawable/ic_launcher"/> 34 35 </RelativeLayout>Item. xml

Activity_main.xml:

1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 tools: context = "com. zzw. testpulltorefresh. mainActivity "> 6 7 <com. handmark. pulltorefresh. library. pullToRefreshListView 8 android: id = "@ + id/listView" 9 android: layout_width = "match_parent" 10 android: layout_height = "match_parent"/> 11 12 </RelativeLayout>Activity_main.xml

MainActivity. java:

1 package com. zzw. testpulltorefresh; 2 3 import java. io. bufferedInputStream; 4 import java. io. byteArrayOutputStream; 5 import java. io. inputStream; 6 import java.net. httpURLConnection; 7 import java.net. URL; 8 import java. util. arrayList; 9 import java. util. date; 10 import java. util. hashMap; 11 12 import com. handmark. pulltorefresh. library. pullToRefreshBase; 13 import com. handmark. pulltorefresh. library. pullToRefreshBase. mode; 14 import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener; 15 import com. handmark. pulltorefresh. library. pullToRefreshListView; 16 17 import android. app. activity; 18 import android. content. context; 19 import android. graphics. bitmap; 20 import android. graphics. bitmapFactory; 21 import android. graphics. color; 22 import android. OS. asyncTask; 23 import android. OS. bundle; 24 import android. view. layoutInflater; 25 import android. view. view; 26 import android. view. viewGroup; 27 import android. widget. arrayAdapter; 28 import android. widget. imageView; 29 import android. widget. listView; 30 import android. widget. textView; 31 import android. widget. toast; 32 33 public class MainActivity extends Activity {34 private int COUNT = 0; 35 36 private final String IMAGE_KEY1 = "IMAGE_KEY1"; 37 private final String IMAGE_KEY2 = "IMAGE_KEY2 "; 38 private final String TEXT_KEY = "TEXT_KEY"; 39 40 private PullToRefreshListView; 41 private ArrayList <HashMap <String, Object> data; 42 private ArrayAdapter adapter; 43 private String [] urls = {"http://p1.so.qhimg.com/bdr/_240_/t01829c584b50b68311.jpg", 44 "http://p2.so.qhimg.com/bdr/_240_/t011c2cd4fe8723bcb2.jpg", 45 "http://p4.so.qhimg.com/bdr/_240_/t01b11db3c0961b24a9.jpg", 46 "http://p4.so.qhimg.com/bdr/_240_/t019786092c7b1688b9.jpg", 47 "http://p4.so.qhimg.com/bdr/_240_/t015b226d64a10097ce.jpg", 48 "http://p1.so.qhimg.com/bdr/_240_/t01f6c4382c907133ab.jpg "}; 49 50 @ Override 51 protected void onCreate (Bundle savedInstanceState) {52 super. onCreate (savedInstanceState); 53 setContentView (R. layout. activity_main); 54 55 data = new ArrayList <HashMap <String, Object> (); 56 57 listView = (PullToRefreshListView) findViewById (R. id. listView); 58 59 // set to refresh 60 listView when sliding down. setMode (Mode. PULL_FROM_START); 61 // supports drop-down and top-up listView. setMode (Mode. BOTH); 62 63 // set listening 64 listView. setOnRefreshListener (new OnRefreshListener <ListView> () {65 66 @ Override 67 public void onRefresh (PullToRefreshBase <ListView> refreshView) {68 // complete the business logic 69 new myasynctask(cmd.exe cute (); 70} 71}); 72 73 adapter = new MyAdapter (this, R. layout. item); 74 listView. setAdapter (adapter); 75 76 // you can specify the 77 TextView textView = new TextView (this); 78. setText ("Please pull down and refresh"); 79 listView. setEmptyView (textView); 80} 81 82 // custom Item display parameter settings in the Adapter operation 83 private class MyAdapter extends ArrayAdapter {84 85 private LayoutInflater inflater; 86 private int item; 87 88 public MyAdapter (Context context, int resource) {89 super (context, resource); 90 inflater = LayoutInflater. from (context); 91 this. item = resource; 92} 93 94 @ Override 95 public int getCount () {96 return data. size (); 97} 98 99 @ Override100 public HashMap <String, Object> getItem (int position) {101 102 return data. get (position); 103} 104 105 @ Override106 public View getView (int position, View convertView, ViewGroup parent) {107 HashMap <String, Object> map = getItem (position ); 108 109 if (convertView = null) {110 convertView = inflater. inflate (item, null); 111} 112 113 ImageView image1 = (ImageView) convertView. findViewById (R. id. imageView1); 114 ImageView image2 = (ImageView) convertView. findViewById (R. id. imageView2); 115 TextView textView = (TextView) convertView. findViewById (R. id. textView); 116 117 image1.setImageBitmap (Bitmap) map. get (IMAGE_KEY1); 118 image2.setImageBitmap (Bitmap) map. get (IMAGE_KEY2); 119 textView. setText (map. get (TEXT_KEY) + ""); 120 121 if (position % 2 = 1) {122 textView. setTextColor (Color. BLUE); 123} 124 125 return convertView; 126} 127 128} 129 130 131 // a series of delayed operations such as networking operate 132 private class MyAsyncTask extends AsyncTask {133 134 @ Override135 protected void onPreExecute () in AsyncTask () {136 // start to refresh the 137 listView. setRefreshing (); 138} 139 140 @ Override141 protected Object doInBackground (Object... params) {142 HashMap <String, Object> map = new HashMap <String, Object> (); 143 try {144 byte [] buf = loadRawDataFromURL (urls [COUNT]); 145 BitmapFactory factory = new BitmapFactory (); 146 Bitmap bitmap = factory. decodeByteArray (buf, 0, buf. length); 147 map. put (TEXT_KEY, "data->" + COUNT); 148 map. put (IMAGE_KEY1, bitmap); 149 map. put (IMAGE_KEY2, bitmap); 150} catch (Exception e) {151 e. printStackTrace (); 152} 153 return map; 154} 155 156 @ Override157 protected void onPostExecute (Object result) {158 159 data. add (0, (HashMap <String, Object>) result); 160 adapter. notifyDataSetChanged (); 161 162 // set the tag 163 listView. setLastUpdatedLabel ("Last updated time:" + new Date (); 164 165 // refresh completed 166 listView. onRefreshComplete (); 167 COUNT ++; 168 if (COUNT> = urls. length) {169 COUNT = 0; 170} 171 Toast. makeText (getApplicationContext (), "loaded successfully", 0 ). show (); 172} 173} 174 175 // read byte array 176 public static byte [] loadRawDataFromURL (String u) through URL) throws Exception {177 URL url = new URL (u); 178 HttpURLConnection conn = (HttpURLConnection) url. openConnection (); 179 180 InputStream is = conn. getInputStream (); 181 BufferedInputStream bis = new BufferedInputStream (is); 182 183 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 184 // cache 2KB185 final int BUFFER_SIZE = 2*1024; 186 final int EOF =-1; 187 188 int c; 189 byte [] buf = new byte [BUFFER_SIZE]; 190 191 192 while (true) {c = bis. read (buf); 193 if (c = EOF) 194 break; 195 196 baos. write (buf, 0, c); 197} 198 199 conn. disconnect (); 200 is. close (); 201 202 byte [] B = baos. toByteArray (); 203 baos. flush (); 204 205 return B; 206} 207 208}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.