• 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

  • 相关阅读:
    css选择器优先级
    内置函数filter和map
    hdu 3068 最长回文 manacher算法(视频)
    hdu 5752 Sqrt Bo
    HDU 2176 取(m堆)石子游戏(Nim)
    HDU 2188 悼念512汶川大地震遇难同胞――选拔志愿者(巴什博奕)
    HDU 2897 邂逅明下(巴什博奕)
    POJ 1740 A New Stone Game(普通博弈)
    HDU 2516 取石子游戏(斐波那契博弈)
    hdu 1361 Parencodings 简单模拟
  • 原文地址:https://www.cnblogs.com/liyiran/p/6197042.html
Copyright © 2020-2023  润新知