重写后的 RecyclerViewPager 完全继承自RecyclerView
cen
9年前
重写后的 RecyclerViewPager 完全继承自RecyclerView,可以自定义触发翻页的距离,可自定义翻页速度,支持VerticalViewPager,支持Fragment。
Features:
- Extends RecyclerView.
- Custom fling factor.
- Custom paging trigger.
- Support Vertical and Horizontal orientation.
- Support FragmentViewPager (api 12+)
Usage
how to import?
add this into gradle
compile('com.lsjwzh:recyclerviewpager:1.0.2')
xml:
<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="15dp" android:paddingRight="15dp" app:triggerOffset="0.1" android:clipToPadding="false"/>
java api:
RecyclerViewPager mRecyclerView = (RecyclerViewPager) view.findViewById(R.id.list); LinearLayoutManager layout = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false); mRecyclerView.setLayoutManager(layout);//setLayoutManager //set adapter mRecyclerView.setAdapter(new LayoutAdapter(activity, mRecyclerView, mLayoutId)); //set scroll listener //this will show you how to implement a ViewPager like the demo gif mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerViewEx recyclerView, int scrollState) { updateState(scrollState); } @Override public void onScrolled(RecyclerViewEx recyclerView, int i, int i2) { int childCount = mRecyclerView.getChildCount(); int width = mRecyclerView.getChildAt(0).getWidth(); int padding = (mRecyclerView.getWidth() - width)/2; mCountText.setText("Count: " + childCount); for (int j = 0; j < childCount; j++) { View v = recyclerView.getChildAt(j); float rate = 0; if (v.getLeft() <= padding) { if (v.getLeft() >= padding - v.getWidth()) { rate = (padding - v.getLeft()) * 1f / v.getWidth(); } else { rate = 1; } v.setScaleY(1 - rate * 0.1f); } else { if (v.getLeft() <= recyclerView.getWidth() - padding) { rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth(); } v.setScaleY(0.9f + rate * 0.1f); } } } }); // registering addOnLayoutChangeListener aim to setScale at first layout action mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { if(mRecyclerView.getChildCount()<3){ if (mRecyclerView.getChildAt(1) != null) { View v1 = mRecyclerView.getChildAt(1); v1.setScaleY(0.9f); } }else { if (mRecyclerView.getChildAt(0) != null) { View v0 = mRecyclerView.getChildAt(0); v0.setScaleY(0.9f); } if (mRecyclerView.getChildAt(2) != null) { View v2 = mRecyclerView.getChildAt(2); v2.setScaleY(0.9f); } } } });