【需求】 手机可以在本地添加通知提醒,并且支持编辑和删除,如下图:
【三方依赖库】
// RecyclerView侧滑菜单,Item拖拽,滑动删除Item,自动加载更多,HeaderView,FooterView,Item分组黏贴
compile 'com.yanzhenjie:recyclerview-swipe:1.1.4'
【xml 布局】
【代码】
initSlide(); // 初始化 侧滑删除编辑
initTallyList(); // 获取 本地记账提醒列表
【方法】
private void initSlide() {
mAdapter = new TallyRemindAdapter(this);
// srvWarn.setItemViewSwipeEnabled(true);// 开启滑动删除。默认关闭。
srvWarn.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
// 创建菜单:
SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
@Override
public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int viewType) {
/* SwipeMenuItem deleteItem = new SwipeMenuItem(mContext);
// 各种文字和图标属性设置。
leftMenu.addMenuItem(deleteItem); // 在Item左侧添加一个菜单。*/
// 在Item右侧添加一个菜单。
// 1.编辑
// 各种文字和图标属性设置。
SwipeMenuItem modifyItem = new SwipeMenuItem(TallyRemindActivity.this)
.setBackgroundColor(getResources().getColor(R.color.vest_yellow))
.setText("编辑")
.setTextColor(Color.BLACK)
.setTextSize(15) // 文字大小。
.setWidth(140)
.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
rightMenu.addMenuItem(modifyItem);
// 2 删除
SwipeMenuItem deleteItem = new SwipeMenuItem(TallyRemindActivity.this);
deleteItem.setText("删除")
.setBackgroundColor(getResources().getColor(R.color.pwd_f3323b))
.setTextColor(Color.WHITE) // 文字颜色。
.setTextSize(15) // 文字大小。
.setWidth(140)
.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
rightMenu.addMenuItem(deleteItem);
// 注意:哪边不想要菜单,那么不要添加即可。
}
};
// 设置监听器。
srvWarn.setSwipeMenuCreator(mSwipeMenuCreator);
SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
@Override
public void onItemClick(SwipeMenuBridge menuBridge) {
// 任何操作必须先关闭菜单,否则可能出现Item菜单打开状态错乱。
menuBridge.closeMenu();
int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。
if (menuPosition == 0) {
TallyRemindBean tallyRemindBean = tallyRecordList.get(adapterPosition);
showTimeSelector(true, tallyRemindBean);
} else {
ToastUtil.showToast("删除成功");
cancelLocalNotify(Long.valueOf(tallyRecordList.get(adapterPosition).getTime())); // 取消本地通知
TallyRemindBean tallyRemindBean = tallyRecordList.get(adapterPosition);
tallyRecordList.clear();
tallyRecordList.add(tallyRemindBean);
TallyRecordDataModel.getInstance(TallyRemindActivity.this).deletTallyRecordFromDb(tallyRecordList);
initTallyList(); // 刷新
}
}
};
// 菜单点击监听。
srvWarn.setSwipeMenuItemClickListener(mMenuItemClickListener);
// 必须 最后执行
srvWarn.setAdapter(mAdapter);
}
【添加通知 和 取消通知】
/**
* 添加本地推送
* @param startTime
*/
private void initLocalNotify(long startTime) {
long intervalMillis = 1000 * 60 * 60 * 24; // 一天
Intent intent = new Intent(mContext, RemindActionService.class);
intent.putExtra("title", "记账提醒");
intent.putExtra("contentText", "记得记账喔");
PendingIntent pendingIntent = PendingIntent.getService(mContext, (int )startTime, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, startTime, intervalMillis, pendingIntent);
}
/**
* 取消本地推送
* @param startTime
*/
private void cancelLocalNotify(long startTime) {
Intent intent = new Intent(mContext, RemindActionService.class);
intent.putExtra("title", "记账提醒");
intent.putExtra("contentText", "记得记账喔");
PendingIntent pendingIntent = PendingIntent.getService(mContext, (int )startTime, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
}
【时间选择器】
/**
* 时间选择器
*/
private void showTimeSelector(final boolean isEdit, final TallyRemindBean tallyRemindBean) {
Calendar c = Calendar.getInstance();
selectedDate = Calendar.getInstance();
startDate = Calendar.getInstance();
endDate = Calendar.getInstance();
//正确设置方式 原因:注意事项有说明
startDate.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR), c.get(Calendar.MINUTE), 0);
endDate.set(2019, 11, 31);
// 时间选择器
TimePickerView pvTime = new TimePickerBuilder(TallyRemindActivity.this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
mTime = date.getTime() + "";
// mTvTime.setText(DateUtil.long2Date(date.getTime(), "yyyy-MM-dd"));
if(isEdit){ // 处于编辑状态
if(tallyRemindBean != null){
tallyRemindBean.setTime(mTime);
TallyRecordDataModel.getInstance(TallyRemindActivity.this).updateTallyRecordFromDb(tallyRemindBean);
initTallyList(); // 刷新
initLocalNotify(date.getTime());
}
}else{ // 添加一条记录
mTallyId++;
TallyRemindBean mTallyBean = new TallyRemindBean();
mTallyBean.setTime(mTime);
mTallyBean.setUserId(userId+"");
TallyRecordDataModel.getInstance(TallyRemindActivity.this).addTallyRecordToDb(mTallyBean); // 保存到数据库
initTallyList(); // 刷新列表
initLocalNotify(date.getTime());
}
}
})
.setRangDate(startDate, endDate) // 设置 时间范围
.setDate(selectedDate) // 设置 默认当前时间
.setTitleText("设置提醒")
.setType(new boolean[]{false, false, false, true, true, false})
.build();// 默认全部显示
pvTime.show();
}
【实体类】
public class TallyRemindBean {
private String time;
private int id;
private String userId;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
【数据库操作】
/**
* 从本地db删除信息的方法
*
* @param tallyBeans
*/
public int deletTallyRecordFromDb(ArrayList tallyBeans) {
if (tallyBeans == null || tallyBeans.isEmpty()) {
return 0;
}
StringBuffer stringBuffer = new StringBuffer();
for (TallyRemindBean messageInfo : tallyBeans) {
if (messageInfo == null) {
continue;
}
if (stringBuffer.length() > 0) {
stringBuffer.append(" or ");
}
stringBuffer.append(ITallyRemindTable.ID + " = " + messageInfo.getId());
}
return delete(ITallyRemindTable.TABLE_NAME, stringBuffer.toString(), null);
}
/**
* 从本地db更改信息的方法
*
* @param tallyBean
*/
public int updateTallyRecordFromDb(TallyRemindBean tallyBean) {
if (tallyBean == null ) {
return 0;
}
String whereClause = ITallyRemindTable.ID + " = ?" ;
ContentValues contentValues = new ContentValues();
contentValues.put(ITallyRemindTable.TIME, tallyBean.getTime() );
return update(ITallyRemindTable.TABLE_NAME, contentValues, whereClause, new String[]{tallyBean.getId() + ""});
}