Android列表控件選項中添加進度框ProgressBar實現

  今天有時間就學習了下在ListView、GridView列表項中清加ProgressBar,小馬用最簡單的代碼實現能夠通用的功能,人人都能看懂,哈哈,直接說下,若是你的適配器getView方法返回的View是一個自定義控件的話,有點很差實現哦,不過能夠用兩個控件之間Visible 、Invisible來切換實現今天的效果,下面的例子是主類、適配器、異步下載類等的都寫在一個類裏面了,爲的是發的博文裏一次性看的話方便,若是你們以爲結構不清楚的話根據自身需求自行抽取下,今天的DEMO只是個簡單的思路,其它控件列表項中添加進度框你們也能夠用以下的方法實現,老規矩,先看效果,再看代碼:

(截圖有點失真,但無防)

一:效果圖以下

二:效果看完了,看下實現的路是怎樣的,吼吼:核心代碼以下:

 
  1. package com.example.listviewitemprogress;
  2. import java.io.InputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.BaseAdapter;
  15. import android.widget.Button;
  16. import android.widget.ImageView;
  17. import android.widget.ListView;
  18. import android.widget.ProgressBar;
  19. import android.widget.TextView;
  20. /**
  21. * @author 小馬果
  22. * ListView列表項中添加ProgressBar
  23. */
  24. public class MainActivity extends Activity {
  25. private ListView mListView = null ;
  26. private static final String imgUrl = "http://www.qq510.net/uploads/allimg/100613/2_100613171230_5.jpg";
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. init();
  32. }
  33. private void init(){
  34. mListView = (ListView)findViewById(R.id.listview);
  35. mListView.setAdapter(new MyListViewAdapter(this));
  36. }
  37. private class MyListViewAdapter extends BaseAdapter{
  38. private LayoutInflater inflater = null;
  39. public MyListViewAdapter(Context con){
  40. inflater = (LayoutInflater)con.getSystemService(LAYOUT_INFLATER_SERVICE);
  41. }
  42. @Override
  43. public int getCount() {
  44. // TODO Auto-generated method stub
  45. return Integer.MAX_VALUE;
  46. }
  47. @Override
  48. public Object getItem(int arg0) {
  49. // TODO Auto-generated method stub
  50. return arg0;
  51. }
  52. @Override
  53. public long getItemId(int position) {
  54. // TODO Auto-generated method stub
  55. return position;
  56. }
  57. @Override
  58. public View getView(int position, View convertView, ViewGroup parent) {
  59. // TODO Auto-generated method stub
  60. ViewHolder mHolder = new ViewHolder() ;
  61. if(convertView == null){
  62. convertView = inflater.inflate(R.layout.list_item, null);
  63. mHolder.iv = (ImageView)(convertView.findViewById(R.id.iv));
  64. mHolder.mBtn = (Button)(convertView.findViewById(R.id.button));
  65. mHolder.pb = (ProgressBar)(convertView.findViewById(R.id.progressBar1));
  66. mHolder.tv = (TextView)(convertView.findViewById(R.id.textView1));
  67. convertView.setTag(mHolder);
  68. }else{
  69. mHolder = (ViewHolder)convertView.getTag();
  70. }
  71. new AsyncTask<ViewHolder, Void, Bitmap>() {
  72. private ViewHolder v;
  73. @Override
  74. protected Bitmap doInBackground(ViewHolder... params) {
  75. v = params[0];
  76. return BitmapFactory.decodeStream(getStreamFromURL(imgUrl));
  77. }
  78. @Override
  79. protected void onPostExecute(Bitmap result) {
  80. super.onPostExecute(result);
  81. v.pb.setVisibility(View.GONE);
  82. v.mBtn.setVisibility(View.VISIBLE);
  83. v.iv.setImageBitmap(result);
  84. }
  85. }.execute(mHolder);
  86. return convertView;
  87. }
  88. }
  89. static class ViewHolder{
  90. ImageView iv = null ;
  91. Button mBtn = null ;
  92. ProgressBar pb = null ;
  93. TextView tv = null ;
  94. }
  95. static String length;
  96. public static InputStream getStreamFromURL(String imageURL) {
  97. InputStream in=null;
  98. try {
  99. URL url=new URL(imageURL);
  100. HttpURLConnection connection=(HttpURLConnection) url.openConnection();
  101. // connection.setRequestProperty("Connection", "Keep-Alive");//維持長鏈接
  102. connection.setConnectTimeout(6* 1000);//設置鏈接超時
  103. if (connection.getResponseCode() != 200) throw new RuntimeException("請求失敗");
  104. length = connection.getHeaderField("Content-Length");
  105. in=connection.getInputStream();
  106. } catch (Exception e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. return in;
  111. }
  112. }

三:再來看下兩個佈局吧(爲方便查,小馬就放一塊兒了:)

 
  1. 主佈局文件:
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <ListView
  7. android:id="@+id/listview"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:drawSelectorOnTop="false"
  11. android:choiceMode="none"
  12. android:focusable="false"
  13. android:scrollingCache="false"
  14. android:fadingEdge="none"
  15. android:focusableInTouchMode="false"
  16. android:clickable="false"
  17. android:dividerHeight="0.5dip" />
  18. </RelativeLayout>
  19. ListView列表控件Item佈局:
  20. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  21. xmlns:tools="http://schemas.android.com/tools"
  22. android:layout_width="match_parent"
  23. android:layout_height="110dp"
  24. android:background="#00ffeeaa"
  25. >
  26. <ImageView
  27. android:id="@+id/iv"
  28. android:layout_width="110dp"
  29. android:background="@drawable/ic_action_search"
  30. android:layout_height="110dp"
  31. android:contentDescription="@string/hello_world"/>
  32. <Button
  33. android:id="@+id/button"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_alignParentRight="true"
  37. android:layout_centerVertical="true"
  38. android:text="@string/xiaomaguo"/>
  39. <ProgressBar
  40. android:id="@+id/progressBar1"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_alignParentLeft="true"
  44. android:layout_centerVertical="true"
  45. android:layout_alignTop="@+id/button"
  46. android:layout_marginLeft="29dp" />
  47. <TextView
  48. android:id="@+id/textView1"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_alignBaseline="@+id/button"
  52. android:layout_alignBottom="@+id/button"
  53. android:layout_centerVertical="true"
  54. android:layout_toLeftOf="@+id/button"
  55. android:text="@string/cool" />
  56. </RelativeLayout>

最後,因爲選項中使用的圖片是小馬從網上異步下載加載的,因此得開下聯網權限,你們別忘了加就好了,以下:

<uses-permission android:name="android.permission.INTERNET"/>

怎麼樣?簡單吧,剛開始的時間沒思路,後來玩花辨的時候想到了,美麗說瀑布流實現的原理跟這個差很少,上面這個只是個小的DEMO,給你們提供個思路,GridView等其它控件,你們能夠直接參照上面這個代碼來寫,吼吼,小DEMO源碼已經加在附件中,以爲有用就下下來瞅瞅吧,加油,天天進步一點點,祝 :Happy ! O_O

相關文章
相關標籤/搜索