我尝试使用SwipeRefreshlayout更新Recyclerview中的所有项目, 但不幸的是,只会更新最后一项,其他项目保持不变。
以下是我的相关代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//LeakCanary.install(this.getApplication());
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
}
handleArrayList(getApplicationContext(), 0);
Log.d("Arraylist", "" + intentNumber2.size());
recyclerView = (RecyclerView) findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(null);
recyclerView.setHasFixedSize(true);
SimpleAdapter adapter = new SimpleAdapter();
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), AddReminder.class);
startActivity(intent);
}
});
adapter.notifyItemInserted(cards.size() - 1);
refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
updateRemainingTime();
refreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
updateRemainingTime();
}
//refresh the time remaining on the cards
private void updateRemainingTime() {
if (!cards.isEmpty()) {
for (int i = cards.size()-1; i >= 0; i--) {
Card card = cards.get(i);
String time = card.getCardTime();
String date = card.getCardDate();
Date now = new Date();
Date then = null;
try {
then = sdtf.parse(date + " " + time);
} catch (ParseException e) {
Log.d("PARSEEXCEPTION:", " " + e);
}
String remainingTime = calculate.calcTimeDiff(then.getTime(), now.getTime());
Log.d("Card","Card updated " + remainingTime);
card.cardRemainingTime(remainingTime);
recyclerView.getAdapter().notifyItemChanged(i);
}
refreshLayout.setRefreshing(false);
} else {
refreshLayout.setRefreshing(false);
}
}
我的适配器:
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> implements RemoveItem {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item_main, parent, false);
return new ViewHolder(itemView, this);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Card card = cards.get(position);
holder.setReminderText(card.getCardText());
card_tv2.setText(card.getCardRemainingTime());
holder.setSelectable(true);
}
@Override
public int getItemCount() {
return cards.size();
}
@Override
public void remove(int position) {
cards.remove(position);
Log.d("Main", "removed " + position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, cards.size());
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(getBaseContext(), AlarmReceiver.class);
int intentNumber = intentNumber2.get(position);
intentNumber2.remove(position);
Log.d("IntentNumber", "" + intentNumber);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), intentNumber, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
alarmIntent.cancel();
alarmManager.cancel(alarmIntent);
File dir = getFilesDir();
File file = new File(dir, "Reminder" + " " + position);
boolean deleted = file.delete();
MainActivity.handleArrayList(getApplicationContext(), 1);
}
public class ViewHolder extends SwappingHolder implements View.OnClickListener, View.OnLongClickListener {
public final LinearLayout background;
public ViewHolder(View itemView, RemoveItem removeItem1) {
super(itemView, multiSelector);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
itemView.setLongClickable(true);
removeItem = removeItem1;
card_tv = (TextView) itemView.findViewById(R.id.card_tv);
card_tv2 = (TextView) itemView.findViewById(R.id.card_tv2);
card_th = (ImageView) itemView.findViewById(R.id.thumbnail);
card_check = (ImageView) itemView.findViewById(R.id.card_check);
background = (LinearLayout) itemView.findViewById(R.id.background);
}
@Override
public void onClick(View view) {
if (multiSelector.tapSelection(this)) {
background.setSelected(true);
} else {
int position = getAdapterPosition();
String text = "error";
String time = "error";
String date = "error";
String repeat;
boolean repeat2 = false;
String quantity = "error";
String mode = "error";
try {
InputStream inputStream = openFileInput("Reminder" + " " + position);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
text = bufferedReader.readLine();
time = bufferedReader.readLine();
date = bufferedReader.readLine();
repeat = bufferedReader.readLine();
repeat2 = repeat.equals("true");
quantity = bufferedReader.readLine();
mode = bufferedReader.readLine();
inputStreamReader.close();
bufferedReader.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent2 = new Intent(getBaseContext(), ChangeReminder.class);
intent2.putExtra("POSITION", position);
intent2.putExtra("TIME", time);
intent2.putExtra("DATE", date);
intent2.putExtra("TEXT", text);
intent2.putExtra("REPEAT", repeat2);
intent2.putExtra("QUANTITY", quantity);
intent2.putExtra("MODE", mode);
startActivity(intent2);
}
}
@Override
public boolean onLongClick(View view) {
AppCompatActivity activity = MainActivity.this;
activity.startSupportActionMode(mDeleteMode);
multiSelector.setSelectable(true);
multiSelector.setSelected(this, true);
background.setSelected(true);
return true;
}
public void setReminderText(String cardText) {
card_tv.setText(cardText);
ColorGenerator colorGenerator = ColorGenerator.MATERIAL;
TextDrawable drawableBuilder;
String letter = "A";
if (cardText != null && !cardText.isEmpty()) {
letter = cardText.substring(0, 1);
}
int color = colorGenerator.getRandomColor();
drawableBuilder = TextDrawable.builder()
.buildRound(letter, color);
card_th.setImageDrawable(drawableBuilder);
}
}
}
}
卡类:
public class Card implements Serializable {
public int drawable;
private String cardText;
private String cardDate;
private String cardTime;
private String cardRemainingTime;
public void cardText(String cardText) {
this.cardText = cardText;
}
public void cardDate(String cardDate) {
this.cardDate = cardDate;
}
public void cardTime(String cardTime) {
this.cardTime = cardTime;
}
public void cardRemainingTime(String cardRemainingTime) {
this.cardRemainingTime = cardRemainingTime;
}
public String getCardText() {
return cardText;
}
public String getCardDate() {
return cardDate;
}
public String getCardTime() {
return cardTime;
}
public String getCardRemainingTime() {
return cardRemainingTime;
}
}
编辑: 我发现当我重新打开应用程序时,时间会正确更新所有项目。 这意味着从onRefresh和onCreate调用updateRemainingTime之间必须有区别,但我不知道哪个。任何帮助将不胜感激。
答案 0 :(得分:0)
在updateRemainingTime方法中进行更改。
if (!cards.isEmpty()) {
for (int i = cards.size() - 1; i >= 0; i--) {
Card card = cards.get(i);
String time = card.getCardTime();
String date = card.getCardDate();
Date now = new Date();
Date then = null;
try {
then = sdtf.parse(date + " " + time);
} catch (ParseException e) {
Log.d("PARSEEXCEPTION:", " " + e);
}
String remainingTime = calculate.calcTimeDiff(then.getTime(), now.getTime());
Log.d("Card", "Card updated " + remainingTime);
//card.cardRemainingTime(remainingTime);
//replaces to
cards.get(i).cardRemainingTime(remainingTime);
}
List<Card> cardTemp = new ArrayList<>();
cardTemp = cards;
cards.clear();
cards.addAll(cardTemp);
recyclerView.notifyDataSetChanged();
}