导入依赖
compile 'com.github.lsjwzh.RecyclerViewPager:lib:v1.1.2'
导入库(repositories下)
maven { url "https://jitpack.io" }
控件
<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager
android:id="@+id/rv"
android:layout_width="match_parent"
android:padding="50dp"
android:layout_height="150dp"
app:rvp_triggerOffset="0.1"
app:rvp_singlePageFling="true"
android:clipToPadding="false"
/>
使用
final RecyclerViewPager rv = findViewById(R.id.rv);
MyAdapter my=new MyAdapter(this,list);
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
rv.setAdapter(my);
rv.setHasFixedSize(true);
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int childCount = rv.getChildCount();
int width = rv.getChildAt(0).getWidth();
int padding = (rv.getWidth() - width) / 2;
// mCountText.setText("Count: " + childCount);
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);
}
}
}
});
rv.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 (rv.getChildCount() < 4) {
if (rv.getChildAt(1) != null) {
if (rv.getCurrentPosition() == 0) {
View v1 = rv.getChildAt(1);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
} else {
View v1 = rv.getChildAt(0);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
}
}
} else {
if (rv.getChildAt(0) != null) {
View v0 = rv.getChildAt(0);
v0.setScaleY(0.9f);
v0.setScaleX(0.9f);
}
if (rv.getChildAt(2) != null) {
View v2 = rv.getChildAt(2);
v2.setScaleY(0.9f);
v2.setScaleX(0.9f);
}
}
}
});
compile 'com.github.lsjwzh.RecyclerViewPager:lib:v1.1.2'
导入库(repositories下)
maven { url "https://jitpack.io" }
控件
<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager
android:id="@+id/rv"
android:layout_width="match_parent"
android:padding="50dp"
android:layout_height="150dp"
app:rvp_triggerOffset="0.1"
app:rvp_singlePageFling="true"
android:clipToPadding="false"
/>
使用
final RecyclerViewPager rv = findViewById(R.id.rv);
MyAdapter my=new MyAdapter(this,list);
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
rv.setAdapter(my);
rv.setHasFixedSize(true);
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int childCount = rv.getChildCount();
int width = rv.getChildAt(0).getWidth();
int padding = (rv.getWidth() - width) / 2;
// mCountText.setText("Count: " + childCount);
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);
}
}
}
});
rv.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 (rv.getChildCount() < 4) {
if (rv.getChildAt(1) != null) {
if (rv.getCurrentPosition() == 0) {
View v1 = rv.getChildAt(1);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
} else {
View v1 = rv.getChildAt(0);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
}
}
} else {
if (rv.getChildAt(0) != null) {
View v0 = rv.getChildAt(0);
v0.setScaleY(0.9f);
v0.setScaleX(0.9f);
}
if (rv.getChildAt(2) != null) {
View v2 = rv.getChildAt(2);
v2.setScaleY(0.9f);
v2.setScaleX(0.9f);
}
}
}
});