android listview 中集成侧滑功能

来源:互联网 发布:php程序员发展前景 编辑:程序博客网 时间:2024/06/07 05:43
<h1>android listview 中集成侧滑功能</h1>

在手机QQ新版中有个比较实用的功能就是在消息记录中侧滑出现两功能菜单,如图所示:



其实不单单手机qq,在很多app中集成了这样的功能,即美观又实用,由于项目需要我也在手机app客户端中集成了该功能。

要实现这个功能我们用到的是Github上的一个非常火的开源组件:SwipeMenuListView

首先,我们需要到 github 中下载该组件。

SwipeMenuListView 下载地址:https://github.com/baoyongzhang/SwipeMenuListView

要在项目中使用该开源控件库,比较快捷的方法是 在eclipse中导入下载好的 library 工程,并在自己的项目中引用该library 库就可以使用该组件 。为了代码管理方便,我直接将这个控件的所有代码 拷贝到了项目工程当中。

在布局文件中添加SwipeMenuListView 替换系统自带的listview:

            <com.insi.ishotelmanager.ui.swipemenulistview.SwipeMenuListView                android:id="@+id/lv_RoomDevice"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:listSelector="@color/transparent" >            </com.insi.ishotelmanager.ui.swipemenulistview.SwipeMenuListView>

在activity中获取该控件:

lv=(SwipeMenuListView)this.findViewById(R.id.lv_RoomDevice);

在代码中动态添加侧滑菜单的功能 ,并添加这两功能的点击事件:

/** * 初始设置listview侧滑菜单 */private void initListView() {SwipeMenuCreator creator =new SwipeMenuCreator() {@Overridepublic void create(SwipeMenu menu) {//添加修改设备ID菜单SwipeMenuItem addItem =new SwipeMenuItem(getApplicationContext());addItem.setBackground(new ColorDrawable(0xff23C5A8));addItem.setWidth(dp2px(90));addItem.setTitle(getString(R.string.setup_id));addItem.setTitleColor(Color.WHITE);addItem.setTitleSize(15);menu.addMenuItem(addItem);//添加注册设备ID菜单SwipeMenuItem registerItem =new SwipeMenuItem(getApplicationContext());registerItem.setBackground(new ColorDrawable(0xffF26F6D));registerItem.setWidth(dp2px(90));registerItem.setTitle(getString(R.string.register_id));registerItem.setTitleColor(Color.WHITE);registerItem.setTitleSize(15);menu.addMenuItem(registerItem);}};lv.setMenuCreator(creator);lv.setOnMenuItemClickListener(new OnMenuItemClickListener() {@Overridepublic void onMenuItemClick(int position, SwipeMenu menu, int index) {// TODO Auto-generated method stubswitch(index){case 0:CtrlDevice device = (CtrlDevice) adatper.getItem(position);//UIhelper.ToastMessage(HotelRoomDeviceActivity.this, "ID:"+device.getId()+" Name:"+device.getName());ShowAddNewIdDialog(device.getId());break;case 1:final CtrlDevice d = (CtrlDevice) adatper.getItem(position);final String id=et_hostId.getText().toString().trim();final String ip=et_hostIp.getText().toString().trim();final String port=et_hostport.getText().toString().trim();LogUtils.v("helloyifa", "host:"+" id:"+id+" ip:"+ip+" port:"+port);if(id.isEmpty()||ip.isEmpty()||port.isEmpty()){UIhelper.ToastMessage(HotelRoomDeviceActivity.this, getString(R.string.please_enter_complete_info));return ;}ProgressDialogUtils.showProgressDialog(HotelRoomDeviceActivity.this, getString(R.string.registering_device));//调用接口注册IDnew Thread(){public void run(){NetInfo info =new NetInfo();info.setIp(ip);info.setPort(Integer.valueOf(port));API api =new API(info);LogUtils.v("helloyifa", "register:"+"deviceId:"+d.getDeviceId());boolean how =api.registerDevice(id, d.getDeviceId());Message msg;if(how){msg=handler.obtainMessage(REGISTER_SUCCESSED);msg.sendToTarget();}else{msg=handler.obtainMessage(REGISTER_FAILED);msg.sendToTarget();}}}.start();break;}}});}



侧滑菜单宽度转换计算代码:

private int dp2px(int dp) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,getResources().getDisplayMetrics());}


由于SwipeMenuListView 是继承自android自身的listview 控件,所以继续使用原先的Adapter。


下边是app 运行的效果:



怎样?效果还不错吧!(请忽略界面中的小图标,这个是为了排版美观暂时从美团客户端中提取的==!)

上边是这个开源侧滑控件的最简单用法,这个控件中还有其它的功能,比如根据listview的每中不同的数据类型可以显示相应的侧滑菜单。这个功能手机qq中也有实现,具体的使用方法大家可以参考原作者在github上的说明。

</pre><pre>
0 0
原创粉丝点击