Android: TODO 应用交互的两种实现方法(Behavior)

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

需求:

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

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

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

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

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

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

CalendarBehavior:

 1 public class CalendarBehavior extends CoordinatorLayout.Behavior<MonthPager> {
 2     private int mTop;
 3 
 4     @Override
 5     public boolean layoutDependsOn(CoordinatorLayout parent, MonthPager child, View dependency) {
 6         return dependency instanceof RecyclerView;
 7     }
 8 
 9     @Override
10     public boolean onLayoutChild(CoordinatorLayout parent, MonthPager child, int layoutDirection) {
11         parent.onLayoutChild(child, layoutDirection);
12         child.offsetTopAndBottom(mTop);
13         return true;
14     }
15 
16     private int dependentViewTop = -1;
17 
18     @Override
19     public boolean onDependentViewChanged(CoordinatorLayout parent, MonthPager child, View dependency) {
20         if (dependentViewTop != -1) {
21             int dy = dependency.getTop() - dependentViewTop;    //dependency对其依赖的view(本例依赖的view是RecycleView)
22             int top = child.getTop();
23             if (dy > -top) {
24                 dy = -top;
25             }
26 
27             if (dy < -top - child.getTopMovableDistance()) {
28                 dy = -top - child.getTopMovableDistance();
29             }
30 
31             child.offsetTopAndBottom(dy);
32         }
33         dependentViewTop = dependency.getTop(); //dependency
34         mTop = child.getTop();
35         return true;
36     }
37 }

 

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

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

 

转载于:https://www.cnblogs.com/liyiran/p/6197042.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值