Android SwipeListView用法
2013-12-31 14:33
731 查看
最近在做一个项目,需要用到像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更新数据之后。
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更新数据之后。
相关文章推荐
- 【Android基本控件】ListView 的用法
- android:descendantFocusability用法简析 ListView 监听问题
- Android常用组件——定制化的SwipeListView
- android:ListView 的简单用法
- android listview用法2
- android 中listview 的用法 分类: Android界面和组件 2014-06-13 08:42 84人阅读 评论(0) 收藏
- Android第四十八期 - SwipeMenuListView
- xamarin android listview的用法
- Android UI控件----ExpandableListView的基本用法
- Android ListView几种Adapter用法简介
- Android listview与adapter用法
- 【Android基本控件】ListView 的用法
- Android实战简易教程-第五枪(ListView用法研究)
- android listview高级用法
- Android:ListView.addHeaderView()用法及其注意事项
- Android ListView常用用法
- ListView Item 中控件获得焦点 android:descendantFocusability用法简析
- android中ListView的基本组件及用法
- Android常用控件之GridView与ExpandableListView的用法
- android SwipeMenuListView ScrollView 滑动冲突