关闭

android之GridView异步加载图片时显示加载进度ProgressBar

标签: androidlayoutencodingnull
2422人阅读 评论(5) 收藏 举报
分类:

GridView异步加载图片时,每个view中先显示加载进度ProgressBar。。。Gallery中使用该方法还可以避免图片没加载到时无法滑动的问题。


GridView布局文件:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:orientation="vertical">  
    
      <GridView    
	    android:id="@+id/main_album_gridview"  
	    android:layout_width="fill_parent"   
	    android:layout_height="fill_parent"  
	    android:numColumns="4"  
	    android:scrollbars="none"
	    android:smoothScrollbar="true"
	    android:fadingEdge="none"
	    android:verticalSpacing="0dip"  
	    android:horizontalSpacing="0dip"  
	    android:columnWidth="90dp"  
	    android:stretchMode="columnWidth"  
	    android:gravity="center"  
	  />
</LinearLayout>


        Item的布局文件gridview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/relativeLayout1">
    <ImageView 
    android:id="@+id/grid_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <ProgressBar 
     android:id="@+id/grid_progressBar"    
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"  
     android:visibility="gone" 
     android:layout_gravity="center_vertical"
    
    />
    <!--  style="@android:style/Widget.ProgressBar.Small" -->
</RelativeLayout>

加载图片时先显示进度:

	
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			LayoutInflater layoutInflater = LayoutInflater.from(mMainActivity);
			convertView = layoutInflater.inflate(R.layout.gridview_item, null);
			GridView.LayoutParams params = new GridView.LayoutParams(mMainActivity.albumWidth,mMainActivity.albumHeight);
			convertView.setLayoutParams(params);
		}
		convertView.setTag(position);
				
		//先显示加载进度条
		ProgressBar pb = (ProgressBar)convertView.findViewById(R.id.grid_progressBar);
		pb.setVisibility(View.VISIBLE);
		
		ImageView imageView = (ImageView)convertView.findViewById(R.id.grid_imageview);
		imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		imageView.setAdjustViewBounds(true);
		syncTask.loadImage(position, imageUrls.get(position), imageLoadListener);//异步加载图片
		
		return convertView;
	}


图片加载完后要隐藏进度条:
View view = (View)mGridView.findViewWithTag(t);  
              
    if(view != null){  
        //隐藏进度条  
        ProgressBar pb = (ProgressBar)view.findViewById(R.id.grid_progressBar);  
        pb.setVisibility(View.INVISIBLE);  
        //显示加载到的图片  
        ImageView imageView = (ImageView)view.findViewById(R.id.grid_imageview);  
        imageView.setBackgroundDrawable(drawable);  
    }  







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:169929次
    • 积分:2266
    • 等级:
    • 排名:第16598名
    • 原创:37篇
    • 转载:133篇
    • 译文:1篇
    • 评论:11条
    最新评论