22

我已经尝试为 LinearLayoutManager 实现无限滚动并且它成功并尝试将 LinearLayoutManager 实现复制到 StaggeredGridLayoutManager 但它不起作用。

我只想得到第一个VisibleItem。

在线性布局管理器中:

int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition(int);

但在 StaggeredGridLayoutManager 中是:

int firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(int[])

如何使用(int)而不是(int [])获得firstVisibleItem?

有什么好的方法/实现吗?

提前致谢。

4

4 回答 4

28

我让它工作:

您可以使用 StaggeredGridLayoutManager 中的两种方法之一:

  1. findFirstVisibleItemPositions(int[])
  2. findFirstCompletelyVisibleItemPositions(int[])

传递一个空的 int 数组,该数组将使用位置进行初始化,并使用对您有意义的数组。

private boolean loading = true;
private int pastVisibleItems, visibleItemCount, totalItemCount;

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener({
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        int[] firstVisibleItems = null;
        firstVisibleItems = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
        if(firstVisibleItems != null && firstVisibleItems.length > 0) {
            pastVisibleItems = firstVisibleItems[0];
        }

        if (loading) {
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                loading = false;
                Log.d("tag", "LOAD NEXT ITEM");
            }
        }
    }
});
于 2015-03-27T02:49:57.077 回答
7

这是我的一个ScrollListener.

这是我实现的代码ScrollListener

private EndlessScrollListener scrollListener =
    new EndlessScrollListener(new EndlessScrollListener.RefreshList() {
        @Override public void onRefresh(int pageNumber) {
            //Here you can execute server connection or anything else to update data and present with Recycler view
            // Notice: It is sync method

        }
    });

recyclerView.addOnScrollListener(scrollListener);

自定义ScrollListener类:

class EndlessScrollListener extends RecyclerView.OnScrollListener {
    private boolean isLoading;
    private boolean hasMorePages;
    private int pageNumber = 0;
    private RefreshList refreshList;
    private boolean isRefreshing;
    private int pastVisibleItems;



    public EndlessScrollListener(RefreshList refreshList) {
        this.isLoading = false;
        this.hasMorePages = true;
        this.refreshList = refreshList;
    }

    @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        StaggeredGridLayoutManager manager =
            (StaggeredGridLayoutManager) recyclerView.getLayoutManager();

        int visibleItemCount = manager.getChildCount();
        int totalItemCount = manager.getItemCount();
        int[] firstVisibleItems = manager.findFirstVisibleItemPositions(null);
        if (firstVisibleItems != null && firstVisibleItems.length > 0) {
            pastVisibleItems = firstVisibleItems[0];
        }

        if ((visibleItemCount + pastVisibleItems) >= totalItemCount && !isLoading) {
            isLoading = true;
            if (hasMorePages && !isRefreshing) {
                isRefreshing = true;
                new Handler().postDelayed(new Runnable() {
                    @Override public void run() {
                        refreshList.onRefresh(pageNumber);
                    }
                }, 200);
            }
        } else {
            isLoading = false;
        }
    }

    public void noMorePages() {
        this.hasMorePages = false;
    }

    public void notifyMorePages() {
        isRefreshing = false;
        pageNumber = pageNumber + 1;
    }



    public interface RefreshList {
        void onRefresh(int pageNumber);
    }
}
于 2017-02-13T16:53:04.930 回答
1

要实现 EndlessRecyclerOnScrollListener 首先创建 EndlessRecyclerOnScrollListener java 类

   import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
    private int scrolledDistance = 0;
    private boolean controlsVisible = false;

    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.

    private int pastVisibleItems, visibleItemCount, totalItemCount;

    private int current_page = 1;

    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    public EndlessRecyclerOnScrollListener(StaggeredGridLayoutManager staggeredGridLayoutManager) {
        this.mStaggeredGridLayoutManager = staggeredGridLayoutManager;

    }

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

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mStaggeredGridLayoutManager.getItemCount();
        //firstVisibleItem = mStaggeredGridLayoutManager.findFirstVisibleItemPosition();
        int[] firstVisibleItems = null;
        firstVisibleItems = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
        if (firstVisibleItems != null && firstVisibleItems.length > 0) {
            pastVisibleItems = firstVisibleItems[0];
        }


        if (loading) {
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (pastVisibleItems + visibleThreshold)) {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }

        if (scrolledDistance > 1 && controlsVisible) {
            controlsVisible = false;
            scrolledDistance = 0;
        } else if (scrolledDistance < -1 && !controlsVisible) {
            controlsVisible = true;
            scrolledDistance = 0;
        }

        if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) {
            scrolledDistance += dy;
        }
    }

    public abstract void onLoadMore(int current_page);

    ;
}

进入您的活动或片段(此示例用于片段)后,使用下一个代码

    RecyclerView mRecyclerView;
StaggeredGridLayoutManager mStaggeredGridLayoutManager;

private RecyclerView.Adapter adapter;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle saveInstanceState) {

        View v = inflater.inflate(R.layout.notices_layout, container, false);

        mRecyclerView = (RecyclerView) v.findViewById(R.id.listaNoticias);
 mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(
                2, //number of grid columns
                GridLayoutManager.VERTICAL);

//Sets the gap handling strategy for StaggeredGridLayoutManager
        mStaggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

        mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);

//initializing our adapter
adapter = new MyAdapter(list, getContext());

//Adding adapter to recyclerview
mRecyclerView.setAdapter(adapter);

mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mStaggeredGridLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                // do something...
                getData(current_page);
            }
        });

return v;
}
于 2016-08-25T18:32:20.410 回答
-1
int mSpanCount = 2;

int[] into = new int[mSpanCount];

int firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0];
于 2015-03-16T17:15:40.317 回答