Search in sources :

Example 1 with PagerSnapHelper

use of androidx.recyclerview.widget.PagerSnapHelper in project CircleIndicator by ongakuer.

the class RecyclerViewFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    mAdapter = new SampleRecyclerAdapter(5);
    RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(mAdapter);
    PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
    pagerSnapHelper.attachToRecyclerView(recyclerView);
    // CircleIndicator2 for RecyclerView
    CircleIndicator2 indicator = view.findViewById(R.id.indicator);
    indicator.attachToRecyclerView(recyclerView, pagerSnapHelper);
    // Scroll To Position
    layoutManager.scrollToPosition(2);
    // Observe Data Change
    mAdapter.registerAdapterDataObserver(indicator.getAdapterDataObserver());
    view.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mAdapter.add();
        }
    });
    view.findViewById(R.id.remove).setOnClickListener(v -> {
        mAdapter.remove();
    });
}
Also used : CircleIndicator2(me.relex.circleindicator.CircleIndicator2) SampleRecyclerAdapter(me.relex.circleindicator.sample.SampleRecyclerAdapter) PagerSnapHelper(androidx.recyclerview.widget.PagerSnapHelper) RecyclerView(androidx.recyclerview.widget.RecyclerView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 2 with PagerSnapHelper

use of androidx.recyclerview.widget.PagerSnapHelper 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)

Example 3 with PagerSnapHelper

use of androidx.recyclerview.widget.PagerSnapHelper in project AndroidPicker by gzu-liyujiang.

the class CalendarView method enablePagerSnap.

public void enablePagerSnap() {
    bodyView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
    new PagerSnapHelper().attachToRecyclerView(bodyView);
}
Also used : PagerSnapHelper(androidx.recyclerview.widget.PagerSnapHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Aggregations

LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)3 PagerSnapHelper (androidx.recyclerview.widget.PagerSnapHelper)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 View (android.view.View)1 CircleIndicator2 (me.relex.circleindicator.CircleIndicator2)1 LoopRecyclerAdapter (me.relex.circleindicator.sample.LoopRecyclerAdapter)1 SampleRecyclerAdapter (me.relex.circleindicator.sample.SampleRecyclerAdapter)1 SnapPageScrollListener (me.relex.recyclerpager.SnapPageScrollListener)1