Android Query 是個頗酷的 Android library,顧名思義就是給 Android 用但用法很像 jQuery 的東西。
跟 jQuery 一樣,AQuery 的目標是讓 Android developer 可以 write less and do more:
public void renderContent(Content content, View view) {
AQuery aq = new AQuery(view);
aq.id(R.id.icon).image(R.drawable.icon).visible().clicked(this, "someMethod");
aq.id(R.id.name).text(content.getPname());
aq.id(R.id.time).text(FormatUtility.relativeTime(System.currentTimeMillis(), content.getCreate())).visible();
aq.id(R.id.desc).text(content.getDesc()).visible();
}
也可以輕鬆地執行 AJAX:
public void asyncJson(){
// perform a Google search in just a few lines of code
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, this, "jsonCallback");
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status) {
if (json != null){
// successful ajax call
} else {
// ajax error
}
}
另外兩個蠻實用的功能是 getCachedImage 及 shouldDelay,直接參考它們的說明吧:
/**
* Return bitmap cached by image requests. Returns null if url is not cached.
*
* @param url
* @param targetWidth The desired downsampled width.
*
* @return Bitmap
*/
public Bitmap getCachedImage(String url, int targetWidth){
Bitmap result = BitmapAjaxCallback.getMemoryCached(url, targetWidth);
if(result == null){
File file = getCachedFile(url);
if(file != null){
result = BitmapAjaxCallback.getResizedImage(file.getAbsolutePath(), null, targetWidth, true, 0);
}
}
return result;
}
/**
* Determines if a list or gallery view item should delay loading a url resource because the view is scrolling very fast.
* The primary purpose of this method is to skip loading remote resources (such as images) over the internet
* until the list stop flinging and the user is focusing on the displaying items.
*
* If the scrolling stops and there are delayed items displaying, the getView method will be called again to force
* the delayed items to be redrawn. During redraw, this method will always return false, thus allowing a particular
* resource to be loaded.
*
* Designed to be used inside getView(int position, View convertView, ViewGroup parent) of an adapter.
*
* <br>
* <br>
* Example usage:
* <pre>
* public View getView(int position, View convertView, ViewGroup parent) {
* ...
*
* if(aq.shouldDelay(position, convertView, parent, tbUrl)){
* aq.id(R.id.tb).image(placeholder);
* }else{
* aq.id(R.id.tb).image(tbUrl, true, true, 0, 0, placeholder, 0, 0);
* }
*
* ...
* }
* </pre>
*
* <br>
* NOTE:
*
* <br>
* This method uses the setOnScrollListener() method and will override any previously non-aquery assigned scroll listener.
* If a scrolled listener is required, use the aquery method scrolled(OnScrollListener listener) to set the listener
* instead of directly calling setOnScrollListener().
*
* @param position the position of the item
* @param convertView the list item view
* @param parent the parent input of getView
* @param url the content url to be checked if cached and is available immediately
*
* @return delay should delay loading a particular resource
*/
public boolean shouldDelay(int position, View convertView, ViewGroup parent, String url){
if(parent instanceof ExpandableListView){
throw new IllegalArgumentException("Please use the other shouldDelay methods for expandable list.");
}
return Common.shouldDelay(position, convertView, parent, url);
}
這個 library 應該會成為我開發 Android app 的標準配備。