Android: TODO 應用互動的兩種實現方法(Behavior)

來源:互聯網
上載者:User

標籤:off   recycle   override   pat   span   extend   android   http   tde   

最近在寫 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     @Override10     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     @Override19     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(); //dependency34         mTop = child.getTop();35         return true;36     }37 }

 

關於自訂 Behavior ,入門參考:http://blog.csdn.net/tabolt/article/details/51821933

 

Android: TODO 應用互動的兩種實現方法(Behavior)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.