解决后,如何通过将RecyclerView与PagerSnapHelper一起使用来获取项目的选定位置?

时间:2019-04-18 11:44:19

标签: android android-recyclerview pagersnaphelper

OnScrolledStateChanged一次又一次被调用,所以我无法获得所选页面:

rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                View centerView = pagerSnapHelper.findSnapView(mLayoutManager);
                int pos = mLayoutManager.getPosition(centerView);

                if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
                    Log.d("Snapped Item Position:",""+pos);
                    // adapter.removeItem(pos);
                }

            }

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

0 个答案:

没有答案