Reputation: 1534
I have implemented Gallery view with Google I\O Volley Volley, Volley presentation and succeed to download images and doing cache.
First, when I was using LruCache for caching, It was very good and Gallery scrolling is nice. But LruCache is not maintaining 16MB of memory but caching only 4MB. I want do cache upto 16MB.
So I shifted to use DiscLruCache in place of LruCache. It doing cache as i want (16MB). But problem is Gallery view scrolling is very slow and it is not smooth while downloading and after doing all cache (16MB).
here is my code.
package com.technotalkative.volleyimageloading;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
import com.android.volley.toolbox.NetworkImageView;
public class SimpleViewFrag extends Fragment {
public static final String TAG = SimpleViewFrag.class.getSimpleName();
private Gallery mGallery;
private List<MyCataLogData> simpleViewList = new ArrayList<MyCataLogData>();
MainActivity mainActivity;
private LayoutInflater inflater;
private int height;
private int width;
private int mItemWidth;
private int mItemHeight;
private SimpleViewAdapter mAdapter;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity) activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getActivity().getWindowManager().getDefaultDisplay();
height = display.getHeight();
width = display.getWidth();
mItemWidth = (int) (width / 1.7f);
mItemHeight = (int) (height / 3f);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflater = inflater;
View v = null;
v = inflater.inflate(R.layout.simple_view, container, false);
// String s = getArguments().getString(UCConstants.CATALOG_CONTENT_KEY);
mGallery = (Gallery) v.findViewById(R.id.simpleViewGalleruy);
mAdapter = new SimpleViewAdapter();
mGallery.setAdapter(mAdapter);
// mGallery.setSelection(1);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
// set gallery to left side
MarginLayoutParams mlp = (MarginLayoutParams) mGallery.getLayoutParams();
mlp.setMargins(-((int) (metrics.widthPixels / 3.8f)), mlp.topMargin,
mlp.rightMargin, mlp.bottomMargin);
parseJsonForCataLog();
mGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.e(TAG, "position: ====="+position);
}
});
return v;
}
public class SimpleViewAdapter extends BaseAdapter {
@Override
public int getCount() {
return simpleViewList.size();
}
@Override
public Object getItem(int position) {
return simpleViewList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NetworkImageView networkImageView = null;
TextView tvTitle = null;
TextView tvText = null;
View childView = null;
if (convertView == null) {
childView = inflater.inflate(R.layout.simple_view_content_item, null);
} else {
childView = convertView;
}
if (childView != null) {
networkImageView = (NetworkImageView) childView
.findViewById(R.id.networkImageView);
networkImageView.getLayoutParams().width = mItemWidth;
networkImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
//getting an object for gallery item
MyCataLogData cld = simpleViewList.get(position);
if (networkImageView != null) {
networkImageView
.setImageUrl(cld.getSimpleImageUrl(), ImageCacheManager.getInstance().getImageLoader());
}
tvTitle = (TextView) childView.findViewById(R.id.title);
if (tvTitle != null) {
tvTitle.setText(cld.getSimpleTitle());
}
tvText = (TextView) childView.findViewById(R.id.text);
if (tvText != null) {
tvText.setText(cld.getSimpleText());
}
}
return childView;
}
}
protected void parseJsonForCataLog() {
try {
String jsonObjectString = getArguments().getString("JSONObject");
JSONObject rootResponce = new JSONObject(jsonObjectString);
Log.e(TAG, "rootResponce: " + rootResponce);
JSONArray simpleArray = rootResponce.optJSONArray("SIMPLE");
if(simpleArray == null)
return;
for (int i = 0; i < simpleArray.length(); i++) {
JSONObject childMenuObject = simpleArray.getJSONObject(i);
String textSimple = childMenuObject.optString("text");
String titleSimple = childMenuObject.optString("title");
String imageUrlSimple = childMenuObject.optString("imageUrl");
MyCataLogData mcld = new MyCataLogData(null ,textSimple, titleSimple, imageUrlSimple);
simpleViewList.add(mcld);
// Log.e(TAG, "" + childMenuObject);
}
mAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 1075
Reputation: 41
I had the same problem for a ListView but I found a solution in googles iosched application. You can try to apply the following but Gallery being deprecated I suggest using a horizontal list view like TwoWayView by Lucas Rocha. This solution can be applied to it, just implement TwoWayView.OnScrollListener instead
add the AbsListView onscroll listener methods and stop volley while scrolling fast/flinging
public class SimpleViewFrag extends Fragment implements AbsListView.OnScrollListener {
ListView mListView;
mListView.setOnScrollListener(this);
@Override
public void onScrollStateChanged( AbsListView view, int scrollState ) {
// Pause disk cache access to ensure smoother scrolling
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
VolleyHelper.getImageLoader().getRequestQueue().stop();
} else {
VolleyHelper.getImageLoader().getRequestQueue().start();
}
}
@Override
public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) {
}
Upvotes: 3
Reputation: 8680
If I understood you correctly (cant' find the ImageLoader
initialization in your sample), it seems you are providing a disk cache to the Volley ImageLoader
constructor, when you should be providing a memory cache.
If your memory cache is too limited in size you should either edit the code to make it bigger, or find / create a new implementation of the ImageCache
interface that works the way you want it.
Whatever you choose - do not use a disk cache where a memory cache is expected.
Upvotes: 1