我正在开发一个小应用程序,其中我设置了通知,每当用户点击recyclerview中的通知图标图像时,通知将触发,同时我想用通知开始图标替换通知图标。
再次,如果用户点击通知开始图标,我想将图标更改为通知停止图标。
请告诉我们如何才能实现这一目标。谢谢你的帮助!!!
请查看以下截图和代码。
1)启动应用程序时的recyclerview屏幕截图
2)如果用户第一次点击通知图标,我想将图标更改为通知开始图标。
3)如果用户点击通知开始图标,我想再次将图标更改为通知停止图标
1)Recyclerview xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="65dp"
android:elevation="10dp"
android:background="#E0F2F1"
android:orientation="horizontal"
tools:ignore="UnusedAttribute">
<ImageView
android:padding="16dp"
android:id="@+id/listIcon"
android:tint="#FF5722"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_adjust_black_24dp"
tools:ignore="ContentDescription" />
<TextView
android:padding="8dp"
android:id="@+id/item_name"
android:textSize="17dp"
android:text="Dummy text"
android:layout_gravity="center"
android:fontFamily="Roboto Regular"
android:textColor="#000"
android:layout_width="250dp"
android:layout_height="wrap_content"
tools:ignore="HardcodedText,SpUsage" />
<ImageView
android:padding="16dp"
android:id="@+id/listIcon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_notifications_off_black_24dp"
tools:ignore="ContentDescription,RtlHardcoded" />
</LinearLayout>
2)Recyclerview适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
Context c;
ArrayList<Habits> habits;
private LayoutInflater layoutInflater;
TextView textItemName;
ImageView imageView, imageView1;
FragmentManager fm;
private ItemCLickCallBack itemCLickCallBack;
EditText Remainder;
Time t = new Time();
public interface ItemCLickCallBack {
void onItemClick();
}
public void setItemCLickCallBack(final ItemCLickCallBack itemCLickCallBack) {
this.itemCLickCallBack = itemCLickCallBack;
}
public RecyclerViewAdapter(Context context, ArrayList<Habits> habits) {
layoutInflater = LayoutInflater.from(context);
this.habits = habits;
this.c = context;
}
@Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.layout_item, parent, false);
return new ItemHolder(itemView);
}
@Override
public void onBindViewHolder(final RecyclerViewAdapter.ItemHolder holder, int position) {
final Habits item = habits.get(position);
holder.textItemName.setText(item.getName());
textItemName = (TextView) holder.textItemName.findViewById(R.id.item_name);
imageView = (ImageView) holder.imageView.findViewById(R.id.listIcon1);
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, final int pos) {
if (v.getId() == textItemName.getId()) {
Intent i = new Intent(c, SecondActivity.class);
c.startActivity(i);
}
if (v.getId() == imageView.getId()) {
LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.edit_dialogue, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
c);
alertDialogBuilder.setTitle("Edit Your Habit");
alertDialogBuilder.setIcon(R.drawable.ic_edit_black_18dp);
alertDialogBuilder.setView(dialogView);
final EditText EdiTime = (EditText) dialogView
.findViewById(R.id.editTime);
final EditText EditHabit = (EditText) dialogView
.findViewById(R.id.edithabit);
final EditText Remainder = (EditText) dialogView.
findViewById(R.id.interval);
Remainder.setText(item.getInterval());
EditHabit.setText((CharSequence) item.getName());
EdiTime.setText(editTimeText());
EdiTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(c, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
mcurrentTime.set(Calendar.HOUR_OF_DAY, hour);
mcurrentTime.set(Calendar.MINUTE, minute);
Intent intent = new Intent(c, Notification_reciever.class);
intent.putExtra("HABIT_KEY",item.getName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) c.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mcurrentTime.getTimeInMillis(),Integer.parseInt(Remainder.getText().toString())*60*1000, pendingIntent);
}
}, hour, minute, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
update(item.getId(), EditHabit.getText().toString(), t.getHour(), t.getMinute(), Remainder.getText().toString());
itemCLickCallBack.onItemClick();
}
})
.setNegativeButton("DELETE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
delete(item.getId());
itemCLickCallBack.onItemClick();
notifyDataSetChanged();
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
@Override
public int getItemCount() {
return habits.size();
}
class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageView, imageView1;
private TextView textItemName;
ItemClickListener itemClickListener;
public ItemHolder(View itemView) {
super(itemView);
textItemName = (TextView) itemView.findViewById(R.id.item_name);
imageView = (ImageView) itemView.findViewById(R.id.listIcon1);
imageView.setOnClickListener(this);
textItemName.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v, getLayoutPosition());
}
}
private void update(int id, String name, int hour, int minute, String interval) {
DBAdapter db = new DBAdapter(c);
db.openDB();
long result = db.update(id, name, hour, minute, interval);
if (result > 0) {
Toast.makeText(c, "Saved successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(c, "failed", Toast.LENGTH_LONG).show();
}
db.closeDB();
}
private void delete(int id) {
DBAdapter db = new DBAdapter(c);
db.openDB();
long result = db.delete(id);
db.closeDB();
if (result > 0) {
Toast.makeText(c, "Deleted successfully", Toast.LENGTH_LONG).show();
}
}