将项目列表添加到回收者视图中>>

问题描述 投票:0回答:2

我想将元素添加到Android的回收者视图显示的项目列表中。

到目前为止,在我的代码中,当列表接近结尾时,我获取了更多项目,并将新项目列表附加到显示的当前列表中。然后,我调用notifyDataSetChanged(),但出现以下错误:

 java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView...

当回收者停留在当前显示的项目上时,我如何更新列表以显示?

这是我的代码:

 private class StatusRecyclerAdapter extends RecyclerView.Adapter<StatusRecyclerHolder> {
    private List<Status> statues;
    private int size;


    private final int LOAD_POSITION = 3;


    public StatusRecyclerAdapter() {
         statues = feed.getStatuses();
    }

    @NonNull
    @Override
    public StatusRecyclerHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        return new StatusRecyclerHolder(layoutInflater, viewGroup);
    }

    @Override
    public void onBindViewHolder(@NonNull StatusRecyclerHolder statusRecyclerHolder, int i) {
        statusRecyclerHolder.bind(statues.get(i));
        if(size - i == LOAD_POSITION){
            getContentNextPage();
        }
    }

    @Override
    public int getItemCount() {
        size = 0;
        if(statues == null){
            Log.i(TAG, "List of status null" );
        }
        else{
            size = statues.size();
        }

        return size;
    }

    private void getContentNextPage(){
        this.statues = Feed.getNextPage();
        }

        notifyDataSetChanged();
    }
}

谢谢!

我想将元素添加到Android的回收者视图显示的项目列表中。到目前为止,在我的代码中,当列表接近结尾时,我获取了更多项目,并将新项目列表追加到...

android android-recyclerview view
2个回答
0
投票
WHILE

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.