我正在构建一个谷歌图片搜索器应用程序,它以网格视图返回结果。我想实现无限滚动机制来异步加载图像。我为EndlessScrolling创建了一个新类。我无法确定用于onLoadMore
方法和customLoadMoreDataFromApi
的逻辑。我发布了我的主要活动和我创建的EndlessScrolling类。
SearchActivity.java
package com.example.gridimagesearch;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
public class SearchActivity extends Activity {
EditText etQuery;
GridView gvResults;
Button btnSearch;
ArrayList<ImageResults> imageResults= new ArrayList<ImageResults>();
ImageResultsArrayAdapter imageAdapter;
private final int REQUEST_CODE=20;
String imageSize;
String colorFilter;
String imageType;
String siteFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setViews();
imageAdapter= new ImageResultsArrayAdapter (this, imageResults);
gvResults.setAdapter(imageAdapter);
gvResults.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
customLoadMoreDataFromApi(page);
// or customLoadMoreDataFromApi(totalItemsCount);
}
});
gvResults.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View parent, int position,long rowId) {
Intent i = new Intent(SearchActivity.this, ImageDisplayActivity.class);
ImageResults imageResult = imageResults.get(position);
i.putExtra("results", imageResult);
startActivity(i);
}
});
}
public void onAdvSearch(MenuItem mi){
Intent i = new Intent(SearchActivity.this, AdvancedSearchActivity.class);
i.putExtra("mode",2);
startActivityForResult(i, REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
imageSize = data.getExtras().getString("imagesize");
colorFilter = data.getExtras().getString("colorfilter");
imageType = data.getExtras().getString("imagetype");
siteFilter = data.getExtras().getString("sitefilter");
//Toast.makeText(this, imageSize, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
public void setViews(){
etQuery=(EditText) findViewById(R.id.etQuery);
gvResults=(GridView)findViewById(R.id.gvResults);
btnSearch=(Button)findViewById(R.id.btnSearch);
}
public void onImageSearch(View v){
String query=etQuery.getText().toString();
Toast.makeText(this, "Searching for " +query, Toast.LENGTH_SHORT).show();
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://ajax.googleapis.com/ajax/services/search/images?rsz=8&" + "start=" + 0 + "&v=1.0&q=" + Uri.encode(query) + "&imgsz=" + Uri.encode(imageSize) + "&imgcolor=" + Uri.encode(colorFilter) + "&imgtype=" + Uri.encode(imageType) + "&as_sitesearch=" + Uri.encode(siteFilter), new JsonHttpResponseHandler(){
@Override
public void onSuccess(JSONObject response) {
JSONArray imageJsonResults=null;
try{
imageJsonResults=response.getJSONObject("responseData").getJSONArray("results");
imageResults.clear();
imageAdapter.addAll(ImageResults.fromJSONArray(imageJsonResults));
Log.d("DEBUG", imageResults.toString());
}catch (JSONException e){
e.printStackTrace();
}
}
});
}
}
EndlessScrolling Class
package com.example.gridimagesearch;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
public abstract class EndlessScrollListener implements OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the data set after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener(){
}
public EndlessScrollListener(int visibleThreshold){
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
@Override
public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount)
{
if (!loading && (totalItemCount < previousTotalItemCount)) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
if (loading) {
if (totalItemCount > previousTotalItemCount) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
}
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold))
{
onLoadMore(currentPage + 1, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
有人可以告诉我如何实现onLoadMore方法以使其正常工作。
感谢。