我使用Google排球库创建了一个Image加载器。我的问题是我不知道如何为每个请求设置优先级。有人可以帮我一样吗?下面显示的是代码。我应该在哪里设置优先级?
Main Activity
public class MainActivity extends ActionBarActivity {
ListView list;
ImageLoader mImageLoader;
ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
mImageLoader = MyVolley.getInstance(this).getImageLoader();
imageAdapter = new ImageAdapter(this, mImageLoader);
list.setAdapter(imageAdapter);
}
}
Adapter class
public class ImageAdapter extends BaseAdapter {
Context context;
NetworkImageView nImgView;
ImageLoader mImageLoader;
public ImageAdapter(Context context, ImageLoader mImageLoader) {
this.context = context;
this.mImageLoader = mImageLoader;
}
@Override
public int getCount() {
return ImageLinkArray.imageUrl.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_adapter_layout,
parent, false);
}
nImgView = (NetworkImageView) convertView
.findViewById(R.id.networkImageView);
nImgView.setAdjustViewBounds(true);
nImgView.setImageUrl(ImageLinkArray.imageUrl[position], mImageLoader);
return convertView;
}
}
MyVolley class
public class MyVolley {
private static MyVolley mVolleyInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;
private MyVolley(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<>(
ImageLinkArray.imageUrl.length);
@Override
public void putBitmap(String url, Bitmap bitmap) {
// TODO Auto-generated method stub
cache.put(url, bitmap);
}
@Override
public Bitmap getBitmap(String url) {
// TODO Auto-generated method stub
return cache.get(url);
}
});
}
public static synchronized MyVolley getInstance(Context context) {
if (mVolleyInstance == null) {
mVolleyInstance = new MyVolley(context);
}
return mVolleyInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// use the application context
mRequestQueue = Volley.newRequestQueue(mContext
.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
答案 0 :(得分:2)
试试这段代码
private Priority priority = Priority.HIGH;
StringRequest strReq = new StringRequest(Method.GET, Const.URL_STRING_REQ,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
@Override
public Priority getPriority() {
return priority;
}
};
<强>更新强>
这是另一个可行的example
设置Request
的优先级,您必须将一些代码段添加到示例的AppController
类
转换
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
到
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
com.android.volley.Request.Priority pr = req.getPriority();
pr = com.android.volley.Request.Priority.HIGH;
getRequestQueue().add(req);
}
并通过使用一些额外的参数
调用该方法使其可行答案 1 :(得分:0)
查看Volley的ImageRequest课程。它是由Google开发人员为图像加载创建的,您只需要扩展它并实现setPriority()。
// default priority
private Priority mPriority = Priority.LOW;
@Override
public Priority getPriority() {
return mPriority;
}
// priority setter
public void setPriority(Priority priority) {
mPriority = priority;
}
Priority
是来自Request
类的ENUM。
每个请求都有从Request类继承的getPriority()
方法。 Volley RequestQueue
内部使用此方法来确定此队列中的请求优先级。
要使用此功能,您的Request必须覆盖getPriority(),并且必须在请求中实现setPriority()
方法,如上所示。