【问题标题】:Android download list of images and show images in viewpager with progress dialogAndroid 下载图像列表并在带有进度对话框的 viewpager 中显示图像
【发布时间】:2015-08-11 08:03:26
【问题描述】:

嗨,我正在尝试在 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;
}

}

这是我的浏览器适配器

 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();
    }

}

}

它在获取计数时给我错误,并且不允许在适配器中添加进度条。

【问题讨论】:

  • 请附上logcat报告(stacktrace),以便调试问题
  • 嗨,logcat 中没有任何内容,问题是它可以工作但不显示任何图像:(

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


【解决方案1】:

更改您的代码顺序

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

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

因为当您设置适配器时,适配器的getCount 将被调用,而您之后只分配列表。
更新
以下是我认为您应该检查的更多注意事项:

  1. 确保imageArra 不为空且图片path 正确。
  2. 您应该为myfivepanelpager 添加一个固定高度。
  3. 不要将mContext 存储在ViewPagerAdapter 中,你不需要那个。
  4. instantiateItem:ImageView 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);

【讨论】:

  • 非常感谢解决方案有效,但它没有显示任何内容。如果你愿意,你能指导我如何展示它吗?我可以分享我的整个代码:-)
  • @user2423768,我根据您的代码更新了答案,请检查。如果它仍然不起作用,那么我现在不知道了。
  • 谢谢,但运气不好。您可以通过任何方式为我的查询提供任何想法,其中我有一个需要显示为普通列表视图的列表,并且此列表项包含我必须水平显示的图像列表。我尝试了很多方法,但运气不好。非常感谢您宝贵的时间和解决方案:)
  • @user2423768,您可以将 recylerview 与 LinearLayoutManager.Vertival 用于列表,将 recylerview 与 LinearLayoutManager.Horizo​​ntal 用于项目。
  • 显示一个虚拟图像,看看它是否可见,如果不可见,那么您的 xml 中可能存在问题,可能是您的查看器不可见或它超出了窗口。
猜你喜欢
  • 2023-03-10
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
相关资源
最近更新 更多