nav*_*ons 4 android android-recyclerview
我正在使用回收视图,我通过 json 从网络获取数据并使用适配器将项目添加到我的回收视图。我想在用户到达回收视图末尾时获取新项目。
我使用 GridLayoutManager 作为我的回收视图。这是代码:
int pastVisiblesItems, visibleItemCount, totalItemCount;
GridLayoutManager mLayoutManager;
private boolean loading = true;
recycle=(RecyclerView)findViewById(R.id.recycle);
mLayoutManager = new GridLayoutManager(this, 3);
// (Context context, int spanCount)
recycle.setLayoutManager(mLayoutManager);
recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loadmore) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
page = page + 1;
Toast.makeText(Cats.this, "test", Toast.LENGTH_SHORT);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我到了 recycleview 的结尾,但它没有显示任何 toast,所以我认为它不明白我到达了 recycle view 的结尾。
这段代码有什么问题?
我为此沮丧了很长时间。不过终于找到了解决办法。如果您使用的是 ,则RecyclerView您也必须使用自定义适配器。在您填充视图的地方,只需检查您是否正在填充列表中的最后一个元素。
public abstract class Adapter extends RecyclerView.Adapter<VH> {
protected final List<String> items;
protected final Context context;
protected Adapter(Context context) {
this.context = context;
this.items = new ArrayList<>();
}
@SuppressWarnings("unused")
public final void add(String item) {
items.add(item);
notifyDataSetChanged();
}
@Override
public final void onBindViewHolder(VH holder, int position) {
holder.textView.setText(items.get(position).toString());
if (position == items.size() - 1)
// Reached End of List
}
@Override
public final int getItemCount() {
return items.size();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13967 次 |
| 最近记录: |