一个用RecyclerView实现的画廊效果,被选中item居中放大,效果很自然。
依赖:compile 'com.yarolegovich:discrete-scrollview:1.0.3'
使用
1. xml中添加DiscreteScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2. 设置adapterDiscreteScrollView scrollView = findViewById(R.id.picker);
scrollView.setAdapter(new YourAdapterImplementation());
apiscrollView.getCurrentItem(); //returns adapter position of the currently selected item or -1 if adapter is empty.
scrollView.scrollToPosition(int position); //position becomes selected
scrollView.smoothScrollToPosition(int position); //position becomes selected with animated scroll
scrollView.setItemTransitionTimeMillis(int millis); //determines how much time it takes to change the item on fling, settle or smoothScroll
设置transformer 实现移动动画,和viewpager的transformer功能是一样的scrollView.setItemTransformer(transformer);
public interface DiscreteScrollItemTransformer {
/**
* In this method you apply any transform you can imagine (perfomance is not guaranteed).
* @param position is a value inside the interval [-1f..1f]. In idle state:
* |view1| |currentlySelectedView| |view2|
* -view1 and everything to the left is on position -1;
* -currentlySelectedView is on position 0;
* -view2 and everything to the right is on position 1.
*/
void transformItem(View item, float position);
}
监听scrollView.setScrollStateChangeListener(listener);
public interface ScrollStateChangeListener {
void onScrollStart(T currentItemHolder, int adapterPosition); //called when scroll is started, including programatically initiated scroll
void onScrollEnd(T currentItemHolder, int adapterPosition); //called when scroll ends
/**
* Called when scroll is in progress.
* @param scrollPosition is a value inside the interval [-1f..1f], it corresponds to the position of currentlySelectedView.
* In idle state:
* |view1| |currentlySelectedView| |view2|
* -view1 is on position -1;
* -currentlySelectedView is on position 0;
* -view2 is on position 1.
*/
void onScroll(float scrollPosition);
}