动态改变 RecyclerView 的颜色
change RecyclerView's color dynamically
在我的应用程序中,我有一个待办事项列表。当用户检查任务是否重要时,它的背景变为红色;
它以某种方式工作,但在返回 mainActivity 后,它会更改随机列表项的颜色!
here一切正常
But here任务 4 未被检查为重要,任务 5 被检查为重要!
这是我的 recyclerViewAdaptor 代码:
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
if (!cursor.moveToPosition(position))
return;
final long id = cursor.getLong(cursor.getColumnIndex(DBContracts.Tasks._ID));
time = cursor.getInt(cursor.getColumnIndex(DBContracts.Tasks.T_TIME));
hour = String.valueOf(time / 60);
min = String.valueOf(time % 60);
if (Integer.parseInt(hour) < 10){
hour = "0" + hour;
}
if (Integer.parseInt(min) < 10){
min = "0" + min;
}
//!!!!!!!!!!!!here!!!!!!!!!!!!!
//change color if isImp is true
isImp = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_IS_IMP)));
if (isImp){
card.setBackgroundColor(context.getResources().getColor(R.color.isImpColor));
}else {
card.setBackgroundColor(context.getResources().getColor(R.color.isNotImpColor));
}
holder.name.setText(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_NAME)));
holder.time.setText(hour + ":" + min);
holder.itemView.setTag(id);
holder.isDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vb.vibrate(100);
DBOps.Ops.done(id,"true");
swap(DBOps.Ops.getEmAll(false));
}});
}
下面是添加新任务的代码:
public class AddNewTaskActivity extends AppCompatActivity {
EditText taskName;
TimePicker taskTime;
Switch aSwitch;
boolean isImp = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_task);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
setTitle("New task");
taskName = (EditText) findViewById(R.id.taskName);
taskTime = (TimePicker) findViewById(R.id.timePicker);
aSwitch = (Switch) findViewById(R.id.isImp);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (aSwitch.isChecked()) {
isImp = true;
} else {
isImp = false;
}
}
});
taskTime.setIs24HourView(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_new_task_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.addNew :
addNewTaskToDB();
break;
case android.R.id.home :
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private void addNewTaskToDB() {
if (taskName.length() == 0){
Toast.makeText(this,"Type something as title.",Toast.LENGTH_SHORT).show();
}else {
int time = taskTime.getHour() * 60 + taskTime.getMinute();
DBOps.Ops.addNewTaskOp(
taskName.getText().toString().trim(),
time,
String.valueOf(isImp));
setAlarm();
finish();
}
}
public void setAlarm(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,taskTime.getHour());
calendar.set(Calendar.MINUTE,taskTime.getMinute());
calendar.set(Calendar.SECOND,0);
Intent intent = new Intent(this,NotificationReciever.class);
intent.putExtra("name",taskName.getText());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
这是示例代码,在查看您的代码后,您必须在 ViewHolder 中定义卡片
isImp = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_IS_IMP)));
if (isImp){
holder.card.setBackgroundColor(context.getResources().getColor(R.color.isImpColor));
} else {
//set the card normal color here
holder.card.setBackGroundColor(*insert your normal color here*);
}
RecyclerView 回收它的项目 ...所以直接改变一个项目的颜色可能会在滚动时改变其他项目的颜色..为了实现这个你需要应用一个小技巧(通过在模型 class.
中使用 boolean isChecked
变量
查看此内容以供参考。
编辑
将您的 card
更改为 holder.card
在我的应用程序中,我有一个待办事项列表。当用户检查任务是否重要时,它的背景变为红色; 它以某种方式工作,但在返回 mainActivity 后,它会更改随机列表项的颜色!
here一切正常
But here任务 4 未被检查为重要,任务 5 被检查为重要!
这是我的 recyclerViewAdaptor 代码:
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
if (!cursor.moveToPosition(position))
return;
final long id = cursor.getLong(cursor.getColumnIndex(DBContracts.Tasks._ID));
time = cursor.getInt(cursor.getColumnIndex(DBContracts.Tasks.T_TIME));
hour = String.valueOf(time / 60);
min = String.valueOf(time % 60);
if (Integer.parseInt(hour) < 10){
hour = "0" + hour;
}
if (Integer.parseInt(min) < 10){
min = "0" + min;
}
//!!!!!!!!!!!!here!!!!!!!!!!!!!
//change color if isImp is true
isImp = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_IS_IMP)));
if (isImp){
card.setBackgroundColor(context.getResources().getColor(R.color.isImpColor));
}else {
card.setBackgroundColor(context.getResources().getColor(R.color.isNotImpColor));
}
holder.name.setText(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_NAME)));
holder.time.setText(hour + ":" + min);
holder.itemView.setTag(id);
holder.isDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vb.vibrate(100);
DBOps.Ops.done(id,"true");
swap(DBOps.Ops.getEmAll(false));
}});
}
下面是添加新任务的代码:
public class AddNewTaskActivity extends AppCompatActivity {
EditText taskName;
TimePicker taskTime;
Switch aSwitch;
boolean isImp = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_task);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
setTitle("New task");
taskName = (EditText) findViewById(R.id.taskName);
taskTime = (TimePicker) findViewById(R.id.timePicker);
aSwitch = (Switch) findViewById(R.id.isImp);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (aSwitch.isChecked()) {
isImp = true;
} else {
isImp = false;
}
}
});
taskTime.setIs24HourView(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_new_task_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.addNew :
addNewTaskToDB();
break;
case android.R.id.home :
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private void addNewTaskToDB() {
if (taskName.length() == 0){
Toast.makeText(this,"Type something as title.",Toast.LENGTH_SHORT).show();
}else {
int time = taskTime.getHour() * 60 + taskTime.getMinute();
DBOps.Ops.addNewTaskOp(
taskName.getText().toString().trim(),
time,
String.valueOf(isImp));
setAlarm();
finish();
}
}
public void setAlarm(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,taskTime.getHour());
calendar.set(Calendar.MINUTE,taskTime.getMinute());
calendar.set(Calendar.SECOND,0);
Intent intent = new Intent(this,NotificationReciever.class);
intent.putExtra("name",taskName.getText());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
这是示例代码,在查看您的代码后,您必须在 ViewHolder 中定义卡片
isImp = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_IS_IMP)));
if (isImp){
holder.card.setBackgroundColor(context.getResources().getColor(R.color.isImpColor));
} else {
//set the card normal color here
holder.card.setBackGroundColor(*insert your normal color here*);
}
RecyclerView 回收它的项目 ...所以直接改变一个项目的颜色可能会在滚动时改变其他项目的颜色..为了实现这个你需要应用一个小技巧(通过在模型 class.
中使用boolean isChecked
变量
查看此内容以供参考。
编辑
将您的 card
更改为 holder.card