我有一个实现OnScrollListener的ArrayAdapter。 OnScroll方法被调用很多次,大多数情况下不止一次。现在永远不会调用OnScrollChangedState,我完全不知道为什么不。当他被称为后来的时候我读了,我没有得到这种行为。这是我的代码
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
/* Some code */
setNewListAdapter()
getListView().setOnScrollListener(movieListAdapter);
}
public class MovieListAdapter extends ArrayAdapter<Movie> implements AbsListView.OnScrollListener{
private ImageView thumbnail;
private TextView title;
private RatingBar ratingBar;
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int currentScrollState;
private int totalItemCount;
public MovieListAdapter(Context context, ArrayList<Movie> movies) {
super(context, 0, movies);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Movie movie = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_list, parent, false);
}
// Lookup view
thumbnail = (ImageView) convertView.findViewById(R.id.film_cover);
title = (TextView) convertView.findViewById(R.id.movieTitle);
ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar);
// Populate the views
new ImageLoader(movie.getPoster_path(),thumbnail).execute();
title.setText(movie.getTitle());
ratingBar.setRating((float)movie.getVote_average());
// Return the completed view to render on screen
return convertView;
}
private void updateData(){
this.clear();
for(Movie m : movieArrayList){
this.insert(m,this.getCount());
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
/* Detect if the scroll has been completed*/
private void isScrollCompleted() {
final int lastItem = currentFirstVisibleItem + currentVisibleItemCount;
if (this.currentScrollState == SCROLL_STATE_IDLE && lastItem == totalItemCount) {
listener.nextPage(++currentPage);
updateData(); // Refresh Movies List
} else if (this.currentScrollState == SCROLL_STATE_IDLE && (currentFirstVisibleItem + totalItemCount == totalItemCount)) {
listener.previousPage(--currentPage);
updateData(); // Refresh Movies List
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItemCount = totalItemCount;
}
}
先谢谢你们。任何反馈都表示赞赏
答案 0 :(得分:1)
我猜你在这里犯了一个逻辑错误,你可以尝试更换如下并检查它是否有效吗?
lastItem == totalItemCount
到
lastItem == totalItemCount - 1
并且同样替换
currentFirstVisibleItem + totalItemCount == totalItemCount
到
currentFirstVisibleItem + totalItemCount == totalItemCount - 1