最近在写 TODO app,涉及到 Calendar 和 RecyclerView 的交互,

需求:

1. 往上滑动, Calendar 显示为周

2. 周显示模式下,往下滑动,显示为月

3. 列表下滑到第一个 item 的位置, Calendar 显示为周的时候,这时候改变为显示月

4. 列表上滑,Calendar 缩起来,显示为周,假如已经缩起来了,让列表滑动,显示更多的 item。

方法一:目前采用的是把 Calendar 和 RecyclerView 放在一个 LinearLayout 中,然后在 dispatchTouchEvent() 方法中根据上下滑动的手势进行了判断,然后让 Calendar 和 RecyclerView 进行交互。

方法二:目前看到另外一个新的解决办法是,使用 CoordinatorLayout 布局,然后自定义 Behavior,实现 Calendar 和 RecyclerView 的交互。

CalendarBehavior:

 public class CalendarBehavior extends CoordinatorLayout.Behavior<MonthPager> {
private int mTop; @Override
public boolean layoutDependsOn(CoordinatorLayout parent, MonthPager child, View dependency) {
return dependency instanceof RecyclerView;
} @Override
public boolean onLayoutChild(CoordinatorLayout parent, MonthPager child, int layoutDirection) {
parent.onLayoutChild(child, layoutDirection);
child.offsetTopAndBottom(mTop);
return true;
} private int dependentViewTop = -1; @Override
public boolean onDependentViewChanged(CoordinatorLayout parent, MonthPager child, View dependency) {
if (dependentViewTop != -1) {
int dy = dependency.getTop() - dependentViewTop; //dependency对其依赖的view(本例依赖的view是RecycleView)
int top = child.getTop();
if (dy > -top) {
dy = -top;
} if (dy < -top - child.getTopMovableDistance()) {
dy = -top - child.getTopMovableDistance();
} child.offsetTopAndBottom(dy);
}
dependentViewTop = dependency.getTop(); //dependency
mTop = child.getTop();
return true;
}
}

关于自定义 Behavior ,入门参考:http://blog.csdn.net/tabolt/article/details/51821933

链接:一文让你搞懂design设计的CoordinatorLayout和AppbarLayout联动,让Design设计更简单~:http://www.cnblogs.com/liushilin/p/6170735.html

05-26 18:34