編輯:關於Android編程
QQ或者微信出現過滑動,最近聯系人列表,可以刪去當前選中的聯系人,這個功能很棒。
就是試著做了下。其實是使用了開源框架SwipeListView 。

SwipeListView 與一般的ListView使用方式差不多,只是增加了一些特殊功能。
<com.fortysevendeg.swipelistview.SwipeListView xmlns:swipe="http://schemas.android.com/apk/res-auto" android:id="@+id/example_lv_list" android:listSelector="#00000000" android:layout_width="fill_parent" android:layout_height="wrap_content" swipe:swipeFrontView="@+id/front" swipe:swipeBackView="@+id/back" swipe:swipeActionLeft="[reveal | dismiss]" swipe:swipeActionRight="[reveal | dismiss]" swipe:swipeMode="[none | both | right | left]" swipe:swipeCloseAllItemsWhenMoveList="[true | false]" swipe:swipeOpenOnLongPress="[true | false]" swipe:swipeAnimationTime="[miliseconds]" swipe:swipeOffsetLeft="[dimension]" swipe:swipeOffsetRight="[dimension]" />
•swipeFrontView -ListView Item正常顯示的控件Id,且必須與Item的布局文件中的控件id一樣
•swipeBackView - 手指滑動時顯示的,隱藏在FrontView後面,且必須與item的布局文件中控件Id一樣
•swipeActionLeft - 左滑的動作,默認reveal,即顯示BackView,還有dismiss,choice會觸發響應的方法。
•swipeActionRight - 右滑動作,其他同上
•swipeMode - Default: 'both' 設置左滑、右滑、都支持
•swipeCloseAllItemsWhenMoveList - 當滾動listview時,關閉所有展開的Item,最好不要設置為false,由於item的
• 復用,false存在一些問題。
•swipeOpenOnLongPress - Default: 'true' 長按時觸發顯示
•swipeAnimationTime - 動畫時間長度
•swipeOffsetLeft - left offset 左偏移量
•swipeOffsetRight - right offset 右偏移量
mSwipeListView = (SwipeListView) findViewById(R.id.id_swipelistview);
mAdapter = new DataAdapter(this, mDatas , mSwipeListView);
mSwipeListView.setAdapter(mAdapter);
mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener()
{
@Override
//重寫BaseSwipeListViewListener父類需要的方法
};
使用方式很簡單 和普通的ListView 相似,不需要多說。
對於 ListView的Item刪除單個元素,只需要在Adapter中處理button的點擊事件,或者寫一個回調傳回Activity中處理
我這裡給出在Adapter中處理的方式的代碼:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.id_text);
Button del = (Button) convertView.findViewById(R.id.id_remove);
tv.setText(mDatas.get(position));
del.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mDatas.remove(position);
notifyDataSetChanged();
/**
* 關閉SwipeListView
* 不關閉的話,剛刪除位置的item存在問題
* 在監聽事件中onListChange中關閉,會出現問題
*/
mSwipeListView.closeOpenedItems();
}
});
return convertView;
}
源碼下載:https://github.com/honjane/SwipeListViewDemo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android之——系統進程與用戶進程分離
在上一篇博文《Android之——殺死用戶選中的進程優化》一文中,我向大家介紹了如何優化用戶的體驗,那麼這篇博文中,我將向大家介紹如何進行系統進程
華為麥芒5高配版怎麼樣 高配版和標配版有什麼區別
麥芒5的正面仍堅持了大黑邊的設計風格,真的很華為,那麼新款的華為麥芒5標配版和高配版有什麼區別呢?高配版如何?讓我們一起來看看吧!華為麥芒5標配版和高配版區
Android系統OTA升級流程
Android系統進行升級的時候,有兩種途徑,一種是通過接口傳遞升級包路徑自動升級,升級完之後系統自動重啟;另一種是手動進入recovery模式下,選擇升級包進行升級,升
Java的進化? Kotlin初探與集成Android項目
介紹:Statically typed programming language for the JVM, Android and the browser. 100% i