Search in sources :

Example 1 with SnapPageScrollListener

use of me.relex.recyclerpager.SnapPageScrollListener in project CircleIndicator by ongakuer.

the class LoopRecyclerViewFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    mAdapter = new LoopRecyclerAdapter(5);
    mIndicator = view.findViewById(R.id.indicator);
    RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(mAdapter);
    mIndicator.createIndicators(mAdapter.getRealItemCount(), 0);
    PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
    pagerSnapHelper.attachToRecyclerView(recyclerView);
    recyclerView.addOnScrollListener(new SnapPageScrollListener() {

        @Override
        public void onPageSelected(int position) {
            mIndicator.animatePageSelected(mAdapter.getRealPosition(position));
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (positionOffsetPixels == 0) {
                recyclerView.scrollToPosition(mAdapter.getLoopPosition(position));
            }
        }
    });
}
Also used : PagerSnapHelper(androidx.recyclerview.widget.PagerSnapHelper) RecyclerView(androidx.recyclerview.widget.RecyclerView) SnapPageScrollListener(me.relex.recyclerpager.SnapPageScrollListener) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) LoopRecyclerAdapter(me.relex.circleindicator.sample.LoopRecyclerAdapter)

Aggregations

LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 PagerSnapHelper (androidx.recyclerview.widget.PagerSnapHelper)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 LoopRecyclerAdapter (me.relex.circleindicator.sample.LoopRecyclerAdapter)1 SnapPageScrollListener (me.relex.recyclerpager.SnapPageScrollListener)1