Wie Artikel in RecyclerView sortieren auf einem Datum abhängig
Dies ist, was mein Bildschirm aussehen sollte.
Ich habe diese Bibliothek https://github.com/ShamylZakariya/StickyHeaders und versuchte, sie in ähnlicher Art und Weise zu tun.
Mein Modell sieht wie folgt aus:
public class Transaktion {
private int id;
private Date createdDate;
private String description;
private int amount;
private float newCredits;
}
und
public class Transactions {
private int totalCount;
private List<Transaction> transactions = new ArrayList<>();
}
So, nachdem ich alle meine Transaktionen hole ich die Daten in Adapterset .. das ist was mein Adapter wie folgt aussieht:
public class WalletAdapter extends SectioningAdapter {
private static final boolean USE_DEBUG_APPEARANCE = false;
private Transactions transactions;
private List<Section> sections = new ArrayList<>();
public WalletAdapter() {
}
private class Section {
String alpha;
Transactions transactions;
}
public Transactions getTransactions() {
return transactions;
}
public void setTransactions(Transactions transactions) {
this.transactions = transactions;
sections.clear();
char alpha = 0;
Section currentSection = null;
for (Transaction transaction : transactions.getTransactions()) {
String date = parseDate(transaction.getCreatedDate());
if (date.charAt(0) != alpha) {
if (currentSection != null) {
sections.add(currentSection);
}
currentSection = new Section();
alpha = date.charAt(0);
currentSection.alpha = String.valueOf(alpha);
}
if (currentSection != null) {
currentSection.transactions = this.transactions;
}
}
sections.add(currentSection);
notifyAllSectionsDataSetChanged();
}
private String parseDate(Date date) {
DateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
String formattedDate = "";
formattedDate = df.format(date);
return formattedDate;
}
@Override
public int getNumberOfSections() {
return sections.size();
}
@Override
public boolean doesSectionHaveHeader(int sectionIndex) {
return true;
}
@Override
public boolean doesSectionHaveFooter(int sectionIndex) {
return false;
}
@Override
public int getNumberOfItemsInSection(int sectionIndex) {
return sections.get(sectionIndex).transactions.getTransactions().size();
}
@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemUserType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.wallet_item, parent, false);
return new ItemViewHolder(v);
}
@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerUserType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.wallet_header, parent, false);
return new HeaderViewHolder(v);
}
@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
Section section = sections.get(sectionIndex);
ItemViewHolder holder = (ItemViewHolder) viewHolder;
Transaction transaction = section.transactions.getTransactions().get(itemIndex);
holder.description.setText(transaction.getDescription());
holder.ammount.setText(String.valueOf(transaction.getAmount()));
holder.time.setText(parseDate(transaction.getCreatedDate()));
holder.total.setText(String.valueOf(transaction.getNewCredits()));
}
@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
Section s = sections.get(sectionIndex);
HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
Transaction transaction = s.transactions.getTransactions().get(sectionIndex);
if (USE_DEBUG_APPEARANCE) {
hvh.itemView.setBackgroundColor(0x55ffffff);
hvh.dateHeader.setText(pad(sectionIndex * 2) + s.alpha);
} else {
hvh.dateHeader.setText(parseDate(transaction.getCreatedDate()));
}
}
private String pad(int spaces) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < spaces; i++) {
b.append(' ');
}
return b.toString();
}
public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
TextView dateHeader;
public HeaderViewHolder(View itemView) {
super(itemView);
dateHeader = (TextView) itemView.findViewById(R.id.date);
}
}
public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
TextView description;
TextView time;
TextView ammount;
TextView total;
public ItemViewHolder(View itemView) {
super(itemView);
description = (TextView) itemView.findViewById(R.id.description);
time = (TextView) itemView.findViewById(R.id.time);
ammount = (TextView) itemView.findViewById(R.id.ammount);
total = (TextView) itemView.findViewById(R.id.new_credits);
}
}
}
Also, wenn ich es auf diese Weise tun, Er sortiert und erstellt Header/sections nach Datum, aber es setzt alle Transaktionen in allen Bereichen, nicht im passenden Termin ...
Sie können sortieren Sie die Arraylist auf der Basis von Datum sortieren, bevor Sie und vergleichen Sie mit bestimmten Feld verwenden können Senden Sie es an den Adapter –
Es ist eigentlich sortiert und ich bekomme es in Kopfzeilen, aber das Problem ist, für jedes Datum muss ich nur Transaktionen an diesem Datum zu bekommen ... wenn ich 2 Transaktionen in einem Datum hätte, hätte ich 2 Artikel unter der Überschrift, genau wie auf dem Bild, das ich vergessen hochgeladen habe :) Ich werde meine Frage bearbeiten, so dass Sie sehen können – joe