-
RecyclerViewPager-一个基于RecyclerView实现的ViewPager,支持类似于gallary的fling操作.zip下载
资源介绍
介绍:一个基于RecyclerView实现的ViewPager,支持类似于gallary的fling操作。点击上面"下载源码" 按钮,可以下载完整的demo。其中recyclerview是android-support中的依赖项目,RecyclerViewPager是主项目。运行效果:使用说明:继承自 RecyclerView.自定义 fling factor.自定义 paging trigger.支持 水平个垂直方向.支持 FragmentViewPager (api 12 )gradlecompile('com.lsjwzh:recyclerviewpager:1.0.2')xml: java:final RecyclerViewPager mRecyclerView = (RecyclerViewPager) this.findViewById(R.id.recyclerViewPager);
LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);//LinearLayoutManager.HORIZONTAL 设置水平滚动
mRecyclerView.setLayoutManager(layout);
//set adapter
mRecyclerView.setAdapter(new MyRecyclerAdapter());static class MyRecyclerAdapter extends RecyclerView.Adapter {
private List items = new ArrayList();
public MyRecyclerAdapter(){
//创建4个页面
for(int i = 1; i<5; i ){
items.add("页面" i);
}
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String item = items.get(position);
TextView msgTv = (TextView)holder.itemView.findViewById(R.id.msg);
msgTv.setText(item);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(inflate == null)
inflate = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = (LinearLayout)(inflate.inflate(R.layout.item, null));
return new ViewHolder(v);
}
}如果你想增加一些切换的特效:mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {//监听滚动,i为每次水平滚动的距离, i2为每次垂直滚动的距离
// mPositionText.setText("First: " mRecyclerView.getFirstVisiblePosition());
System.out.println("MainaActiv: i=" i " i2=" i2);
//以下代码的作用是,使页面切换产生立体的效果
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;
for (int j = 0; j < childCount; j ) {
View v = recyclerView.getChildAt(j);
//往左 从 padding 到 -(v.getWidth()-padding) 的过程中,由大到小
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);
v.setScaleX(1 - rate * 0.1f);
} else {
//往右 从 padding 到 recyclerView.getWidth()-padding 的过程中,由大到小
if (v.getLeft() <= recyclerView.getWidth() - padding) {
rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
}
v.setScaleY(0.9f rate * 0.1f);
v.setScaleX(0.9f rate * 0.1f);
}
}
}
});
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) {
if(mRecyclerView.getCurrentPosition()==0) {
View v1 = mRecyclerView.getChildAt(1);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
} else {
View v1 = mRecyclerView.getChildAt(0);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
}
}
} else {
if (mRecyclerView.getChildAt(0) != null) {
View v0 = mRecyclerView.getChildAt(0);
v0.setScaleY(0.9f);
v0.setScaleX(0.9f);
}
if (mRecyclerView.getChildAt(2) != null) {
View v2 = mRecyclerView.getChildAt(2);
v2.setScaleY(0.9f);
v2.setScaleX(0.9f);
}
}
}
});
}