今天繼續學習“鴻祥_”大神的寫的“ListView滑動刪除,仿騰訊QQ” 。
今天,我準備學習並理清QQListView的邏輯。
1.自定義ListView
先看看佈局文件activity_main.xml:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.example.listviewitemslidedeletebtnshow.QQListView
- android:id="@+id/id_listview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- </com.example.listviewitemslidedeletebtnshow.QQListView>
- </RelativeLayout>
然後,我們將視線轉到com.example.listviewitemslidedeletebtnshow.QQListView類中
- public class QQListView extends ListView
- {
- //首先,定義一些屬性
- //然後,定義構造函數,用PopupWindow顯示“刪除”按鈕,並獲得PopupWindow的寬高;
- //然後,覆寫dispatchTouchEvent和onTouchEvent方法;
- //然後,再寫一些需要用到的方法;
- }
這部分是進行了非常關鍵的操作(我理解了其中操作的實現,還不是非常理解在操作時是否有衝突)。
2.1 dispatchTouchEvent的功能
通過dispatchTouchEvent判斷是否存在“從右到左的滑動”事件,並根據觸摸點確定所在Item,然後獲得Item所在View;
2.2 onTouchEvent的功能
對滑動事件進行處理,根據Touch處View的座標確定mPopupWindow的顯示位置,並以動畫的形式進行顯示。
3.其它
在這篇博文中還有很多值的學習的地方,比如接口的使用等等。我先把上面的這些消化後,再深入分析吧。