1

傢伙我有一個小問題,我的progressDialog。它不能以正確的方式工作。我的progressDialog是在RecylerView適配器裏面,因爲我使用Glide加載了很多圖片。這只是當我使用glide時顯示progressDialog的唯一方法 - 如果我將progressDialog移動到Asynctask proExecute和postExecute,它只會在doinBackground作業正在執行和隱藏時顯示。但Glide仍在加載圖片。 正如我寫的,我的解決方案,把它放在適配器內,並隱藏,直到圖片加載,工作奇怪。有時當我運行的應用程序,我必須等到圖片加載沒有progressdialog,有時它顯示短時間。如何解決它?在我的情況下使用progressDialog的最佳方式是什麼?Android ProgressDialog裏面的Asynctask和RecyclerView適配器

我的MainActivity:

public class MainActivity extends AppCompatActivity { 

    List<String> shopsNames; 
    ArrayList<RowModel> rowModels; 

    public Realm mRealm; 
    @BindView(R.id.toolbar_layout) 
    CollapsingToolbarLayout toolbarLayout; 
    @BindView(R.id.app_bar) 
    AppBarLayout appBar; 
    private ShopsAdapter adapter; 
    @BindView(R.id.recycler_view) 
    RecyclerView recyclerView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 
     ButterKnife.bind(this); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     ConnectionDetector connectionDetector = new ConnectionDetector(this); 
     if (!connectionDetector.isConnection()) { 
      finish(); 
     } 

     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 123); 



     rowModels = new ArrayList<>(); 
     shopsNames = new ArrayList<>(); 
     Realm.init(this); 

     mRealm = Realm.getDefaultInstance(); 

     initCollapsingToolbar(); 

     Glide.with(getApplicationContext()).load(R.drawable.shoplogo). 
       into((ImageView) findViewById(R.id.backdrop)); 

     adapter = new ShopsAdapter(MainActivity.this, shopsNames); 

     RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(adapter); 

     AsyncTaskRetro asyncTaskRetro = new AsyncTaskRetro(); 
     asyncTaskRetro.execute(); 
    } 

    private class AsyncTaskRetro extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... voids) { 

      PlacesAPI.Factory.getInstance().getPlaces().enqueue(new Callback<Places>() { 

       @Override 
       public void onResponse(Call<Places> call, Response<Places> response) { 
        for (int i = 0; i < response.body().getPosts().size(); i++) { 
         RowModel rowModel = new RowModel(response.body().getPosts().get(i).getNazwa(), 
           Double.parseDouble(response.body().getPosts().get(i).getSzer()), 
           Double.parseDouble(response.body().getPosts().get(i).getDlug())); 
         rowModels.add(rowModel); 
        } 
        String oldName; 
        oldName = rowModels.get(0).getName(); 
        shopsNames.add(rowModels.get(0).getName()); 

        mRealm.beginTransaction(); 
        mRealm.copyToRealm(rowModels); 
        mRealm.commitTransaction(); 

        for (int j = 0; j < rowModels.size(); j++) { 

         if (rowModels.get(j).getName().equals(oldName)) { 
          continue; 
         } 
         oldName = rowModels.get(j).getName(); 
         shopsNames.add(rowModels.get(j).getName()); 
        } 

        //sortowanie listy z nazwami sklepow 
        Collections.sort(shopsNames); 

        adapter = new ShopsAdapter(MainActivity.this, shopsNames); 
        recyclerView.setAdapter(adapter); 
       } 

       @Override 
       public void onFailure(Call<Places> call, Throwable t) { 

       } 
      }); 

      return null; 
     } 
    } 

    private void initCollapsingToolbar() { 
     final CollapsingToolbarLayout collapsingToolbar = 
       (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); 
     collapsingToolbar.setTitle(" "); 
     AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar); 
     appBarLayout.setExpanded(true); 

     // hiding & showing the title when toolbar expanded & collapsed 
     appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
      boolean isShow = false; 
      int scrollRange = -1; 

      @Override 
      public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
       if (scrollRange == -1) { 
        scrollRange = appBarLayout.getTotalScrollRange(); 
       } 
       if (scrollRange + verticalOffset == 0) { 
        collapsingToolbar.setTitle(getString(R.string.app_name)); 
        isShow = true; 
       } else if (isShow) { 
        collapsingToolbar.setTitle(" "); 
        isShow = false; 
       } 
      } 
     }); 
    } 
} 

我的適配器類:

public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> { 

    Context mContext; 
    List<String> shopsList; 
    ProgressDialog progressDialog; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public ImageView thumbnail; 

     public MyViewHolder(View view) { 
      super(view); 
      thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
     } 
    } 

    public ShopsAdapter(Context mContext, List<String> shopsList) { 
     this.mContext = mContext; 
     this.shopsList = shopsList; 

     progressDialog = new ProgressDialog(mContext); 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.shop_card, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 

     shopsList.get(position); 
     try { 
      progressDialog.show(); 
      progressDialog.setMessage("Czekaj..."); 
      String stringformat = String.format("http://www.wp.pl%s.png", shopsList.get(position)); 
      Glide.with(mContext).load(stringformat).listener(new RequestListener<String, GlideDrawable>() { 
       @Override 
       public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { 
        Log.d("fsfsdf", e.getMessage()); 
        return false; 
       } 

       @Override 
       public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
        progressDialog.hide(); 
        return false; 
       } 
      }).centerCrop().fitCenter().into(holder.thumbnail); 
     } catch (Exception e) { 
      Glide.with(mContext).load("http://www.wp.pl/Zabka.png").centerCrop().fitCenter().into(holder.thumbnail); 
     } 

     holder.thumbnail.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       //Log.d("Nazwa sklepu: ", shopsList.get(position)); 
       Intent intent = new Intent(mContext, MapActivity.class); 
       intent.putExtra("shopName", shopsList.get(position)); 
       mContext.startActivity(intent); 
      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     return shopsList.size(); 
    } 
} 

回答

0

最簡單的方法是顯示ImageView的背後進度。將ImageView和ProgressBar添加到RelativeLayout。當圖像被加載時,它會自動隱藏進度條。快捷方式:D

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

    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <ImageView 
     android:layout_width="200dp" 
     android:layout_height="200dp" /> 
</RelativeLayout> 

只需加載圖像,不要對進度條進行任何操作。

+0

很好,但如何在圖像加載時得到通知? –

+0

不需要那個。它看起來像是它的後面的圖像將被加載在它的頂部。 –

+0

你能展示這個佈局應該是什麼樣子嗎?我很困惑,我無法想象它是如何工作的... –

相關問題