Java Code Examples for androidx.recyclerview.widget.SnapHelper#attachToRecyclerView()

The following examples show how to use androidx.recyclerview.widget.SnapHelper#attachToRecyclerView() . 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: 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 4
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 5
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 6
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 7
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);
  }
}