androidx.recyclerview.widget.SnapHelper Java Examples

The following examples show how to use androidx.recyclerview.widget.SnapHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FavoriteFragment.java    From memorize with MIT License 6 votes vote down vote up
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_favorite, container, false);
        mRecyclerView = root.findViewById(R.id.fav_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(AppMain.getContext(), LinearLayoutManager.VERTICAL, false));
        SnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(mAdapter);

        swipeRefreshLayout = (SwipeRefreshLayout) root.findViewById(R.id.fav_swiper);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.loadWords(false);
            }
        });
//        animation = AnimationUtils.loadAnimation(AppMain.getContext(), R.anim.card_in);

        return root;
    }
 
Example #2
Source File: SimilarAppcAppsViewHolder.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
public SimilarAppcAppsViewHolder(View view, DecimalFormat oneDecimalFormat,
    PublishSubject<SimilarAppClickEvent> similarAppClick) {
  super(view);
  this.oneDecimalFormat = oneDecimalFormat;
  this.similarAppClick = similarAppClick;

  similarAppcApps = view.findViewById(R.id.similar_appc_list);
  similarAppcApps.setNestedScrollingEnabled(false);
  HorizontalHeaderItemDecoration similarAppcHeaderItemDecoration =
      new HorizontalHeaderItemDecoration(view.getContext(), similarAppcApps,
          R.layout.appview_appc_similar_header,
          AptoideUtils.ScreenU.getPixelsForDip(112, view.getResources()),
          AptoideUtils.ScreenU.getPixelsForDip(5, view.getResources()));
  similarAppcApps.addItemDecoration(similarAppcHeaderItemDecoration);

  LinearLayoutManager similarAppcLayout =
      new LinearLayoutManager(view.getContext(), RecyclerView.HORIZONTAL, false);

  similarAppcApps.setLayoutManager(similarAppcLayout);
  SnapHelper similarSnap = new SnapToStartHelper();
  similarSnap.attachToRecyclerView(similarAppcApps);

  similarAppcApps.setAdapter(getSimilarAdapter());
}
 
Example #3
Source File: SnapUtil.java    From litho with Apache License 2.0 6 votes vote down vote up
@Nullable
public static SnapHelper getSnapHelper(
    @SnapMode int snapMode, int deltaJumpThreshold, int startSnapFlingOffset) {
  switch (snapMode) {
    case SNAP_TO_CENTER:
      return new PagerSnapHelper();
    case SNAP_TO_START:
      return new StartSnapHelper(startSnapFlingOffset);
    case SNAP_TO_CENTER_CHILD:
      return new LinearSnapHelper();
    case SNAP_TO_CENTER_CHILD_WITH_CUSTOM_SPEED:
      return new CustomSpeedLinearSnapHelper(deltaJumpThreshold);
    case SNAP_TO_END:
    case SNAP_NONE:
    default:
      return null;
  }
}
 
Example #4
Source File: DayPickerView.java    From cathode with Apache License 2.0 5 votes vote down vote up
public void init(Context context) {
  setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

  inflate(context, R.layout.day_picker_view, this);

  recyclerView = (RecyclerView) findViewById(android.R.id.list);
  layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
  recyclerView.setLayoutManager(layoutManager);
  recyclerView.setAdapter(mAdapter);
  SnapHelper snapHelper = new LinearSnapHelper();
  snapHelper.attachToRecyclerView(recyclerView);
}
 
Example #5
Source File: SnapOnScrollListener.java    From Android-Application with GNU General Public License v3.0 5 votes vote down vote up
int getSnapPosition(SnapHelper sh, RecyclerView recyclerView) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if(layoutManager == null) return RecyclerView.NO_POSITION;
    View snapView = snapHelper.findSnapView(layoutManager);
    if(snapView == null) return RecyclerView.NO_POSITION;
    return layoutManager.getPosition(snapView);
}
 
Example #6
Source File: CircleIndicator2.java    From CircleIndicator with Apache License 2.0 5 votes vote down vote up
public void attachToRecyclerView(@NonNull RecyclerView recyclerView,
        @NonNull SnapHelper snapHelper) {
    mRecyclerView = recyclerView;
    mSnapHelper = snapHelper;
    mLastPosition = -1;
    createIndicators();
    recyclerView.removeOnScrollListener(mInternalOnScrollListener);
    recyclerView.addOnScrollListener(mInternalOnScrollListener);
}
 
Example #7
Source File: RecyclerSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUnmount
static void onUnmount(
    ComponentContext context,
    SectionsRecyclerView sectionsRecycler,
    @Prop Binder<RecyclerView> binder,
    @Prop(optional = true) RecyclerView.ItemDecoration itemDecoration,
    @Prop(optional = true, resType = ResType.COLOR) @Nullable
        Integer refreshProgressBarBackgroundColor,
    @Prop(optional = true) SnapHelper snapHelper) {
  final RecyclerView recyclerView = sectionsRecycler.getRecyclerView();

  if (recyclerView == null) {
    throw new IllegalStateException(
        "RecyclerView not found, it should not be removed from SwipeRefreshLayout "
            + "before unmounting");
  }

  recyclerView.setId(RecyclerSpec.recyclerViewId);

  if (refreshProgressBarBackgroundColor != null) {
    sectionsRecycler.setProgressBackgroundColorSchemeColor(
        DEFAULT_REFRESH_SPINNER_BACKGROUND_COLOR);
  }

  if (itemDecoration != null) {
    recyclerView.removeItemDecoration(itemDecoration);
  }

  binder.unmount(recyclerView);

  if (snapHelper != null) {
    snapHelper.attachToRecyclerView(null);
  }

  sectionsRecycler.resetItemAnimator();
}
 
Example #8
Source File: RecyclerSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecyclerSpecOnBind() {
  EventHandler refreshHandler = mock(EventHandler.class);
  Binder<RecyclerView> binder = mock(Binder.class);

  SnapHelper snapHelper = mock(SnapHelper.class);

  final int size = 3;
  List<RecyclerView.OnScrollListener> scrollListeners = createListOfScrollListeners(size);

  LithoRecylerView.TouchInterceptor touchInterceptor =
      mock(LithoRecylerView.TouchInterceptor.class);

  RecyclerSpec.onBind(
      mComponentContext,
      mSectionsRecyclerView,
      binder,
      null,
      scrollListeners,
      snapHelper,
      true,
      touchInterceptor,
      refreshHandler);

  assertThat(mSectionsRecyclerView.isEnabled()).isTrue();

  assertThat(mSectionsRecyclerView.getRecyclerView()).isSameAs(mRecyclerView);

  verifyAddOnScrollListenerWasCalledNTimes(mRecyclerView, size);
  assertThat(mRecyclerView.getTouchInterceptor()).isSameAs(touchInterceptor);

  verify(binder).bind(mRecyclerView);
  assertThat(mRecyclerView.isLayoutRequested()).isTrue();
  assertThat(mSectionsRecyclerView.hasBeenDetachedFromWindow()).isFalse();
  verify(snapHelper).attachToRecyclerView(mRecyclerView);
}
 
Example #9
Source File: CarouselTest.java    From epoxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testOverrideGlobalSnapHelper() {
  Carousel.setDefaultGlobalSnapHelperFactory(new SnapHelperFactory() {
    @NonNull
    @Override
    public SnapHelper buildSnapHelper(Context context) {
      return new LinearSnapHelper();
    }
  });
}
 
Example #10
Source File: SampleActivity.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private void initView() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.openDrawer(GravityCompat.START);
    drawer.clearAnimation();
    drawer.setAnimation(null);
    drawer.setLayoutAnimation(null);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.clearAnimation();
    navigationView.setAnimation(null);
    navigationView.setLayoutAnimation(null);

    bitmapView = findViewById(R.id.iv_image);
    bitmapView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bitmapView.setVisibility(View.GONE);
        }
    });

    rvImage = (RecyclerView) findViewById(R.id.rv_image);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    adapter = new MainAdapter(this, selectedPhotoList);
    rvImage.setLayoutManager(linearLayoutManager);
    rvImage.setAdapter(adapter);
    SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(rvImage);
}
 
Example #11
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
void setSnapHelper(SnapHelper snapHelper) {
    if (mRecyclerView == null) {
        return;
    }
    mSnapHelper = snapHelper;
    mRecyclerView.setOnFlingListener(null);
    mSnapHelper.attachToRecyclerView(mRecyclerView);
}
 
Example #12
Source File: GridRecyclerConfiguration.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public @Nullable SnapHelper getSnapHelper() {
  return SnapUtil.getSnapHelper(mSnapMode, mDeltaJumpThreshold, mStartSnapFlingOffset);
}
 
Example #13
Source File: CardSlideView.java    From CardSlideView with Apache License 2.0 4 votes vote down vote up
public void setSnapHelper(SnapHelper snapHelper) {
    mLayoutManager.setSnapHelper(snapHelper);
}
 
Example #14
Source File: StaggeredGridRecyclerConfiguration.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public @Nullable SnapHelper getSnapHelper() {
  return null;
}
 
Example #15
Source File: RecyclerConfiguration.java    From litho with Apache License 2.0 4 votes vote down vote up
@Nullable
SnapHelper getSnapHelper();
 
Example #16
Source File: ListRecyclerConfiguration.java    From litho with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public SnapHelper getSnapHelper() {
  return SnapUtil.getSnapHelper(mSnapMode, mDeltaJumpThreshold, mStartSnapFlingOffset);
}
 
Example #17
Source File: RecyclerSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnBind
protected static void onBind(
    ComponentContext context,
    SectionsRecyclerView sectionsRecycler,
    @Prop Binder<RecyclerView> binder,
    @Prop(optional = true) final RecyclerEventsController recyclerEventsController,
    @Prop(optional = true, varArg = "onScrollListener") List<OnScrollListener> onScrollListeners,
    @Prop(optional = true) SnapHelper snapHelper,
    @Prop(optional = true) boolean pullToRefresh,
    @Prop(optional = true) LithoRecylerView.TouchInterceptor touchInterceptor,
    @Nullable @Prop(optional = true) final EventHandler refreshHandler) {

  // contentDescription should be set on the recyclerView itself, and not the sectionsRecycler.
  sectionsRecycler.setContentDescription(null);

  sectionsRecycler.setEnabled(pullToRefresh && refreshHandler != null);
  sectionsRecycler.setOnRefreshListener(
      new OnRefreshListener() {
        @Override
        public void onRefresh() {
          Recycler.dispatchPTRRefreshEvent(refreshHandler);
        }
      });

  final LithoRecylerView recyclerView = (LithoRecylerView) sectionsRecycler.getRecyclerView();

  if (recyclerView == null) {
    throw new IllegalStateException(
        "RecyclerView not found, it should not be removed from SwipeRefreshLayout "
            + "before unmounting");
  }

  if (onScrollListeners != null) {
    for (OnScrollListener onScrollListener : onScrollListeners) {
      recyclerView.addOnScrollListener(onScrollListener);
    }
  }

  if (touchInterceptor != null) {
    recyclerView.setTouchInterceptor(touchInterceptor);
  }

  // We cannot detach the snap helper in unbind, so it may be possible for it to get
  // attached twice which causes SnapHelper to raise an exception.
  if (snapHelper != null && recyclerView.getOnFlingListener() == null) {
    snapHelper.attachToRecyclerView(recyclerView);
  }

  binder.bind(recyclerView);

  if (recyclerEventsController != null) {
    recyclerEventsController.setSectionsRecyclerView(sectionsRecycler);
  }

  if (sectionsRecycler.hasBeenDetachedFromWindow()) {
    recyclerView.requestLayout();
    sectionsRecycler.setHasBeenDetachedFromWindow(false);
  }
}
 
Example #18
Source File: MainActivity.java    From ScrollingPagerIndicator with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int screenWidth = getScreenWidth();

    // Setup ViewPager with indicator
    ViewPager pager = findViewById(R.id.pager);
    DemoPagerAdapter pagerAdapter = new DemoPagerAdapter(8);
    pager.setAdapter(pagerAdapter);

    ScrollingPagerIndicator pagerIndicator = findViewById(R.id.pager_indicator);
    pagerIndicator.attachToPager(pager);

    // Setup ViewPager2 with indicator
    ViewPager2 pager2 = findViewById(R.id.pager2);
    DemoRecyclerViewAdapter pagerAdapter2 = new DemoRecyclerViewAdapter(8, ViewGroup.LayoutParams.MATCH_PARENT);
    pager2.setAdapter(pagerAdapter2);

    ScrollingPagerIndicator pagerIndicator2 = findViewById(R.id.pager_indicator2);
    pagerIndicator2.attachToPager(pager2);

    // Setup RecyclerView with indicator
    // One page will occupy 1/3 of screen width
    RecyclerView recyclerView = findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    DemoRecyclerViewAdapter recyclerAdapter = new DemoRecyclerViewAdapter(8, screenWidth / 3);
    recyclerView.setAdapter(recyclerAdapter);

    recyclerView.setPadding(screenWidth / 3, 0, screenWidth / 3, 0);

    ScrollingPagerIndicator recyclerIndicator = findViewById(R.id.recycler_indicator);
    // Consider page in the middle current
    recyclerIndicator.attachToRecyclerView(recyclerView);

    SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);

    // Some controls
    NumberPicker pageCountPicker = findViewById(R.id.page_number_picker);
    pageCountPicker.setMaxValue(99);
    pageCountPicker.setMinValue(0);
    pageCountPicker.setValue(pagerAdapter.getCount());

    NumberPicker visibleDotCountPicker = findViewById(R.id.visible_dot_number_picker);
    visibleDotCountPicker.setMinValue(3);
    visibleDotCountPicker.setMaxValue(11);
    visibleDotCountPicker.setValue(pagerIndicator.getVisibleDotCount());

    visibleDotCountPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
        if (newVal % 2 == 0) {
            Toast.makeText(this, "Visible dot count must be odd number", Toast.LENGTH_SHORT).show();
            return;
        }
        pagerIndicator.setVisibleDotCount(newVal);
        recyclerIndicator.setVisibleDotCount(newVal);
    });

    pageCountPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
        if (pager.getCurrentItem() >= newVal - 1) {
            pager.setCurrentItem(newVal - 1, false);
        }
        pagerAdapter.setCount(newVal);
        recyclerAdapter.setCount(newVal);
    });
}
 
Example #19
Source File: SnapOnScrollListener.java    From Android-Application with GNU General Public License v3.0 4 votes vote down vote up
SnapOnScrollListener(SnapHelper sh, Behavior b, OnSnapPositionChangeListener cl){
    snapHelper = sh;
    behavior = b;
    listener = cl;
}
 
Example #20
Source File: Carousel.java    From epoxy with Apache License 2.0 2 votes vote down vote up
/**
 * Create and return a new instance of a {@link androidx.recyclerview.widget.SnapHelper} for use
 * with a Carousel.
 */
@NonNull
public abstract SnapHelper buildSnapHelper(Context context);