Last active
July 8, 2018 19:23
-
-
Save marcusedu/4a3fa3bcb7a35f05daf57d9650a5e9b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExemploPaginacaoFirebaseDoUltimoParaOPrimeiro{ | |
private DatabaseReference listaRef = FirebaseDatabase.getInstance().getReference("suaLista"); | |
private int numItensPorPagina = 15; | |
private ArrayList<SeuItem> itens = new ArrayList(); | |
private ChildEventListener meuChildEventListener = new ChildEventListener() { | |
@Override | |
public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
// Aqui ele registra a ultima key recebida e | |
// compara se a key atual é mais velha se for guardamos ela | |
if (ultimoItem == null) | |
ultimoItem = dataSnapshot.getKey(); | |
else if (ultimoItem.compareTo(dataSnapshot.getKey()) < 0) | |
ultimoItem= dataSnapshot.getKey(); | |
SeuItem seuItem = dataSnapshot.getValue(SeuItem.class); | |
itens.add(seuItem); | |
} | |
@Override | |
public void onChildChanged(DataSnapshot dataSnapshot, String s) {} | |
@Override | |
public void onChildRemoved(DataSnapshot dataSnapshot) {} | |
@Override | |
public void onChildMoved(DataSnapshot dataSnapshot, String s) {} | |
@Override | |
public void onCancelled(DatabaseError databaseError) {} | |
}; | |
public void recuperarLista(){ | |
//Aqui simplesmente recupera os itens da lista com limite de items | |
listaRef.orderByKey() | |
.limitToFirst(numItensPorPagina)// Essa consulta começa do ultimo item para o primeiro | |
.addChildEventListener(meuChildEventListener); | |
} | |
public void recuperarMaisItensDaLista(){ | |
if (ultimoItem != null) | |
listaRef.orderByKey() | |
.startAt(ultimoItem) // Quando precisamos de recuperar mais itens é preciso ter | |
// a referencia de qual foi o utimo item recuperado | |
.limitToFirst(numItensPorPagina)//Aqui tem que coincidir com a consulta original | |
.addChildEventListener(meuChildEventListener); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExemploPaginacaoFirebaseDoUltimoParaOPrimeiro{ | |
private DatabaseReference listaRef = FirebaseDatabase.getInstance().getReference("suaLista"); | |
private int numItensPorPagina = 15; | |
private ArrayList<SeuItem> itens = new ArrayList(); | |
private ChildEventListener meuChildEventListener = new ChildEventListener() { | |
@Override | |
public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
// Aqui ele registra a ultima key recebida e | |
// compara se a key atual é mais recente se for guardamos ela | |
if (ultimoItem == null) | |
ultimoItem = dataSnapshot.getKey(); | |
else if (ultimoItem.compareTo(dataSnapshot.getKey()) > 0) | |
ultimoItem= dataSnapshot.getKey(); | |
SeuItem seuItem = dataSnapshot.getValue(SeuItem.class); | |
itens.add(seuItem); | |
} | |
@Override | |
public void onChildChanged(DataSnapshot dataSnapshot, String s) {} | |
@Override | |
public void onChildRemoved(DataSnapshot dataSnapshot) {} | |
@Override | |
public void onChildMoved(DataSnapshot dataSnapshot, String s) {} | |
@Override | |
public void onCancelled(DatabaseError databaseError) {} | |
}; | |
public void recuperarLista(){ | |
//Aqui simplesmente recupera os itens da lista com limite de items | |
listaRef.orderByKey() | |
.limitToLast(numItensPorPagina)// Essa consulta começa do ultimo item para o primeiro | |
.addChildEventListener(meuChildEventListener); | |
} | |
public void recuperarMaisItensDaLista(){ | |
if (ultimoItem != null) | |
listaRef.orderByKey() | |
.endAt(ultimoItem) // Quando precisamos de recuperar mais itens é preciso ter | |
//a referencia de qual foi o utimo item recuperado | |
.limitToLast(numItensPorPagina)//Aqui tem que coincidir com a consulta original | |
.addChildEventListener(meuChildEventListener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment