android – 添加项目到无尽滚动RecyclerView与底部的ProgressBar

我遵循了Vilen对SO:Put an indeterminate progressbar as footer in a RecyclerView grid的极好回答,介绍了如何使用ProgressBar实现无限滚动循环观看。

我自己实现它,它的工作,但我想扩展的例子。我想在循环视图的顶部添加额外的项目,类似于当你添加一个新的状态更新Facebook的做法。

我不能够成功地添加额外的项目到列表 – 这里是我添加到Vilen的代码在他的MainActivity的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.add) {
        myDataset.add(0, "Newly added");
        mRecyclerView.smoothScrollToPosition(0);
        mAdapter.notifyItemInserted(0);
}
return super.onOptionsItemSelected(item);
}

当我点击“添加”按钮:

当我向下滚动时,我得到两个旋转而不是一个:

当旋转器完成并加载下5个项目时,旋转器仍然存在:

我究竟做错了什么?

问题是,当你添加新项目内部EndlessRecyclerOnScrollListener不知道它和计数器打破。
事实上,EndlessRecyclerOnScrollListener的答案有一些限制和可能的问题,例如。如果你一次加载1项,它将不工作。所以这里是一个增强版本。

>摆脱EndlessRecyclerOnScrollListener,我们不再需要它了
>将您的适配器更改为包含滚动监听器的适配器

public class MyAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private List<T> mDataset;

    // The minimum amount of items to have below your current scroll position before loading more.
    private int visibleThreshold = 2;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;

    public MyAdapter(List<T> myDataSet, RecyclerView recyclerView) {
        mDataset = myDataSet;

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                    if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        // End has been reached
                        // Do something
                        if (onLoadMoreListener != null) {
                            onLoadMoreListener.onLoadMore();
                        }
                        loading = true;
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mDataset.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh;
        if (viewType == VIEW_ITEM) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(android.R.layout.simple_list_item_1, parent, false);

            vh = new TextViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.progress_item, parent, false);

            vh = new ProgressViewHolder(v);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TextViewHolder) {
            ((TextViewHolder) holder).mTextView.setText(mDataset.get(position).toString());
        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
        }
    }

    public void setLoaded() {
        loading = false;
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public static class TextViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public TextViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(android.R.id.text1);
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
        }
    }
}

>更改Activity类中的代码

mAdapter = new MyAdapter<String>(myDataset, mRecyclerView);
mRecyclerView.setAdapter(mAdapter);

mAdapter.setOnLoadMoreListener(new MyAdapter.OnLoadMoreListener() {
    @Override
    public void onLoadMore() {
        //add progress item
        myDataset.add(null);
        mAdapter.notifyItemInserted(myDataset.size() - 1);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //remove progress item
                myDataset.remove(myDataset.size() - 1);
                mAdapter.notifyItemRemoved(myDataset.size());
                //add items one by one
                for (int i = 0; i < 15; i++) {
                    myDataset.add("Item" + (myDataset.size() + 1));
                    mAdapter.notifyItemInserted(myDataset.size());
                }
                mAdapter.setLoaded();
                //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
            }
        }, 2000);
        System.out.println("load");
    }
});

其余保持不变,让我知道如果这对你有用。

翻译自:https://stackoverflow.com/questions/30681905/adding-items-to-endless-scroll-recyclerview-with-progressbar-at-bottom

转载注明原文:android – 添加项目到无尽滚动RecyclerView与底部的ProgressBar