无法检索无限滚动RecyclerView中的最后一项

时间:2017-04-23 06:15:11

标签: android android-recyclerview recycler-adapter

我有一个无尽的Recycler View,其中包含使用JSoup提取的在线论坛帖子中的数据。在用户到达最后一页之前,recyclerview将自行填充。我想夸大一个"结束视图"一旦用户到达最后一页并滚动到最后一个可见项目,但我无法检索它。

我尝试使用来自recyclerview linearlayoutmanager的getItemCount()和findLastVisibleItemPosition()。它设法获得正确的大小,但"结束视图"每次加载下一页时都会在底部充气。当用户到达最后一页的最后一项时,它不会膨胀。

RecyclerAdapter.java

public RecyclerAdapter(Context context, final ArrayList<ReplyItem> recyclerReplyList, RecyclerView recyclerView) {
    this.recyclerReplyList= recyclerReplyList;
    this.context = context;

    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();
                        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                        verylastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();

                        if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {

                            if (onLoadMoreListener != null) {
                                onLoadMoreListener.onLoadMore();

                            }

                            loading = true;

                        }

                    }
                });

    }

}


@Override
public int getItemViewType(int position) {


    if(recyclerReplyList.get(position) != null){
        return VIEW_POST;
    }

    else if(position == totalItemCount){
        return VIEW_END_ITEM;
    }

}


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

        vh = new PostViewHolder(v);

    }

    else  if (viewType == VIEW_END_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.end_item, parent, false);

        vh = new EndViewHolder(v);

    }

     return vh;
}

2 个答案:

答案 0 :(得分:0)

您可以使用 setOnScrollChangeListener ,但它将支持API 23及更高版本

recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (linearLayoutManager .findLastCompletelyVisibleItemPosition() == linearLayoutManager.findLastVisibleItemPosition())
               //last item reached 

    });

修改

要识别滚动方向,请使用 onScrollChange scrollXscrollY

if (scrollY == 0) { 
    // scrolled horizontally  
} 

答案 1 :(得分:0)

尝试为您的回收商视图设置下边距!