RecyclerViewPager - 基于RecyclerView实现的ViewPager

jopen 9年前

介绍:

一个基于RecyclerView实现的ViewPager,支持类似于gallary的fling操作

运行效果:

RecyclerViewPager - 基于RecyclerView实现的ViewPager

使用说明:

  1. 继承自 RecyclerView.

  2. 自定义 fling factor.

  3. 自定义 paging trigger.

  4. 支持 水平个垂直方向.

  5. 支持 FragmentViewPager (api 12+)


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);                      }                  }                }          });

项目主页:http://www.open-open.com/lib/view/home/1435026935638