Android下载图像列表并在viewpager中显示图像并显示进度对话框

时间:2015-08-11 08:03:26

标签: android android-asynctask android-viewpager android-adapter android-progressbar

您好我想在viewPager中显示一些图像需要在显示之前下载,我还需要在下载时显示进度条。任何人都可以帮我解决这个问题。此外,我正在尝试所有viewpager适配器窗体列表视图适配器。这是一个很好的做法。以下是我的代码

这是我的列表适配器

public class adapter extends ArrayAdapter<FriendList> {

private Context context;
private List<FriendList> frndList;
ProgressDialog mProgressDialog;
LinearLayout inHorizontalScrollView;
View view;
public adapter(Context context, int resource, List<FriendList> objects) {
    super(context, resource, objects);
    this.context = context;
    this.frndList = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.item_file, parent, false);
    FriendList friensList = frndList.get(position);
    Log.v("=====================","======111111========"+friensList);
    Friend temp = friensList.getFriend();
    Log.v("=====================","======temp========"+temp);
    //inHorizontalScrollView = (LinearLayout)view.findViewById(R.id.inhorizontalscrollview);
    List<String> imageArra = temp.getImageData();
    Log.v("=====================","======imageArra========"+imageArra.size());

    ViewPager myPager = (ViewPager) view.findViewById(R.id.myfivepanelpager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(context);
    myPager.setAdapter(adapter);
    adapter.change(imageArra);

    return view;
}

}

这是我的viewpager适配器

 public class ViewPagerAdapter extends PagerAdapter {

/* Adapter activity;
 int imageArray[];
 private Context context;


 public ViewPagerAdapter( Context context, int[] imgArra) {
     imageArray = imgArra;
     //activity = act;
     context = context;
 }*/
ProgressDialog mProgressDialog;

private List<String> mPaths;

private Context mContext;

public ViewPagerAdapter(Context cx) {
    mContext = cx.getApplicationContext();
}

public void change(List<String> paths) {
    mPaths = paths;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mPaths.size();
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView iv = new ImageView(mContext);
    String path = mPaths.get(position);
    Log.v("=====================","======path====11===="+path);
    new calc_stanica().execute(path, container, iv);


    try {
        Bitmap bitmap = Glide.with(mContext).load(path).asBitmap().into(-1, -1).get();
        //Bitmap bm = BitmapFactory.decodeFile(mPaths.get(position));
        iv.setImageBitmap(bitmap);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ((ViewPager) container).addView(iv, 0);
    return iv;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object obj) {
    // TODO Auto-generated method stub
    return view == (View) obj;
}


private class calc_stanica extends AsyncTask<Object, Void, Bitmap> {
    ViewGroup container;
    ImageView iv;

    /*@Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(mContext);
        // Set progressdialog title
        mProgressDialog.setTitle("Download Image Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }*/

    @Override
    protected Bitmap doInBackground(Object... params) {

        //ArrayList<Bitmap> result = new ArrayList<Bitmap>();
        String pass = (String) params[0];
        container = (ViewGroup) params[1];
        iv = (ImageView) params[2];
        //View str = (View) params[1];
        //Context ctx = (Context) params[2];
        //Log.v("================", "===passed========" + passed);


        Bitmap bitmap = null;
        try {
            // Download Image from URL
            //InputStream input = new java.net.URL(imageURL).openStream();
            // Decode Bitmap
            bitmap = Glide.with(mContext).load(pass).asBitmap().into(-1, -1).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //int intArray[] = new int[bitmap.getWidth()*bitmap.getHeight()];
        //result.add(bitmap);


        //Log.v("================", "===result========" + result);
        //Some calculations...

        return bitmap; //return result
    }

    //*protected Bitmap doInBackground(String... URL) {

   /* String imageURL = URL[0];

    Bitmap bitmap = null;
    ArrayList<Bitmap> list;

    try {
        // Download Image from URL
        //InputStream input = new java.net.URL(imageURL).openStream();
        // Decode Bitmap
        bitmap =  Glide.with(getApplicationContext()).load(imageURL).asBitmap().into(-1, -1).get();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;*/

    @Override
    protected void onPostExecute(Bitmap result) {

        if(result  != null) {
            iv.setImageBitmap(result);
            ((ViewPager)container).addView(iv, 0);
        }

        //mProgressDialog.dismiss();
    }

}

}

它给了我计数错误,不允许在适配器中添加进度条。

1 个答案:

答案 0 :(得分:0)

更改您的代码顺序
myPager.setAdapter(adapter);
adapter.change(imageArra);

adapter.change(imageArra);
myPager.setAdapter(adapter);

因为当您设置适配器时,将在您之后仅分配列表时调用适配器getCount
更新
以下是我认为你应该检查的一些注意事项:

  1. 确保imageArra不为空且图片path正确无误。
  2. 您应为myfivepanelpager添加固定的高度。
  3. 不要将mContext存储在ViewPagerAdapter中,您不需要这样做。
  4. instantiateItemImageView iv = new ImageView(mContext);中不使用应用程序上下文,请使用container.getContext()
  5. 您不需要此new calc_stanica().execute(path, container, iv);,因为已使用Glide
  6. Bitmap bitmap = Glide.with(mContext).load(path).asBitmap().into(-1, -1).get(); //Bitmap bm = BitmapFactory.decodeFile(mPaths.get(position)); iv.setImageBitmap(bitmap);更改为
    Glide.with(mContext).load(path).asBitmap().into(iv)
    如果您遇到滑翔载荷不正确的问题,请阅读this
  7. ((ViewPager) container).addView(iv, 0);,我不确定这一点,但我认为它应该是((ViewPager) container).addView(iv);