【需求】 手機(jī)可以在本地添加通知提醒譬挚,并且支持編輯和刪除粪薛,如下圖:
支持編輯和刪除.png
【三方依賴(lài)庫(kù)】
// RecyclerView側(cè)滑菜單黔姜,Item拖拽甘有,滑動(dòng)刪除Item律适,自動(dòng)加載更多腔长,HeaderView袭祟,F(xiàn)ooterView,Item分組黏貼
compile 'com.yanzhenjie:recyclerview-swipe:1.1.4'
【xml 布局】
<com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
android:id="@+id/xrv_warn"
android:layout_above="@+id/rl_add_remind"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
【代碼】
initSlide(); // 初始化 側(cè)滑刪除編輯
initTallyList(); // 獲取 本地記賬提醒列表
【方法】
private void initSlide() {
mAdapter = new TallyRemindAdapter(this);
// srvWarn.setItemViewSwipeEnabled(true);// 開(kāi)啟滑動(dòng)刪除捞附。默認(rèn)關(guān)閉巾乳。
srvWarn.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
// 創(chuàng)建菜單:
SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
@Override
public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int viewType) {
/* SwipeMenuItem deleteItem = new SwipeMenuItem(mContext);
// 各種文字和圖標(biāo)屬性設(shè)置您没。
leftMenu.addMenuItem(deleteItem); // 在Item左側(cè)添加一個(gè)菜單。*/
// 在Item右側(cè)添加一個(gè)菜單胆绊。
// 1.編輯
// 各種文字和圖標(biāo)屬性設(shè)置氨鹏。
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);
// 注意:哪邊不想要菜單,那么不要添加即可种冬。
}
};
// 設(shè)置監(jiān)聽(tīng)器肢础。
srvWarn.setSwipeMenuCreator(mSwipeMenuCreator);
SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
@Override
public void onItemClick(SwipeMenuBridge menuBridge) {
// 任何操作必須先關(guān)閉菜單,否則可能出現(xiàn)Item菜單打開(kāi)狀態(tài)錯(cuò)亂碌廓。
menuBridge.closeMenu();
int direction = menuBridge.getDirection(); // 左側(cè)還是右側(cè)菜單传轰。
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(); // 刷新
}
}
};
// 菜單點(diǎn)擊監(jiān)聽(tīng)慨蛙。
srvWarn.setSwipeMenuItemClickListener(mMenuItemClickListener);
// 必須 最后執(zhí)行
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);
}
【時(shí)間選擇器】
/**
* 時(shí)間選擇器
*/
private void showTimeSelector(final boolean isEdit, final TallyRemindBean tallyRemindBean) {
Calendar c = Calendar.getInstance();
selectedDate = Calendar.getInstance();
startDate = Calendar.getInstance();
endDate = Calendar.getInstance();
//正確設(shè)置方式 原因:注意事項(xiàng)有說(shuō)明
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);
// 時(shí)間選擇器
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){ // 處于編輯狀態(tài)
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); // 保存到數(shù)據(jù)庫(kù)
initTallyList(); // 刷新列表
initLocalNotify(date.getTime());
}
}
})
.setRangDate(startDate, endDate) // 設(shè)置 時(shí)間范圍
.setDate(selectedDate) // 設(shè)置 默認(rèn)當(dāng)前時(shí)間
.setTitleText("設(shè)置提醒")
.setType(new boolean[]{false, false, false, true, true, false})
.build();// 默認(rèn)全部顯示
pvTime.show();
}
【實(shí)體類(lèi)】
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;
}
}
【數(shù)據(jù)庫(kù)操作】
/**
* 從本地db刪除信息的方法
*
* @param tallyBeans
*/
public int deletTallyRecordFromDb(ArrayList<TallyRemindBean> 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() + ""});
}