偶然之间发现探探的左右滑动的图片挺好玩,试着去做了下,再到后来,看到许多大神也推出了同样仿探探效果的博客,从头到尾阅读下来,写得通俗易懂,基本上没什么问题。于是,实现仿探探效果的想法再次出现在脑海中。那么,还犹豫什么,趁热来一发吧!就这么愉快地决定了。
RecyclerView 是最佳选择!
RecyclerView 是最佳选择!
RecyclerView 是最佳选择!
重要的话讲三遍!!!
究其原因,第一,RecyclerView 是自带 Item View 回收和重用功能的,就不需要我们考虑这个问题了;第二,RecyclerView 的布局方式是通过设置 LayoutManager 来实现的,这样就充分地把布局和 RecyclerView “解耦”开来了。而 LayoutManager 是可以通过自定义的方式来实现的。这恰恰是我们想要的!!!再说一点,这也正是不选用 ListView 的原因之一。
下面,我们就开始动手了。带你见证奇迹的时刻。
1.创建 CardLayoutManager 并继承自 RecyclerView.LayoutManager 。需要我们自己实现 generateDefaultLayoutParams() 方法:
@Overridepublic RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
}
2.一般情况下,像上面这样写即可。
下面这个方法就是我们的重点了。 onLayoutChildren(final RecyclerView.Recycler recycler, RecyclerView.State state) 方法就是用来实现 Item View 布局的:
@Overridepublic void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
removeAllViews();
detachAndScrapAttachedViews(recycler);
int itemCount = getItemCount();
if(itemCount>CardConfig.DEFAULT_SHOW_ITEM){
for(int position = CardConfig.DEFAULT_SHOW_ITEM;position>=0;position--){
final View view = recycler.getViewForPosition(position);
addView(view);
measureChildWithMargins(view,0,0);
int widthSpace = getWidth() -getDecoratedMeasuredWidth(view);
int heightSpace = getHeight() - getBottomDecorationHeight(view);
layoutDecoratedWithMargins(view,widthSpace/2,heightSpace/2,widthSpace/2+getDecoratedMeasuredWidth(view),heightSpace/2+getBottomDecorationHeight(view));
if(position == CardConfig.DEFAULT_SHOW_ITEM){
view.setScaleX(1-(position-1)*CardConfig.DEFAULT_SCALE);
view.setScaleY(1-(position-1)*CardConfig.DEFAULT_SCALE);
view.setTranslationY((position-1)*view.getMeasuredHeight()/CardConfig.DEFAULT_TRANSLATE_Y);
}else if(position>0){
view.setScaleX(1 - position * CardConfig.DEFAULT_SCALE);
view.setScaleY(1 - position * CardConfig.DEFAULT_SCALE);
view.setTranslationY(position * view.getMeasuredHeight() / CardConfig.DEFAULT_TRANSLATE_Y);
}else{
view.setOnTouchListener(mOnTouchListener);
}
}
}else{
// 当数据源个数小于或等于最大显示数时
for (int position = itemCount - 1; position >= 0; position--) {
final View view = recycler.getViewForPosition(position);
addView(view);
measureChildWithMargins(view, 0, 0);
int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
// recyclerview 布局
layoutDecoratedWithMargins(view, widthSpace / 2, heightSpace / 2,
widthSpace / 2 + getDecoratedMeasuredWidth(view),
heightSpace / 2 + getDecoratedMeasuredHeight(view));
if (position > 0) {
view.setScaleX(1 - position * CardConfig.DEFAULT_SCALE);
view.setScaleY(1 - position * CardConfig.DEFAULT_SCALE);
view.setTranslationY(position * view.getMeasuredHeight() / CardConfig.DEFAULT_TRANSLATE_Y);
} else {
view.setOnTouchListener(mOnTouchListener);
}
}
}
}
3.创建onTouchListener
private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(v);
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mItemTouchHelper.startSwipe(childViewHolder);
}
return false;
}
};
4.完整CardLayoutManager代码如下
public class CardLayoutManager extends RecyclerView.LayoutManager {
private RecyclerView mRecyclerView;
private ItemTouchHelper mItemTouchHelper;
public CardLayoutManager(@NonNull RecyclerView recyclerView, @NonNull ItemTouchHelper itemTouchHelper) {
this.mRecyclerView = recyclerView;
this.mItemTouchHelper = itemTouchHelper;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
removeAllViews();
detachAndScrapAttachedViews(recycler);
int itemCount = getItemCount();
if(itemCount>CardConfig.DEFAULT_SHOW_ITEM){
for(int position = CardConfig.DEFAULT_SHOW_ITEM;position>=0;position--){
final View view = recycler.getViewForPosition(position);
addView(view);
measureChildWithMargins(view,0,0);
int widthSpace = getWidth() -getDecoratedMeasuredWidth(view);
int heightSpace = getHeight() - getBottomDecorationHeight(view);
layoutDecoratedWithMargins(view,widthSpace/2,heightSpace/2,widthSpace/2+getDecoratedMeasuredWidth(view),heightSpace/2+getBottomDecorationHeight(view));
if(position == CardConfig.DEFAULT_SHOW_ITEM){
view.setScaleX(1-(position-1)*CardConfig.DEFAULT_SCALE);
view.setScaleY(1-(position-1)*CardConfig.DEFAULT_SCALE);
view.setTranslationY((position-