使用swipelistview向左滑动不动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用swipelistview向左滑动不动相关的知识,希望对你有一定的参考价值。
本例子实现了滑动删除ListView的Itemdemo的效果、大家都知道、这种创意是来源于ios的、左滑删除的功能、在android上面实现比较麻烦、本例子中不仅实现了左滑删除功能、还实现了左滑赞、左滑分享、左滑收藏等功能、当然大家也可以根据自己项目的需求来修改功能、且在左滑的时候还配有简单的滑动动画呢、非常不错、下面一起来看看实现过程吧初始化代码
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mAppList = getPackageManager().getInstalledApplications(0);
mListView = (SwipeMenuListView) findViewById(R.id.listView);
mAdapter = new AppAdapter();
mListView.setAdapter(mAdapter);
// step 1. create a MenuCreator
SwipeMenuCreator creator = new SwipeMenuCreator()
@Override
public void create(SwipeMenu menu)
// create "open" item
SwipeMenuItem openItem = new SwipeMenuItem(
getApplicationContext());
// set item background
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
openItem.setWidth(dp2px(90));
// set item title
openItem.setTitle("Open");
// set item title fontsize
openItem.setTitleSize(18);
// set item title font color
openItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getApplicationContext());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(dp2px(90));
// set a icon
deleteItem.setIcon(R.drawable.ic_delete);
// add to menu
menu.addMenuItem(deleteItem);
;
// set creator
mListView.setMenuCreator(creator);
绑定左滑和删除事件
// step 2. listener item click event
mListView.setOnMenuItemClickListener(new
OnMenuItemClickListener()
@Override
public void onMenuItemClick(int position,
SwipeMenu menu, int index)
ApplicationInfo item = mAppList.get(position);
switch (index)
case 0:
// open
open(item);
break;
case 1:
// delete
//delete(item);
mAppList.remove(position);
mAdapter.notifyDataSetChanged();
break;
);
// set SwipeListener
mListView.setOnSwipeListener(new OnSwipeListener()
@Override
public void onSwipeStart(int position)
// swipe start
@Override
public void onSwipeEnd(int position)
// swipe end
);
// other setting
//listView.setCloseInterpolator(new BounceInterpolator());
// test item long click
mListView.setOnItemLongClickListener(new
OnItemLongClickListener()
@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view,
int position, long id)
Toast.makeText(getApplicationContext(),
position " long click", 0).show();
return false;
); 参考技术A 以android-swipelistview的开源项目为例。
android-swipelistview项目分两个部分:
1.lib是:android-swipelistview;
2.例子是:SwipeListViewExampleActivity。
进入正题:
android-swipelistview:
1. 引入android-swipelistview库:导入开源库,用Import选项,然后Android选项下的“Existing Android Code Into Workspace”引入库;
2.引入android-swipelistview的依赖库nineoldandroids-2.4.0.jar:项目的Properties选项,Java Build Path标签,Libraries标签,Add External JARs按钮导入nineoldandroids-2.4.0.jar包;
3.引入android-swipelistview的依赖内部库android-support-v4.jar:项目的Android Tools选项,“Add Support Library”来增加android-support-v4库;(注:如果找不到对应的support库,可以通过SDK Manager来进行下载)
4.编译android-swipelistview库的jar包:项目的Properties选项,Android选项,勾选"Is Library";
5.编译android-swipelistview项目,在项目的bin目录应该能看到android-swipelistview.jar包。
SwipeListViewExampleActivity:
6.引入android-swipelistview.jar包:项目的Properties选项,Android选项,Library框选择add按钮添加android-swipelistview.jar包;
7.同第3步一样引入android-support-v4库;
8.打包前要包含android-support-v4库:项目的Properties选项,Java Build Path标签,Order and Export选项,勾选“Android Private Libraries”选项;
9.编译SwipeListViewExampleActivity项目。(注:更改配置项可能不会重新编译,所以需要手动clean,否则可能会出现一些莫名其妙的错误,本人就吃了大亏,请大家牢记)本回答被提问者采纳
以上是关于使用swipelistview向左滑动不动的主要内容,如果未能解决你的问题,请参考以下文章