@Override protected View onCreateView() { View root = LayoutInflater.from(getContext()).inflate(R.layout.fragment_pagerlayoutmanager, null); ButterKnife.bind(this, root); mQDItemDescription = QDDataManager.getInstance().getDescription(this.getClass()); initTopBar(); mRecyclerView = new RecyclerView(getContext()); mPagerLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); mRecyclerView.setLayoutManager(mPagerLayoutManager); mRecyclerViewAdapter = new QDRecyclerViewAdapter(); mRecyclerViewAdapter.setItemCount(10); mRecyclerView.setAdapter(mRecyclerViewAdapter); mPagerWrap.addView(mRecyclerView); // PagerSnapHelper每次只能滚动一个item;用LinearSnapHelper则可以一次滚动多个,并最终保证定位 // mSnapHelper = new LinearSnapHelper(); mSnapHelper = new PagerSnapHelper(); mSnapHelper.attachToRecyclerView(mRecyclerView); return root; }
private void initPagerView() { pagerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); pagerView.setHasFixedSize(true); pagerView.setAdapter(pagerAdapter); new PagerSnapHelper().attachToRecyclerView(pagerView); pagerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) { int index = ((LinearLayoutManager) pagerView.getLayoutManager()).findFirstVisibleItemPosition(); View pagerItemView = ImageGalleryView.this.pagerView.getLayoutManager().findViewByPosition(index); index = ((LinearLayoutManager) pagerIndicatorView.getLayoutManager()).findFirstVisibleItemPosition(); View pagerIndicatorItemView = pagerIndicatorView.getLayoutManager().findViewByPosition(index); float offset = 1f * dx / (pagerView.getAdapter().getItemCount() * pagerItemView.getWidth()); pagerIndicatorFrameView.setTranslationX(pagerIndicatorFrameView.getTranslationX() + offset * pagerIndicatorItemView.getWidth() * pagerIndicatorView.getAdapter().getItemCount()); } }); }
@Override public void onCreateRecyclerView(Bundle savedInstanceState) { int backgroundColor = 0xFF665F30; Context context = this; recyclerView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; recyclerView.setBackgroundColor(backgroundColor); SimpleViewAdapter adapter = new SimpleViewAdapter(5, R.layout.item_view_pager_page); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)); // add pager behavior PagerSnapHelper snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(recyclerView); // pager indicator recyclerView.addItemDecoration(new LinePagerIndicatorDecoration()); }
/** * transform to ViewPager * * @param recyclerView {@link RecyclerView} * @param orientation the orientation of Linearlayout */ public static void toViewPager(RecyclerView recyclerView , @LinearLayoutCompat.OrientationMode int orientation) { recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext() , orientation, false)); new PagerSnapHelper().attachToRecyclerView(recyclerView); }
private void init(Context context, @Nullable AttributeSet attrs) { if (attrs != null) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MetalRecyclerViewPager, 0, 0); itemMargin = (int) typedArray.getDimension(R.styleable.MetalRecyclerViewPager_itemMargin, 0f); typedArray.recycle(); } setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)); SnapHelper snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(this); }
public RecyclerViewPager(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); setLayoutManager(mLayoutManager); new PagerSnapHelper().attachToRecyclerView(this); final float density = context.getResources().getDisplayMetrics().density; mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density); mTouchSlop = ViewConfiguration.get(context).getScaledPagingTouchSlop(); }
/** * 初始化SnapHelper * * @param helper */ public void initSnapHelper(int helper) { switch (helper) { case GalleryRecyclerView.LinearySnapHelper: mLinearySnapHelper = new LinearSnapHelper(); mLinearySnapHelper.attachToRecyclerView(mGalleryRecyclerView); break; case GalleryRecyclerView.PagerSnapHelper: mPagerSnapHelper = new PagerSnapHelper(); mPagerSnapHelper.attachToRecyclerView(mGalleryRecyclerView); break; } }
@Override public void onBindViewHolder(final ViewHolder holder, int position) { HomeGroup snap = mSnaps.get(position); holder.snapTextView.setText(snap.getText()); if (snap.getGravity() == Gravity.START || snap.getGravity() == Gravity.END) { holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false)); holder.recyclerView.setOnFlingListener(null); new GravitySnapHelper(snap.getGravity(), false, this).attachToRecyclerView(holder.recyclerView); } else if (snap.getGravity() == Gravity.CENTER_HORIZONTAL) { holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder .recyclerView.getContext(), snap.getGravity() == Gravity.CENTER_HORIZONTAL ? LinearLayoutManager.HORIZONTAL : LinearLayoutManager.VERTICAL, false)); holder.recyclerView.setOnFlingListener(null); new LinearSnapHelper().attachToRecyclerView(holder.recyclerView); } else if (snap.getGravity() == Gravity.CENTER) { // Pager snap holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false)); holder.recyclerView.setOnFlingListener(null); new PagerSnapHelper().attachToRecyclerView(holder.recyclerView); } else { // Top / Bottom holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder .recyclerView.getContext())); holder.recyclerView.setOnFlingListener(null); new GravitySnapHelper(snap.getGravity()).attachToRecyclerView(holder.recyclerView); } HomeItemAdapter itemAdapter = new HomeItemAdapter(mContext, snap.getCardSize(), snap.getApps()); holder.recyclerView.setAdapter(itemAdapter); itemAdapter.setOnItemClickListener(new HomeItemAdapter.OnItemClickListener() { @Override public void itemClick(HomeItem item, int Position) { if(mContext instanceof HomeActivity) { if(item.isWebView()) { ((HomeActivity) mContext).attachFragment(WebFragment.newInstance(item.getUrl(), item.getName()), true); } else { ((HomeActivity) mContext).attachFragment(FirebaseListFragment.newInstance(item.getUrl(), item.getExtra()), true); } } } }); }