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;
}
}
よろしくお願いします。どんなフィードバックでも大歓迎です