Android列表控件选项中添加进度框ProgressBar实现

  今天有时间就学习了下在ListView、GridView列表项中清加ProgressBar,小马用最简单的代码实现可以通用的功能,人人都能看懂,哈哈,直接说下,如果你的适配器getView方法返回的View是一个自定义控件的话,有点不好实现哦,不过可以用两个控件之间Visible 、Invisible来切换实现今天的效果,下面的例子是主类、适配器、异步下载类等的都写在一个类里面了,为的是发的博文里一次性看的话方便,如果大家觉得结构不清楚的话根据自身需求自行抽取下,今天的DEMO只是个简单的思路,其它控件列表项中添加进度框大家也可以用如下的方法实现,老规矩,先看效果,再看代码:

(截图有点失真,但无防)

一:效果图如下

Android列表控件选项中添加进度框ProgressBar实现

二:效果看完了,看下实现的路是怎样的,吼吼:核心代码如下:

 
 
  
  
  
  
  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

你可能感兴趣的:(Android列表控件选项中添加进度框ProgressBar实现)