1、需求说明:
A、listView展示
B、点击item进入下个界面
C、长按item进入下个界面
D、左滑展示delete按钮,点击按钮删除item
E、在Header有添加功能进入下个界面
F、在footer有描述说明,根据listview不同高度,展示不同的位置
2、实现说明:
在网络上找了许多资料,总有一些不能满足,最后终于找到了,如下
请参考https://github.com/jdsjlzx/LRecyclerView
LRecyclerView还是有很多功能的,根据自己的需求就可以了
A、展示listview
//获取设备列表
devices = deviceDBTools.findAll();
mDataAdapter = new SwipeAdapter();
mDataAdapter.setDataList(devices);
mLRecyclerViewAdapter = new LRecyclerViewAdapter(this, mDataAdapter);
mRecyclerView.setAdapter(mLRecyclerViewAdapter);
B、添加item事件
mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {//点击事件 @Override public void onItemClick(View view, int position) { if (mMeshServiceUtil.isConnectState()) { Intent intent = new Intent(mContext, SingleLightMainControlActivitiy.class); AssociateDevice device = devices.get(position); intent.putExtra("DEVICEID", device.getDeviceId()); startActivity(intent); } else { ToastUtils.showShortMsg(R.string.dialog_delete_lights); } }//长按事件 @Override public void onItemLongClick(View view, int position) { Intent intent = new Intent(mContext, EditLightActivity.class); AssociateDevice device = devices.get(position); intent.putExtra("DEVICEID", device.getDeviceId()); startActivity(intent); }});C、添加侧滑按钮//侧滑按钮mRecyclerView.setSwipeMenuCreator(swipeMenuCreator);// 设置菜单Item点击监听。mRecyclerView.setSwipeMenuItemClickListener(menuItemClickListener);private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() { @Override public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) { int size = getResources().getDimensionPixelSize(R.dimen.item_height); SwipeMenuItem deleteItem = new SwipeMenuItem(mContext) .setBackgroundColor(Color.RED) .setText(mContext.getString(R.string.delete)) .setWidth(size) .setHeight(size); swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧菜单。 }};private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() { /** * Item的菜单被点击的时候调用。 * @param closeable closeable. 用来关闭菜单。 * @param adapterPosition adapterPosition. 这个菜单所在的item在Adapter中position。 * @param menuPosition menuPosition. 这个菜单的position。比如你为某个Item创建了2个MenuItem,那么这个position可能是是 0、1, * @param direction 如果是左侧菜单,值是:SwipeMenuRecyclerView#LEFT_DIRECTION,如果是右侧菜单,值是:SwipeMenuRecyclerView#RIGHT_DIRECTION. */ @Override public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) { closeable.smoothCloseMenu();// 关闭被点击的菜单。 if (mMeshServiceUtil.isConnectState()) { //获取设备信息 AssociateDevice device = devices.get(adapterPosition); //删除设备关联分组信息 lightDBTools.deleteGroupDeviceByDeviceId(device.getDeviceId()); //删除设备新 deviceDBTools.deleteDeviceById(device.getDeviceId()); //重置设备 mMeshServiceUtil.resetDevice(device.getDeviceId()); devices.remove(adapterPosition); mDataAdapter.remove(adapterPosition); if (devices.size() == 0) { //搜索K的设备 mMeshServiceUtil.setMeshAdvert(MeshServiceUtil.SACN_Mesh_Advert); lightDBTools.deleteAll(); } //如果删除设备就可以生成二维码给别人扫描 if (settings.getUserId() == -1) { settings.setUserId(0); } //增加灯具后,用户为0,nextUserId从1开始增长;用户大于0,nextUserId从0开始增长。 if (settings.getUserId() != 0) { settings.setNextUserId(0); } else { settings.setNextUserId(1); } settingsDBTools.saveOrUpdate(settings); } else { ToastUtils.showShortMsg(R.string.dialog_delete_lights); } }};D、添加header和footer//add a HeaderViewmLRecyclerViewAdapter.addHeaderView(new SampleHeader(this));//add a FooterViewmLRecyclerViewAdapter.addFooterView(new SampleFooter(this));以上内容仅供学习参考,如有问题可以一起交流讨论