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

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