向下滚动时RecyclerView自动加载

时间:2016-05-20 06:22:10

标签: android android-recyclerview onscrolllistener

我使用RecyclerView并在向下滚动时实现自动加载项。但我有问题。当服务器只返回一个项目时," onScrolled事件未被调用以加载下一个数据。在我的案例中,项目包含用于加载下一个数据集的数据请帮帮我。 THX。

P.S:对不起我的英文。

mNewsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            int totalItem = mLayoutManager.getItemCount();
            int lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

            if (!mNewsListModel.isWorking() && lastVisibleItem == totalItem - 1 && !mNewsRecyclerAdapter.hasErrorFooter()) {
                mNewsListModel.getNewsListByDate(mNewsCategory, mNewsListModel.getLastResponse().getNewsNextDate());
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

我使用另一种自动加载项解决方案。 使用另一个ViewType将新项添加到适配器(例如,常见项具有ViewType = 0且加载项具有ViewType = 1)。 当recyclelerView使用viewType = 1调用onBindViewHolder时,我们需要开始加载数据。加载并放入已加载的项目后,我们需要删除加载项并将其再次放到列表末尾。

适配器应该是这样的:

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> {

public static class ItemWrapper {
    private int type;
    private String name;

    public ItemWrapper(int type) {
        this.type = type;
    }

    public ItemWrapper(int type, String name) {
        this.type = type;
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public static int getLoaderPosition(ArrayList<ItemWrapper> itemWrappers) {
        for (int i = 0; i < itemWrappers.size(); i++) {
            if (itemWrappers.get(i).getType() == TYPE_LOADER) {
                return i;
            }
        }

        return -1;
    }
}

public static final int TYPE_ITEM = 0;
public static final int TYPE_LOADER = 1;

private boolean loadingInProgress = false;
private ArrayList<ItemWrapper> items;

public static class ViewHolder extends RecyclerView.ViewHolder {

    TextView text_title;

    public ViewHolder(View v, int type) {
        super(v);

        if (type == TYPE_ITEM) {
            text_title = (TextView) v.findViewById(R.id.text_title);
        }
    }
}

public DemoAdapter() {
    items = new ArrayList<>();
    items.add(new ItemWrapper(TYPE_ITEM, "one"));
    items.add(new ItemWrapper(TYPE_ITEM, "two"));
    items.add(new ItemWrapper(TYPE_ITEM, "three"));
    items.add(new ItemWrapper(TYPE_LOADER));
}

public int getItemCount() {
    return items.size();
}

public ItemWrapper getItem(int position) {
    return items.get(position);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elem_demo, parent, false);
        ViewHolder vh = new ViewHolder(v, viewType);
        return vh;

    }
    else if (viewType == TYPE_LOADER) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elem_loader, parent, false);
        ViewHolder vh = new ViewHolder(v, viewType);
        return vh;
    }

    return null;
}

private void startLoadData() {
    if (!loadingInProgress) {
        loadingInProgress = true;

        // load your data here

        // then update list
        onLoadSuccess(loadedItems);
    }
}

private void onLoadSuccess(final ArrayList<ItemWrapper> loadedItems) {
    try {
        final int loaderPosition = ItemWrapper.getLoaderPosition(items);
        items.remove(loaderPosition);
        notifyItemRemoved(loaderPosition);

        items.addAll(loadedItems);
        items.add(new ItemWrapper(TYPE_LOADER));
        notifyItemRangeInserted(loaderPosition, loadedItems.size());
    }
    catch (Exception e) {}
}

@Override
public int getItemViewType(int position) {
    return getItem(position).getType();
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    int type = getItemViewType(position);

    if (type == TYPE_ITEM) {
        holder.text_title.setText(getItem(position).getName());
    }
    else if (type == TYPE_LOADER) {
        startLoadData();
    }
}

}