1
我有一個列表視圖,當列表項目到達20個列表項目時,應該加載更多列表項目。我已添加onscroll偵聽器來執行此操作 但它沒有按照我的預期工作。列表視圖的Onscroll監聽器不起作用
要完全理解請看這video。
我的代碼:
public class InterActivity extends Activity implements OnItemClickListener, OnItemLongClickListener
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
boolean loadingMore = false;
private int limit = 20;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
shrdPreference = new SharedPreference();
//Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading");
// Set progressdialog message
mProgressDialog.setMessage("Please wait loading ...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
//my own doinbackground()
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.inter_layoutListView);
// Pass the results into ListViewAdapter.java
adapter = new FinalAdapter(InterActivity.this,
codelist);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
animationAdapter.setAbsListView(listview);
View loadMoreView = ((LayoutInflater)InterActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.loadmore, null, false);
listview.addFooterView(loadMoreView);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setOnItemClickListener(InterActivity.this);
listview.setOnItemLongClickListener(InterActivity.this);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount) && !(loadingMore)){
new LoadMoreDataTask().execute();
}
}
});
}
}
private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// my own doinbackground()
}
@Override
protected void onPostExecute(Void result) {
// Locate listview last item
int position = listview.getLastVisiblePosition();
// Pass the results into ListViewAdapter.java
adapter = new FinalAdapter(InterActivity.this, codelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Show the latest retrived results on the top
listview.setSelectionFromTop(position, 0);
}
}
在這裏看到提示http://stackoverflow.com/questions/19466233/change-single-item-listview-android -with-的AsyncTask – Tauqir