这几天项目中做了一个页面滑动效果,今天总结了一个小demo来分享一下,先来看一下效果哈:::
这种效果总结起来可以分为两部分,一个是包裹在最外层的layout,滑动时先由layout拦截滑动事件,当顶部view缩小为0后,RecyclerView再接收滑动事件,在RecyclerView中支持多种不同item布局。
先看一下最外层的Layout事件拦截逻辑,这里我自定义了一个UniteSlideLayout继承RelativeLayout,在UniteSlideLayout的事件拦截方法(onInterceptTouchEvent)中,对MotionEvent.ACTION_MOVE做判断,当符合Layout滑动条件时,该方法反回true,即拦截了MOVE事件,当拦截了MOVE事件后,之后的MOVE都由Layout消耗,即调用UniteSlideLayout的onTouchEvent(MotionEvent event)消耗MOVE事件,MOVE事件被拦截后,在一个事件序列中,针对MOVE事件,onInterceptTouchEvent方法都不会再被调用。下面看一下Layout事件拦截方法onInterceptTouchEvent的实现:
public boolean onInterceptTouchEvent(MotionEvent ev) {
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInterceptLastMotionY = y;
if (!isCanSlide) {
return true;
}
break;
case MotionEvent.ACTION_MOVE:
int dy = y - mInterceptLastMotionY;
if ((Math.abs(dy) >= mProtectDistance)
&& ((mIsShrink && mCardListView.getScrollToTop() && dy > 0) || (!mIsShrink))) {
mOnTouchLastMotionY = y;
mCardListView.setOpListActionUp(false);
return true;
}
break;
case MotionEvent.ACTION_UP:
if (!mCardListView.getOpListActionUp()) {
mCardListView.setOpListActionUp(true);
return true;
}
break;
default:
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int y = (int) event.getY();
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mOnTouchLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
if (mIsFirstTouch) {
mOnTouchLastMotionY = y;
mIsFirstTouch = false;
}
int dy = y - mOnTouchLastMotionY;
if (mCardListView.getVisibility() == View.VISIBLE) {
doListAnimByMove(mListContent, mListContentLayoutParams, dy);
if (mScaleSize == 0) {
event.setAction(MotionEvent.ACTION_DOWN);
dispatchTouchEvent(event);
}
}
mOnTouchLastMotionY = y;
break;
case MotionEvent.ACTION_UP:
if (!mCardListView.getOpListActionUp()) {
mCardListView.setOpListActionUp(true);
}
mIsFirstTouch = true;
handleUpAnim();
releaseVelocityTracker();
break;
case MotionEvent.ACTION_CANCEL:
releaseVelocityTracker();
break;
}
return super.onTouchEvent(event);
}
public boolean dispatchTouchEvent(MotionEvent event) {
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mInterceptLastMotionY = y;
if (!opListActionUp) {
return true;
}
break;
case MotionEvent.ACTION_MOVE:
int dy = y - mInterceptLastMotionY;
if (dy > mProtectDistance) {
if (mIsScrollToTop) {
float offsetX = 0;
float offsetY = 0;
for (View v = this; v != null && v != parent; v = (View) v.getParent()) {
offsetX += v.getLeft() - v.getScrollX();
offsetY += v.getTop() - v.getScrollY();
}
final MotionEvent eventParent = MotionEvent.obtainNoHistory(event);
eventParent.offsetLocation(offsetX, offsetY);
eventParent.setAction(MotionEvent.ACTION_DOWN);
if (parent != null) {
post(new Runnable() {
@Override
public void run() {
parent.dispatchTouchEvent(eventParent);
}
});
return false;
}
}
}
break;
case MotionEvent.ACTION_UP:
if (!opListActionUp) {
opListActionUp = true;
}
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
public class OpViewHolderFactory {
public static final int GRID_VIEWHOLDER = 1;
public static final int DEFAULT_VIEWHOLDER = 2;
public static final int IMAGE_VIEWHOLDER = 3;
public static final Map linkCardAndHolderMap = new HashMap();
static {
linkCardAndHolderMap.put(OneCardView.CARDVIEW_KEY, OpDefaultViewHolder.HOLDER_KEY);
linkCardAndHolderMap.put(TwoCardView.CARDVIEW_KEY, OpDefaultViewHolder.HOLDER_KEY);
linkCardAndHolderMap.put(ThreeCardView.CARDVIEW_KEY, OpDefaultViewHolder.HOLDER_KEY);
linkCardAndHolderMap.put(GridCardView.CARDVIEW_KEY, GridViewHolder.HOLDER_KEY);
linkCardAndHolderMap.put(ImageCardView.CARDVIEW_KEY, ImageViewHolder.HOLDER_KEY);
}
public static BaseCardViewHolder getViewHolderByKey(ViewGroup viewGroup, int holderKey) {
LayoutInflater inflater = LayoutInflater.from(MyApplication.getInstance());
switch (holderKey) {
case DEFAULT_VIEWHOLDER:
return new OpDefaultViewHolder(inflater.inflate(R.layout.big_icon_item_view, viewGroup, false));
case GRID_VIEWHOLDER:
return new GridViewHolder(inflater.inflate(R.layout.grid_item_view, viewGroup, false));
case IMAGE_VIEWHOLDER:
return new ImageViewHolder(inflater.inflate(R.layout.image_item_view, viewGroup, false));
default:
break;
}
return null;
}
}
下面就是具体的Item了,每个Item继承OpBaseCardItem这个抽象类,代码如下:
public abstract class OpBaseCardItem {
private boolean mFirstCreate = true;
/**
* 返回列表展示的UI样式类型
*/
public abstract int getCardViewType();
/**
* 返回列表展示的UI样式类型
*/
public abstract String getCardPageType();
/**
* 判断列表是否可以展示,各列表自行控制
*/
public boolean needShow() {
return false;
}
/**
* 等同于Adapter的getView,具体列表控制UI展示
*/
public abstract void bindItemView(Context context, RecyclerView.ViewHolder viewHolder, CardListAdapter
cardAdapter, int position);
public void reportShowAndbindView(Context context, RecyclerView.ViewHolder viewHolder, CardListAdapter
cardAdapter, int position) {
// 默认实现,上报功能卡片的创建展示
if (mFirstCreate) {
mFirstCreate = false;
}
bindItemView(context, viewHolder, cardAdapter, position);
}
}
public class OpCardViewFactory {
public static final String TAG = "OpCardViewFactory";
public static final String GRID_CARD_KEY = "grid_item";
public static final String BIG_ONE_CARD_KEY = "one_big_icon_item";
public static final String BIG_TWO_CARD_KEY = "two_big_icon_item";
public static final String BIG_THREE_CARD_KEY = "ohree_big_icon_item";
public static final String IMAGE_CARD_KEY = "image_card_item";
/**
* 卡片key - 实例映射表
*/
private static final Map> keyCardInstanceMap =
new HashMap>();
static {
keyCardInstanceMap.put(GRID_CARD_KEY, GridCardView.class);
keyCardInstanceMap.put(BIG_ONE_CARD_KEY, OneCardView.class);
keyCardInstanceMap.put(BIG_TWO_CARD_KEY, TwoCardView.class);
keyCardInstanceMap.put(BIG_THREE_CARD_KEY, ThreeCardView.class);
keyCardInstanceMap.put(IMAGE_CARD_KEY, ImageCardView.class);
}
public static OpBaseCardItem getCardItem(String cardKey) {
Class extends OpBaseCardItem> itemClass = keyCardInstanceMap.get(cardKey);
if (itemClass != null) {
try {
return itemClass.newInstance();
} catch (Exception e) {
}
}
return null;
}
}