我无法解决这个问题,可能是因为缺乏经验。我在服务器上有图像,我在JSON中获取它们的路径以及描述和名称。我正在通过Volley获得所有这些数据。
private static final String TAG = "StoreActivity";
private RecyclerView shopsRecyclerView;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Store> stores;
private StoreAdapter storeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
shopsRecyclerView = (RecyclerView) findViewById(R.id.shopsRecyclerView);
shopsRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
shopsRecyclerView.setLayoutManager(layoutManager);
stores = new ArrayList<>();
storeAdapter = new StoreAdapter(getApplicationContext(), stores);
shopsRecyclerView.addOnItemTouchListener(new StoreAdapter.RecyclerTouchListener(getApplicationContext(), shopsRecyclerView, new StoreAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Intent i = new Intent(StoreActivity.this, ProductsActivity.class);
startActivity(i);
}
@Override
public void onLongClick(View view, int position) {
}
}));
fetchStores();
}
private void fetchStores() {
JsonObjectRequest fetchAllStores = new JsonObjectRequest(Request.Method.POST, API.GET_STORES, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Fetch Stores: " + response);
showStores(response);
shopsRecyclerView.setAdapter(storeAdapter);
storeAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Fetch Stores Error: " + error.getMessage());
}
});
ApplicationController.getInstance().addToRequestQueue(fetchAllStores);
}
private void showStores(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("images");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Store store = new Store();
store.setId(jsonObject.getString("id"));
store.setImage_url(jsonObject.getString("url"));
store.setTitle(jsonObject.getString("name"));
stores.add(store);
}
} catch (JSONException e) {
Log.d(TAG, "Show Stores: " + e.getMessage());
}
}
然后我把我从Volley中获得的路径/ url放入Glide并在RecyclerView中加载所有图像。
@Override
public void onBindViewHolder(StoreViewHolder holder, final int position) {
Store store = stores.get(position);
holder.tvDescription.setText(store.getTitle());
Glide.with(context)
.load(store.getImage_url())
.placeholder(android.R.drawable.ic_menu_upload_you_tube)
.error(android.R.drawable.stat_notify_error)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.ivImage);
}
下次打开相同的活动时,会再次发送请求并再次下载图片?或者它们以前是以某种方式缓存的,它们是从缓存中加载的? 当我关闭互联网并开启相同的活动时,没有任何反应,因为无法发送截击请求,并且我认为高速缓存的图像未被显示。 对我的问题最好的方法是什么?基本上,我需要从服务器获取图像的路径并将它们放在我的recyclerview中,但我也希望它们被缓存,以免每次都下载它们。
答案 0 :(得分:0)
我想建议您使用Glide lib,非常简单并为您的应用程序缓存图像,我自己尝试过。您还可以在网址中添加签名,以便下次更新时必须更改。
以下是代码段:
dependencies {
// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
String imgUrl = "http://imageurl";
ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
Glide.with(mContext).load(imgUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
希望这有助于。