2017-05-03 119 views
1

我正在尝试设置progressdialogprogressbar而图像不下载,我已经使用了位于绘制文件夹中的形象,我设置的图像作为默认图像而不是下载图片。我怎么可以通过progress repace 默认图像这样的:如何显示进度加载图片时没有下载

enter image description here

JAVA:

public class Art extends Fragment { 
RecyclerView recyclerView; 
GridView myGrid; 
CategoryAdapter adapter; 

public Art() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { 
    final ArrayList<String> movieList = new ArrayList<>(); 
    movieList.add("https://myimage.com"); 
    movieList.add("https://myimage.com"); 
    View v = inflater.inflate(R.layout.fragment_art, container, false); 
    myGrid= (GridView) v.findViewById(R.id.gridViewCategory); 
    adapter =new CategoryAdapter(getActivity(),movieList); 
    myGrid.setAdapter(adapter); 
    myGrid.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        Intent popup = new Intent(getActivity(), Pop.class); 
        popup.putExtra("WallpaperURL", movieList.get(position)); 
        startActivity(popup); 
     } 
    }); 
    return v; 
    } 
} 
class CategoryAdapter extends BaseAdapter { 
    private Context context; 
    private ArrayList<String> imageId=new ArrayList<String>(); 
    CategoryAdapter(Context context,ArrayList<String> imageId){ 
    this.context=context; 
    this.imageId=imageId; 
} 
@Override 
public int getCount() { 
    return imageId.size(); 
} 

@Override 
public Object getItem(int position) { 
    return imageId.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 
class ViewHolder{ 
    ImageView CategoryImage; 
    ViewHolder(View view){ 
     CategoryImage = (ImageView) view.findViewById(R.id.cat_imageView); 
    } 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row=convertView; 
    ViewHolder holder=null; 
    if(row == null){ 
     LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     row=inflater.inflate(R.layout.picture_item,parent,false); 
     holder=new ViewHolder(row); 
     row.setTag(holder); 
    } 
    else{ 
     holder=(ViewHolder) row.getTag(); 
    } 
     Glide.with(context).load(imageId.get(position)).placeholder(R.drawable.arrw).into(holder.CategoryImage); 
     return row; 
     } 
    } 
+0

据我理解你想显示进度,而你的图像被下载? – Umair

+0

@Umair这就是我需要的! – MrMR

+0

@AliILa 请在下面看到我的回答 – Pehlaj

回答

1

没有必要对inflaters或压倒一切的getView(),你只是不必要的放缓,您的应用程序复杂。使用视图可见性更简单。

  • 创建你的布局既发展观和ImageView,使ImageView走了。
  • 制作一个AsyncTask,下载图像并将其设置为ImageView并执行它。
  • AsyncTaskonPostExecute,使进度视图消失,ImageView可见。
2

我想你可能要像这样

view.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE); 
//enable loading 

Glide.with(context) 
    .load(IMAGES.get(position)) 
    .error(R.drawable.matrade_logo2) 
    .placeholder(R.drawable.placeholder) 
    .listener(new RequestListener < String, GlideDrawable >() { 
     @Override 
     public boolean onException(Exception e, String model, Target <GlideDrawable> target, boolean isFirstResource) { 
      // 
      return false; 
     } 

     @Override 
     public boolean onResourceReady(GlideDrawable resource, String model, Target <GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
      // hide loading if image finished downloading 
      return false; 
     } 
    }) 
    .into(imageView); 
+0

picture_item.xml怎么办? <进度 机器人:ID = “@ + ID /进展” 机器人:能见度= “隐形” 机器人:layout_width = “match_parent” 机器人:layout_height = “100dp”/> – MrMR

1

你不这样做是正确的。如果你想下载一个图像,最好的方法是使用asyncTask类来完成。尝试这样的事情会对你有所帮助。或者使用一些类似Glide,Picasso等的库。下面是如何在不使用某些库的情况下做到的。

private class DownloadFileFromURL extends AsyncTask<String, String, File> 
    { 
    String downloadedFilePath = ""; 
    String fileName = ""; 

    /** 
    * Before starting background thread 
    * Show Progress Bar Dialog 
    */ 

    DownloadFileFromURL(String fName) { 
     fileName = fName; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     onCreateDialog(progress_bar_type); 
    } 

    /** 
    * Downloading file in background thread 
    */ 
    @SuppressWarnings("ResultOfMethodCallIgnored") 
    @Override 
    protected File doInBackground(String... f_url) { 
     int count = 0; 
     File convertedFile = null; 
     try { 
      URL url = new URL(f_url[0]); 

      URLConnection connection = url.openConnection(); 
      connection.setDoOutput(true); 
      connection.connect(); 
      // getting file length 
      // getting file length 
      int lenghtOfFile = connection.getContentLength(); 
      String contentType = connection.getContentType(); 


      // input stream to read file - with 8k buffer 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 

      // Output stream to write file 
      downloadedFilePath = getActivity().getFilesDir().getAbsolutePath() + "/" + fileName; 
      OutputStream output = new FileOutputStream(downloadedFilePath); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     return null; 
    } 

    /** 
    * Updating progress bar 
    */ 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     progressDialogForDownload.setProgress(Integer.parseInt(progress[0])); 
    } 

    /** 
    * After completing background task 
    * Dismiss the progress dialog 
    **/ 

    @Override 
    protected void onPostExecute(File file_url) { 
     // dismiss the dialog after the file was downloaded 
     if (progressDialogForDownload != null && progressDialogForDownload.isShowing()) { 
      progressDialogForDownload.dismiss(); 
      progressDialogForDownload = null; 
     } 

     openFileWithIntent(this.downloadedFilePath); 
    } 

} 

并在此方法openFileWithIntent(this.downloadedFilePath);你可以通过任何你想要显示它的图像的下载路径。希望能帮助到你。

1

尝试

showLoader(); //show loader or progress here 
Glide 
    .with(context) // could be an issue! 
    .load(url) 
    .asBitmap() 
    .into(new SimpleTarget<Bitmap>() { 
      @Override 
      public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation){ 
       //hide loader or progress here 
       imageView1.setImageBitmap(bitmap); 
      } 
     }); 
}