0
我創建了一個GridView,其中我加載數據。數據有一個圖像鏈接,我得到這個鏈接,並把它放在畢加索加載ImageView。 當我的GridView滾動結束時,我正在將更多數據加載到適配器。 如果適配器數據有更多的40個項目,神祕的滾動工作。 當我停留在適配器的中間位置並嘗試向上滾動時,適配器將我移動到第一個位置,並且所有圖像自動刷新。 任何人都可以幫助我嗎?GridView滾動神祕行爲後notifyDataSetChanged()
MyAdapter:
public class GridArrayAdapter extends BaseAdapter {
private static final String URL_SITE = "some url";
private List<Movie> movies = new ArrayList<>();
private Context context;
public GridArrayAdapter(Context context, List<Movie> model) {
super();
this.movies = model;
this.context = context;
}
public void addMovies(List<Movie> newMovies){
this.movies.addAll(newMovies);
notifyDataSetChanged();
}
@Override
public int getCount() {
return movies.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
Holder holder;
if(convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
holder = new Holder();
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
holder.imageView.setImageDrawable(null);
}
com.squareup.picasso.Picasso
.with(context)
.load(URL_SITE + movies.get(position).getPosterPath())
.into(holder.imageView);
return convertView;
}
class Holder{
ImageView imageView;
}}
滾動代碼:
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
private boolean canScroll = false;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
canScroll = false;
} else if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING ||
scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
canScroll = true;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == filmArray.size()&& canScroll) {
canScroll = false;
getPage(currentPage);
}
}
});
}
獲取數據:
private void getPage(Integer page){
Call<Result> callPage = service.getPopularPage(++page, MovieDBApi.API_KEY);
callPage.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, retrofit2.Response<Result> response) {
currentPage = response.body().getPage();
adapter.addMovies(response.body().getResults());
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", "Fail");
}
});
}
刪除onScrollStateChanged()中的代碼。您不需要控制滾動狀態。你只需要識別列表的最後一個滾動並調用getPage()。 – nnn