我有一个包含listview的活动。当用户滚动到列表视图的底部时,该列表将获取更多数据。当用户向下滚动时,滚动是平滑的。但是,当用户向上滚动时,列表视图往往会将其可见项目跳过一两个。
课程:
public class ListViewActivity extends Activity implements OnScrollListener {
int id = 0;
List<String> dataParse = new ArrayList<String>();
LazyAdapter adapter;
ListView listView;
List<RichPost> richArrayList = new ArrayList<RichPost>();
private int currentScrollState;
private int currentVisibleItemCount;
private int skipParam = 0;
private boolean flag_loading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_list_list);
listView = (ListView) findViewById(R.id.listView);
// Need to create the adapter constructor
listView.setOnScrollListener(this);
adapter = new LazyAdapter(PostListActivity.this, dataParse, richArrayList);
listView.setAdapter(adapter);
setStory();
}
public void setStory() {
// Pull data from Parse
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("userid", ParseUser.getCurrentUser().getObjectId());
params.put("skip", skipParam);
ParseCloud.callFunctionInBackground("studentsPosts", params, new FunctionCallback<List<List<ParseObject>>>() {
@Override
public void done(List<List<ParseObject>> data, com.parse.ParseException arg1) {
if (data == null) {
} else {
Log.e("size ", "RUNNING Size : " + data.size());
if (data.size() > 0) {
flag_loading = false;
for (int i = 0; i < data.size(); i++) {
RichPost rc = new RichPost();
if (data.get(0).get(0).get("htmlContent") != null) {
// Switch i to 1 to force just 1 additon.
dataParse.add(data.get(i).get(0).getString("htmlContent"));
rc.setHtmlCode(data.get(i).get(0).getString("htmlContent"));
Log.i("Calling notifiyDataSetchanged", "now");
}
if(data.get(i).get(0).get("photos") != null){
List<ParseFile> pFileList;
pFileList = (List<ParseFile>) data.get(i).get(0).get("photos");
rc.setPhotosArray(pFileList);
}
if(data.get(i).get(0).getParseFile("photo") != null){
//Time to retrieve the photo
ParseFile getParseFile;
getParseFile = (ParseFile) data.get(i).get(0).get("photo");
//ParseFile getParseFile = ParseFile.this.getDataInBackground(data.get(i).get(0).getParseFile("photo"));
rc.setPhoto(getParseFile);
}
richArrayList.add(rc);
adapter.notifyDataSetChanged();
}
}
}
}
});
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
private void isScrollCompleted() {
if (this.currentVisibleItemCount >= 5 && this.currentScrollState == SCROLL_STATE_IDLE) {
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentVisibleItemCount = visibleItemCount;
Log.e("firstVisibleItem", ""+firstVisibleItem);
Log.e("visibleItemCount", ""+visibleItemCount);
Log.e("totalItemCount", ""+totalItemCount);
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
if (flag_loading == false) {
flag_loading = true;
Log.d("IN ON SCROLL", "NOW: " + skipParam);
skipParam = skipParam + 10;
setStory();
}
}
}
}
我哪里错了?为什么向上滚动会受到影响?