想recyclerview快速实现卡片滑动,需要一个重要帮助类,LinearSnapHelper类,这个类是Android24.2.0之后引入的帮助类,是SnapHelper抽象类的子类
具有让recyclerview中的item居中的功能,实现也相当简单
LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recycler);
相比于通过viewpager然后监听viewpager的滑动,实现要简单一点
设置linearsnahelper只能让item选为居中,没有卡片滑动效果,接下来如果实现?
第一步:item的宽度,设置左右margin边距,让item左右有空白间隙;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_recyclerview_item,parent,false);
//这里设置view的宽度
RecyclerScaleUtils.onCreateViewHolder(parent,view,ScreenUtil.dip2px(parent.getContext(),30f));
return new MyViewHolder(view);
}
RecyclerScaleUtils类中的onCreateViewHolder方法,就是设置item的宽度
public static void onCreateViewHolder(ViewGroup parent, View itemView,int margin) {
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams();
if(margin <= 0){
margin = mDefaultMargin;
}
lp.width = parent.getWidth() - 2*margin;
itemView.setLayoutParams(lp);
//这里是处理最左边和最右边的宽度
public static void onBindViewHolder(View itemView, final int position, int itemCount,int margin) {
int leftMarin = 0;
int rightMarin = 0;
int topMarin = 0;
int bottomMarin = 0;
if(position == 0){
leftMarin = margin;
rightMarin = 0;
}else if(position == (itemCount-1)){
leftMarin = 0;
rightMarin = margin;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) itemView.getLayoutParams();
if (lp.leftMargin != leftMarin || lp.topMargin != topMarin || lp.rightMargin != rightMarin || lp.bottomMargin != bottomMarin) {
lp.setMargins(leftMarin, topMarin, rightMarin, bottomMarin);
itemView.setLayoutParams(lp);
}
}
第二步:就是在左右滑动的时候,当前item,和左右item都有相应的缩放效果;如左滑,当前item缩小,右边item,放大(这里可以用动画)
public void attachToRecyclerView(RecyclerView recyclerView,int margin){
if(recyclerView == null){
return;
}
mRecycler = recyclerView;
if(margin <= 0){
mMargin = mDefaultMargin;
}else {
mMargin = margin;
}
initView();
final CustomLinearSnapHelper helper = new CustomLinearSnapHelper();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == RecyclerView.SCROLL_STATE_IDLE){
if(mDistances == 0 || mDistances == (mItemCount*mItemwidth)){
helper.mStateIdle = true;
}else{
helper.mStateIdle = false;
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(recyclerView.getLayoutManager().getLayoutDirection() == LinearLayoutManager.HORIZONTAL){
mDistances += dx;
getCurrentPosition();
setItemScale();
}
}
});
helper.attachToRecyclerView(recyclerView);
}
//这里获取item的宽与countitem数
protected void initView( ){
mRecycler.post(new Runnable() {
@Override
public void run() {
mItemCount = mRecycler.getAdapter().getItemCount();
mItemwidth = mRecycler.getWidth() - 2 * mMargin;
mRecycler.smoothScrollToPosition(mCurrentPosition);
setItemScale();
}
});
}
//item在滑动时,进行动画的缩放
public void setItemScale(){
View leftView = null;
View rightView = null;
if(mCurrentPosition > 0){
leftView = mRecycler.getLayoutManager().findViewByPosition(mCurrentPosition - 1);
}
View currentView = mRecycler.getLayoutManager().findViewByPosition(mCurrentPosition);
if(mCurrentPosition < (mItemCount - 1)){
rightView = mRecycler.getLayoutManager().findViewByPosition(mCurrentPosition +1);
}
//滑动百分比,左右的都是放大,中间缩小
float percent = Math.abs((mDistances - mCurrentPosition * mItemwidth*1.0f)/mItemwidth);
if(leftView != null){
//这里是缩小原来大小的0.8-1.0 左右0.8,中间1.0 0.8+(percent*0.2)
leftView.setScaleY(0.8f+(percent*0.2f));
}
if(currentView != null){
currentView.setScaleY(1-0.2f*percent);
}
if(rightView != null){
rightView.setScaleY(0.8f+(percent*0.2f));
}
}
protected void getCurrentPosition(){
if(mItemwidth <= 0) return;
boolean change = false;
if (Math.abs(mDistances - mCurrentPosition * mItemwidth) >= mItemwidth) {
change = true;
}
if (change) { //这里是从0开始的
mCurrentPosition = mDistances / mItemwidth;
}
}
public static void setItemPosition(int position){
this.mCurrentPosition = position;
this.mDistances = position * mItemwidth;
} 这里自定义了linearsanphelper,因为在第一个item和最后一个item设置了marginleft与rightmargin,导致这两个item的 中心位置滑动的偏移,此时LinearSnapHelper通过调用recyclerview的onscrolllistener获取不动具体的中心点,而误以为 客户一直在滑动,
public class CustomLinearSnapHelper extends LinearSnapHelper {
public static boolean mStateIdle = false;
/**
// * int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, snapView);
// if (snapDistance[0] != 0 || snapDistance[1] != 0) {
// mRecyclerView.smoothScrollBy(snapDistance[0], snapDistance[1]);
// }
// 这个方法返回的数组值,是让recyclerview移动并居中显示的,如果是第一个或者最后一个位置,
// 因无法居中而调用recyclerview的OnScrollListener监听
// */
@Override
public int[] calculateScrollDistance(int velocityX, int velocityY) {
if(mStateIdle){
return new int[2];
}else{
int[] ints = super.calculateScrollDistance(velocityX, velocityY);
for (int i:ints){
Log.e("tests","==i=="+i);
}
return super.calculateScrollDistance(velocityX, velocityY);
}
}
}
recyclerView在设置适配器之后
RecyclerScaleUtils utils = new RecyclerScaleUtils();
utils.attachToRecyclerView(recycler,ScreenUtil.dip2px(this,30f));
demo链接http://download.csdn.net/detail/change987654321/9689690