Progressbar未在GridView中显示自定义视图

时间:2015-02-25 19:58:21

标签: android gridview android-progressbar

我有一个应用程序,可以在gridview中加载一些图像。我有一个像这样的自定义视图:

grid_item_view.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<com.entu.artapp.utils.SquareImageView
    android:id="@+id/gridItem_squareImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:visibility="invisible"/>

<ProgressBar
    android:id="@+id/gridItem_progressBar"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_centerInParent="true"
    android:visibility="visible"/>

</RelativeLayout>

我在我的GridView适配器中使用它,如下所示:

ImageAdapter:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    //ArrayList<Bitmap> bitmapList;
    List<ParseObject> urlList;
    private LayoutInflater inflater;

public ImageAdapter(Context c, List<ParseObject> urlList) {
    mContext = c;
    this.urlList = urlList;
    inflater = LayoutInflater.from(c);
    //this.bitmapList = bitmapList;
}

public int getCount() {
    return urlList.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View view, ViewGroup parent) {

    View gridItem = view;
    ViewHolder viewHolder;

    if (gridItem == null) {
        viewHolder = new ViewHolder();
        gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
        viewHolder.squareImageView = (SquareImageView) gridItem.findViewById(R.id.gridItem_squareImageView);
        viewHolder.progressBar = (ProgressBar) gridItem.findViewById(R.id.gridItem_progressBar);
        gridItem.setTag(viewHolder);
    } else {
        viewHolder= (ViewHolder) view.getTag();
    }

    //imageView.setImageBitmap(bitmapList.get(position));
    Picasso.with(mContext).load(urlList.get(position).get("thumbnailURL").toString()).resize(mContext.getResources().getDisplayMetrics().widthPixels/2, mContext.getResources().getDisplayMetrics().widthPixels/2).into(viewHolder.squareImageView);
    viewHolder.progressBar.setVisibility(View.INVISIBLE);
    viewHolder.squareImageView.setVisibility(View.VISIBLE);
    Log.e("gridviewitem: ", "" + position);
    return gridItem;
}

class ViewHolder {
    SquareImageView squareImageView;
    ProgressBar progressBar;
     }
}

出于某种原因,ProgressBar根本不显示,只显示空白屏幕,然后图像完成加载并显示图像。

感谢任何帮助!

干杯!

1 个答案:

答案 0 :(得分:1)

Picasso异步加载图片并在您的代码行中

viewHolder.progressBar.setVisibility(View.INVISIBLE);

在加载图像之前设置进度条的可见性不可见。仅在图像设置为图像视图时隐藏进度条。我已经更新了getView方法。希望它能帮到你......

public View getView(int position, View view, ViewGroup parent) {

View gridItem = view;
ViewHolder viewHolder;

if (gridItem == null) {
    viewHolder = new ViewHolder();
    gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
    viewHolder.squareImageView = (SquareImageView) gridItem.findViewById(R.id.gridItem_squareImageView);
    viewHolder.progressBar = (ProgressBar) gridItem.findViewById(R.id.gridItem_progressBar);
    gridItem.setTag(viewHolder);
} else {
    viewHolder= (ViewHolder) view.getTag();
}

//imageView.setImageBitmap(bitmapList.get(position));

Picasso.with(context)
        .load(imageUrl)
        .into(myImage,  new Callback() {

            @Override
            public void onSuccess() {
               viewHolder.squareImageView.setVisibility(View.VISIBLE);
               viewHolder.progressBar.setVisibility(View.INVISIBLE);        
            }

            @Override
            public void onError() {
               viewHolder.progressBar.setVisibility(View.VISIBLE);
               viewHolder.squareImageView.setVisibility(View.INVISIBLE);
            }
}).resize(mContext.getResources().getDisplayMetrics().widthPixels/2, mContext.getResources().getDisplayMetrics().widthPixels/2).into(viewHolder.squareImageView);

Log.e("gridviewitem: ", "" + position);
return gridItem;
}