如何制作无尽的回收者观点

时间:2015-03-03 17:34:44

标签: android android-recyclerview

我在SOF上看到了很多问题..我的问题是相似的,如果不一样,我试图让回收者视图继续滚动,问题的答案说在布局管理器中使用一个方法叫:< / p>

  

findLastVisibleItemPosition()

但我似乎无法在布局管理器中找到该方法。这是我的代码:

      premiumOffers = (RecyclerView) view.findViewById(R.id.premiumOffers);
            layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);

  premiumOffers.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == 0) {
                    Log.v("pos ", recyclerView.getLayoutManager().getChildCount() + "");
                    if (recyclerView.getChildPosition(recyclerView.getFocusedChild()) == recyclerView.getAdapter().getItemCount() - 1) {
                    }

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // Log.v("OnScrolled", dx + " | " + dy);
            }
        });

4 个答案:

答案 0 :(得分:1)

请务必在LinearLayoutManager上调用该方法

android.intent.extra.REFERRER_NAME

按照本指南进行完美的工作: EndlessRecyclerOnScrollListener

答案 1 :(得分:0)

请按照步骤

进行操作

http://android-pratap.blogspot.in/2015/01/endless-recyclerview-onscrolllistener.html

您将能够实现您想要的目标。

答案 2 :(得分:0)

这是一个可以做你想做的recyclelerView的例子。只需实现将在最后一个项目上调用的OnLastVisible回调。

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by poliveira on 04/12/2014.
 */
public class CustomRecyclerView extends RecyclerView {
    private int mLastVisibleItem;
    private OnLastVisible mCallbacks;
    private IScrollCallbacks mScrollCallbacks;

    public CustomRecyclerView(Context context) {
        super(context);
        init();
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setOnScrollListener(new OnScrollListener() {
                                @Override
                                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                    if (newState == SCROLL_STATE_IDLE) {
                                        if (mScrollCallbacks != null)
                                            mScrollCallbacks.onIdleScroll();
                                    } else if (mScrollCallbacks != null)
                                        mScrollCallbacks.onScroll();
                                    super.onScrollStateChanged(recyclerView, newState);
                                }

                                @Override
                                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                                    int lastVisible = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                                    if (recyclerView.getAdapter().getItemCount() - 1 == lastVisible && lastVisible != mLastVisibleItem) {
                                        mLastVisibleItem = lastVisible;
                                        if (mCallbacks != null) {
                                            mCallbacks.loadMore();
                                        }
                                    }
                                }
                            }
        );
    }

    public OnLastVisible getCallbacks() {
        return mCallbacks;
    }

    public void setCallbacks(OnLastVisible callbacks) {
        mCallbacks = callbacks;
    }


    public IScrollCallbacks getScrollCallbacks() {
        return mScrollCallbacks;
    }

    public void setScrollCallbacks(IScrollCallbacks scrollCallbacks) {
        mScrollCallbacks = scrollCallbacks;
    }

    public interface IScrollCallbacks {
        void onScroll();

        void onIdleScroll();
    }

    public interface OnLastVisible {
        void loadMore();
    }
}

答案 3 :(得分:0)

您可以简单地覆盖onscrolled方法

 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (null == mLayoutManager || null == mAdapter) {
                return;
            }
            mVisibleItemCount = mLayoutManager.getChildCount();
            mTotalItemCount = mLayoutManager.getItemCount();
            mFirstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
            if ((mVisibleItemCount + mFirstVisibleItemPosition) >= mTotalItemCount) {
                loadMore();
            }
        }
    });

您可以在此处查看演示: https://github.com/yefengfreedom/YfRecyclerView