我想在我的列表视图中应用onScroll侦听器,但我在滚动之前加载了所有值,但我想首先加载10个值然后滚动更多10个值应加载等等...
list.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int loadedItems = firstVisibleItem + visibleItemCount;
if((loadedItems == totalItemCount) && !isloading){
//value = value+1;
if(task != null && (task.getStatus() == AsyncTask.Status.FINISHED)){
//isloading = false;
task = new Webconnection();
task.execute();
}
}
}
});
Webconnceion Class
class Webconnection extends AsyncTask<Void, Void, Void> {
LinearLayout linlaHeaderProgress = (LinearLayout)getView().findViewById(R.id.linlaHeaderProgress);
@Override
protected void onPreExecute() {
linlaHeaderProgress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
try {
RestClient client = new RestClient("http://192.168.100.16/html/services/connectpk/web/app.php/api/jobs");
client.AddParam("p", String.valueOf(value));
client.AddParam("limit", String.valueOf(offset));
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
response = client.getResponse();
//JSONObject jObj = new JSONObject(response);
//JSONArray jArray = jObj.getJSONArray("");
JSONArray jArray = new JSONArray(response);
for (int i = 0; i <= offset; i++){
// for (int i = 0; i < jArray.length(); i++) {
isloading = true;
JSONObject jRealObject = jArray.getJSONObject(i);
JobsModel actors = new JobsModel();
actors.setJobTitle(jRealObject.getString("job_title"));
actors.setCompanyLogo(jRealObject.getString("company_logo"));
actors.setCompanyName(jRealObject.getString("company_name"));
actors.setJobLocation(jRealObject.getString("job_city"));
actors.setJobDescription(jRealObject.getString("job_description"));
// actors.setJobQualification(jRealObject.getString("description"));
// actors.setJobBasicSalary(jRealObject.getString("description"));
actors.setJobLastDate(jRealObject.getString("job_startdate"));
actorlist.add(actors);
value+=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
if (response != null) {
//Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
adapter = new JobsAdapter(getActivity(), R.layout.activity_jobs, actorlist);
list.setAdapter(adapter);
linlaHeaderProgress.setVisibility(View.GONE);
isloading = false;
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
不要使用onScroll。使用lazy loading
。简而言之,这意味着在最后一个或前一个位置调用适配器getView()
后加载更多数据
答案 1 :(得分:0)
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if((firstVisibleItem + visibleItemCount) == totalItemCount)
{
Log.i("Info", "Scroll Bottom" );
}
}});