最近在做一个项目,需要用到像IPone删除的效果,即在item上左滑弹出删除的按钮。找了一些资料,发现在github上有个SwipeListView的项目可以实现这个功能。下面介绍一下SwipeListView的使用方法。
1、下载library和demo:https://github.com/47deg/android-swipelistview。
2、下载依赖lib nineoldandroids-2.4.0.jar:https://github.com/JakeWharton/NineOldAndroids/downloads。
3、解压SwipeListView文件,在eclipse里导入swipeListView的lib项目,把nineoldandroids-2.4.0.jar和android-support-v4.jar加入到libs下面。
4、导入demo项目,引入swipeListView库。
5、swipeListView的用法,在布局文件里:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_app"
android:orientation="vertical" >
<com.fortysevendeg.swipelistview.SwipeListView
android:id="@+id/example_lv_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#00000000"
swipe:swipeBackView="@+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeDrawableChecked="@drawable/choice_selected"
swipe:swipeDrawableUnchecked="@drawable/choice_unselected"
swipe:swipeFrontView="@+id/front"
swipe:swipeMode="both" />
</LinearLayout>
item布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/back"
style="@style/ListBackContent"
android:tag="back" >
<Button
android:id="@+id/example_row_b_action_1"
style="@style/ListButtonAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open" />
<Button
android:id="@+id/example_row_b_action_2"
style="@style/ListButtonAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/googlePlay" />
<Button
android:id="@+id/example_row_b_action_3"
style="@style/ListButtonAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/uninstall" />
</LinearLayout>
<RelativeLayout
android:id="@+id/front"
style="@style/ListFrontContent"
android:orientation="vertical"
android:tag="front" >
<ImageView
android:id="@+id/example_row_iv_image"
style="@style/ListImage" />
<TextView
android:id="@+id/example_row_tv_title"
style="@style/ListTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/example_row_iv_image" />
<TextView
android:id="@+id/example_row_tv_description"
style="@style/ListDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/example_row_tv_title"
android:layout_toRightOf="@id/example_row_iv_image" />
</RelativeLayout>
</FrameLayout>
代码:
adapter = new PackageAdapter(this, data);
swipeListView = (SwipeListView) findViewById(R.id.example_lv_list);
if (Build.VERSION.SDK_INT >= 11) {
swipeListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
swipeListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
mode.setTitle("Selected (" + swipeListView.getCountSelected() + ")");
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_delete) {
swipeListView.dismissSelected();
return true;
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_choice_items, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
swipeListView.unselectedChoiceStates();
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
}
swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
}
@Override
public void onClosed(int position, boolean fromRight) {
}
@Override
public void onListChanged() {
}
@Override
public void onMove(int position, float x) {
}
@Override
public void onStartOpen(int position, int action, boolean right) {
Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
}
@Override
public void onStartClose(int position, boolean right) {
Log.d("swipe", String.format("onStartClose %d", position));
}
@Override
public void onClickFrontView(int position) {
Log.d("swipe", String.format("onClickFrontView %d", position));
}
@Override
public void onClickBackView(int position) {
Log.d("swipe", String.format("onClickBackView %d", position));
}
@Override
public void onDismiss(int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
data.remove(position);
}
adapter.notifyDataSetChanged();
}
});
swipeListView.setAdapter(adapter);
以上就是SwipeListView的用法。
注:设置swipeListView的模式要在adapter更新数据之后。