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();
});
}
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));
}
}
});
}
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);
}
Aggregations