伙计们我的progressDialog有点问题。它没有以正确的方式工作。我的progressDialog在RecylerView Adapter中,因为我使用Glide加载了许多图片。当我使用Glide时,这是显示progressDialog的唯一方法 - 如果我将progressDialog移动到Asynctask proExecute并且postExecute它只显示doinBackground作业正在执行和隐藏时。但Glide仍在加载图片。 正如我写的,我的解决方案是把它放在适配器和隐藏,直到图片加载,工作奇怪。有时当我运行应用程序时,我必须等到pic加载而没有进度转换,有时它会显示很短的时间。怎么解决?在我的情况下使用progressDialog的最佳方法是什么?
我的主要活动:
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 :(得分: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>
只需加载图片,不要对进度条做任何事情。