新版本的微信和QQ上引入的滑動刪除功能是現在比較流行的一個功能。其實這個滑動刪除的控制項,github上已經有了,是一個熱門的開源架構SwipeListView。不過,這個SwipeListView是一個framelayout,即是一個兩層的布局,上面的布局front覆蓋了下面的布局back,滑動的時候則會滑開front,這樣下面的back就顯示出來了。但是看了一下微信的滑動刪除好像不是這樣的,感覺更像是一個超出了螢幕的單層布局,滑動的時候是右邊超出螢幕的button進入螢幕,猜測應該不是使用SwipeListView控制項。QQ的滑動刪除則是在ListView的item右邊隱藏一個button,但檢測到滑動事件的時候,給button一個出現的動畫,使其可見,這個方案應該是最好實現的了。
本篇主要是學習SwipeListView這個開源架構。
使用這個架構有兩種方式,一種是匯入SwipeListViewLibrary這個工程,將其作為一個android工程的依賴庫。由於SwipeListViewLibrary庫工程自身也依賴另外一個熱門的開源架構NineOldAndroids,這個也很容易就能網上或者github上搜到。
匯入這兩個庫工程,對於NineOldAndroids,做如下設定,其實主要就是勾選Is Library這個選項,這樣就能是NineOldAndroids工程作為別的工程的依賴庫使用:
對於SwipeListViewLibrary,除了要勾選Is Library選項,記得在旁邊的Add裡面,加上上面的NineOldAndroids作為本庫的依賴庫:
下面就是使用這個庫了,先clean一下上面兩個庫工程,很多時候工程的錯誤,clean一下就好了。然後建立自己的工程,在Add選項裡面添加SwipeListViewLibrary工程就行。這樣就能直接使用SwipeListView這個控制項了,很簡單,代碼如下:
<com.fortysevendeg.swipelistview.SwipeListView android:id="@+id/swipe_lv" android:layout_width="match_parent" android:layout_height="match_parent" app:swipeMode="left" app:swipeAnimationTime="300" app:swipeOffsetLeft="200dp" app:swipeFrontView="@+id/front" app:swipeBackView="@+id/back" app:swipeActionLeft="reveal"/>
其中app:swipeFrontView屬性就是指定前面說的framelayout裡面上面一層的view的id,app:swipeBackView則是指定下面一層的view的id,在下面自訂BaseAdatpter要使用的item的布局裡面可以看到:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/back" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/close_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/delete_btn" android:text="Close" android:textAppearance="?android:attr/textAppearanceMedium" android:focusable="false" android:focusableInTouchMode="false"/> <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:text="Delete" android:textAppearance="?android:attr/textAppearanceMedium" android:focusable="false" android:focusableInTouchMode="false"/> </RelativeLayout> <RelativeLayout android:id="@+id/front" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <TextView android:id="@+id/content_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="hello world" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@android:color/black"/> </RelativeLayout></FrameLayout>
在Activity裡面初始化的代碼:
arrays = new ArrayList<String>(Arrays.asList(Util.arrays)); mSwipeLv = (SwipeListView)findViewById(R.id.swipe_lv); mAdapter = new MyAdapter(this, arrays); mSwipeLv.setAdapter(mAdapter); mSwipeLv.setSwipeListViewListener(new BaseSwipeListViewListener() { @Override public void onClosed(int position, boolean fromRight) { } });
以及自訂BaseAdapter中的getView():
@Override public View getView(final int position, View convertView, final ViewGroup parent) { ViewHolder holder; if(convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate( R.layout.layout_swipe_list_item, null); holder.mContentTv = (TextView)convertView.findViewById(R.id.content_tv); holder.mCloseBtn = (Button)convertView.findViewById(R.id.close_btn); holder.mDeleteBtn = (Button)convertView.findViewById(R.id.delete_btn); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.mContentTv.setText(arrays.get(position)); holder.mCloseBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((SwipeListView)parent).closeAnimate(position); } }); holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((SwipeListView)parent).closeOpenedItems(); mHandler.postDelayed(new Runnable() { @Override public void run() { mArrays.remove(position); mAdapter.notifyDataSetChanged(); } }, 350); } }); return convertView; }
然後就ok了,運行工程的效果如下圖:
另外一種是用SwipeListView控制項的方法就是直接匯入官方給出的兩個jar包,上面開篇的地址裡可以看到,但是直接匯入這兩個jar包,不代表可以立即使用了!首先先把這個包添加到建立工程的build path裡面,如果你的工程沒有添加android的支援包android-support-v4.jar記得也添加以下,然後記得從前面已經匯入過的SwipeListViewLibrary庫工程中的res\values\swipelistview__attrs.xml檔案複製到建立工程的res/values/目錄下,這個檔案主要是申明SwipeListView控制項裡面的各項屬性的,直接匯入的jar包是沒有包含申明這些屬性的檔案的。然後就是向上面一樣在代碼裡面引用了,不過需要注意兩點:一,jar包裡面SwipeListView的包名和庫工程裡面的包名是不一樣的,引用的時候需要注意以下;二,準備妥當,確認前面步驟無誤後,有時在編譯工程時回報錯,說沒有申明swipeFrontView和swipeBackView兩個屬性,這個問題好像是SwipeListView架構的一個bug,stackoverflow上有人指出過,,大意就是在布局檔案裡面申明swipeFrontView和swipeBackView這兩個屬性的值得時候,最好不要自訂id的名稱,而是使用swipelistview_backview和swipelistview_frontview。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。