androidx.recyclerview.widget.PagerSnapHelper Java Examples

The following examples show how to use androidx.recyclerview.widget.PagerSnapHelper. 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: PagerSnapActivity.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
@Override
    public void initValues() {
        super.initValues();


        List<ItemBean> lists = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            lists.add(ItemBean.newItemBeanPager());
        }

        // 初始化布局管理器、适配器
        pagerSnapAdapter = new PagerSnapAdapter(lists);
        vid_bvr_recy.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
//        vid_bvr_recy.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        vid_bvr_recy.setAdapter(pagerSnapAdapter);

        PagerSnapHelper helper = new PagerSnapHelper();
        helper.attachToRecyclerView(vid_bvr_recy);
    }
 
Example #2
Source File: PagerSnapMAXActivity.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
@Override
    public void initValues() {
        super.initValues();


        List<ItemBean> lists = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            lists.add(ItemBean.newItemBeanPager());
        }

        // 初始化布局管理器、适配器
        pagerSnapAdapter = new PagerSnapMAXAdapter(this, lists);
        vid_bvr_recy.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
//        vid_bvr_recy.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        vid_bvr_recy.setAdapter(pagerSnapAdapter);

        PagerSnapHelper helper = new PagerSnapHelper();
        helper.attachToRecyclerView(vid_bvr_recy);


        int size = lists.size();
        // 滑动到中间 ( 无滑动过程 )
        ((LinearLayoutManager) vid_bvr_recy.getLayoutManager()).scrollToPositionWithOffset(size * 100 - 1, 10);
        // 复位到中间
        ListViewUtils.smoothScrollToPosition(vid_bvr_recy, size * 100 + 1);
    }
 
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: 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 #5
Source File: NotePreviewActivity.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
protected void initView() {
    mDelete.setColorFilter(Color.WHITE);
    mPagerSnapHelper = new PagerSnapHelper();
    mImageList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    // 重置
    mImageList.setOnFlingListener(null);
    mPagerSnapHelper.attachToRecyclerView(mImageList);
    mImageList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            scrollImage(getCurrentPosition(recyclerView));
        }
    });
}
 
Example #6
Source File: RecyclerViewFragment.java    From CircleIndicator with Apache License 2.0 5 votes vote down vote up
@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();
    });
}
 
Example #7
Source File: PreviewActivity.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
private void initRecyclerView() {
    rvPhotos = findViewById(R.id.rv_photos);
    adapter = new PreviewPhotosAdapter(this, photos, this);
    lm = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    rvPhotos.setLayoutManager(lm);
    rvPhotos.setAdapter(adapter);
    rvPhotos.scrollToPosition(index);
    toggleSelector();
    snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(rvPhotos);
    rvPhotos.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            View view = snapHelper.findSnapView(lm);
            if (view == null) {
                return;
            }
            int position = lm.getPosition(view);
            if (lastPosition == position) {
                return;
            }
            lastPosition = position;
            previewFragment.setSelectedPosition(-1);
            tvNumber.setText(getString(R.string.preview_current_number_easy_photos, lastPosition + 1, photos.size()));
            toggleSelector();
            PreviewPhotosAdapter.PreviewPhotosViewHolder holder = (PreviewPhotosAdapter.PreviewPhotosViewHolder) rvPhotos.getChildViewHolder(view);
            if (holder == null) {
                return;
            }
            if (holder.ivPhoto != null && holder.ivPhoto.getScale() != 1f) {
                holder.ivPhoto.setScale(1f, true);
            }
            if (holder.ivBigPhoto != null && holder.ivBigPhoto.getScale() != 1f) {
                holder.ivBigPhoto.resetScaleAndCenter();
            }
        }
    });
    tvNumber.setText(getString(R.string.preview_current_number_easy_photos, index + 1,
            photos.size()));
}
 
Example #8
Source File: PagerLayoutManager.java    From videoplay with Apache License 2.0 4 votes vote down vote up
PagerLayoutManager(Context context) {
    super(context);
    mPagerSnapHelper = new PagerSnapHelper();
}
 
Example #9
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 #10
Source File: ScrollCompat.java    From SmoothRefreshLayout with MIT License 4 votes vote down vote up
public static boolean scrollCompat(View view, float deltaY) {
    if (view != null) {
        if (view instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) view;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                absListView.scrollListBy((int) deltaY);
            } else if (absListView instanceof ListView) {
                // {@link android.support.v4.widget.ListViewCompat#scrollListBy(ListView,
                // int)}
                final ListView listView = (ListView) absListView;
                final int firstPosition = listView.getFirstVisiblePosition();
                if (firstPosition == ListView.INVALID_POSITION) {
                    return false;
                }
                final View firstView = listView.getChildAt(0);
                if (firstView == null) {
                    return false;
                }
                final int newTop = (int) (firstView.getTop() - deltaY);
                listView.setSelectionFromTop(firstPosition, newTop);
            } else {
                absListView.smoothScrollBy((int) deltaY, 0);
            }
            return true;
        } else if (view instanceof WebView
                || view instanceof ScrollView
                || view instanceof NestedScrollView) {
            view.scrollBy(0, (int) deltaY);
            return true;
        } else if (ViewCatcherUtil.isRecyclerView(view)) {
            RecyclerView recyclerView = (RecyclerView) view;
            if (!(recyclerView.getOnFlingListener() instanceof PagerSnapHelper)) {
                // Fix the problem of adding new data to RecyclerView while in Fling state,
                // the new items will continue to Fling
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING) {
                    recyclerView.stopScroll();
                }
                view.scrollBy(0, (int) deltaY);
            }
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: NoteAdapter.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
@Override
protected void convert(@NonNull BaseViewHolder holder, @NonNull NoteEntity item) {
    TextView date = holder.getTextView(R.id.item_note_date);
    ImageView menu = holder.getImageView(R.id.item_note_menu);

    TextView content = holder.getTextView(R.id.item_note_content);
    date.setText(DateUtil.formatDate(item.getId(), DateUtil.PATTERN_ONE));
    menu.setColorFilter(Color.parseColor("#333333"));
    content.setText(item.getText());
    String videoUrl = item.getVideoUrl();
    ViewGroup typeVideo = holder.getView(R.id.item_note_type_video);
    ViewGroup typeImage = holder.getView(R.id.item_note_type_image);
    if (videoUrl == null) {
        typeVideo.setVisibility(View.GONE);
        typeImage.setVisibility(View.VISIBLE);
        RecyclerView images = holder.getView(R.id.item_note_images, RecyclerView.class);
        TextView indicator = holder.getTextView(R.id.item_note_images_indicator);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(ScreenUtil.dp2px(mContext, 15));
        drawable.setColor(Color.parseColor("#7FB2B2B2"));
        indicator.setBackground(drawable);
        // 图片显示
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        images.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        images.setAdapter(new ImageAdapter(item.getImages()));
        // 重置
        images.setOnFlingListener(null);
        pagerSnapHelper.attachToRecyclerView(images);
        images.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //noinspection ConstantConditions
                final int i = layoutManager.getPosition(pagerSnapHelper.findSnapView(layoutManager));
                scrollImage(i, holder);
            }
        });
        scrollImage(0, holder);
    } else {
        typeVideo.setVisibility(View.VISIBLE);
        typeImage.setVisibility(View.GONE);
        ImageView thumbnail = holder.getView(R.id.item_note_video_thumbnail);
        ImageLoader.load(mContext, videoUrl, thumbnail);
        thumbnail.setOnClickListener(v -> {
            ViewGroup parent = (ViewGroup) mVideoPlayer.getParent();
            if (parent != null) {
                parent.removeView(mVideoPlayer);
                resetRecycler();
            }
            NeihanVideoPlayerController controller = new NeihanVideoPlayerController(mContext);
            controller.setTitle("");
            mVideoPlayer.setUp(videoUrl, null);
            mVideoPlayer.setController(controller);
            mVideoPlayer.start();
            typeVideo.addView(mVideoPlayer);
        });
    }
    menu.setOnClickListener(v -> {
        if (mOnMenuClickListener != null) {
            mOnMenuClickListener.onClick(mData.get(holder.getLayoutPosition()));
        }
    });
}