我有一个列表视图,可以从parse.com检索数据。我添加了onscroll侦听器以在每15个项目后加载列表项。它工作正常,但是当整个列表加载时,当我们想要滚动回到顶部时,滚动不顺畅,就像上下移动一样。为了更好地理解我的问题watch this video
我的代码
public class InterActivity extends Activity
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
private int limit = 15;
View footerView;
@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) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit);
ob = query.find();
for (ParseObject inter : ob) {
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} 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);
// 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) { // TODO Auto-generated method stub
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
}
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter = new FinalAdapter(InterActivity.this,
codelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
答案 0 :(得分:1)
每次加载更多数据时,都不应重新创建列表和适配器。用这个替换LoadMore类:
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist.clear();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter.notifyDataSetChanged();
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
告诉我:)
答案 1 :(得分:0)
尝试使用OnTouchListener
代替OnScrollListener
(这将帮助您更轻松地检测滚动方向并做出相应反应):
listView.setOnTouchListener(new View.OnTouchListener() {
float height;
@Override
public boolean onTouch(View v, MotionEvent event) {
float height = event.getY();
int action = event.getAction();
int threshold = 1;
int count = listview.getCount();
if(action == MotionEvent.ACTION_DOWN){
// do nothing
this.height = height;
}else if(action == MotionEvent.ACTION_UP){
if(this.height < height){
Log.v(TAG, "Scrolled up");
// do nothing - user is going up
}else if(this.height > height){
Log.v(TAG, "Scrolled down");
//execute LoadMore Task
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
return false;
}
});