2017-02-22 99 views
1

我使用Firebase數據庫開發應用程序。我正在接收數據並上傳它沒有問題,但我想將數據添加到數據庫數組的頂部,而不是數據庫數組的底部。如何從recyclerview中的Firebase數據庫從底部到頂部檢索數據,或者如何在Firebase數據庫頂部添加數據?

所以建議我可以幫助我從底層到頂層檢索數據,或者我可以將其上傳到我的數據庫數組頂端。提前致謝。請幫助我。

+1

的StackOverflow是不是爲您提供的作品的代碼。你試過什麼了? – jhhoff02

+0

我嘗試** notifyItemInserted(0); **與FirebaseRecyclerAdapter,但不工作 – sam

回答

5

有幾種方法可以做到這一點。

第一:

實施FirebaseUI,然後執行以下操作:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
layoutManager.setReverseLayout(true); 
layoutManager.setStackFromEnd(true); 
recyclerView.setLayoutManager(layoutManager) 

二:

我更喜歡這種方式,特別是如果你計劃實施的一個GridLayoutManager未來。請注意,GridLayoutManager不支持setStackFromEnd();。以下將允許您在「AdapterView」中反轉任何數據。

在你RecyclerViewAdapter

@Override 
public Item getItem(int pos) { 
return super.getItem(getCount() - 1 - pos); 
} 
+0

是的,米與** GridLayoutManager **與FirebaseRecyclerAdapter **工作。你可以解釋如何把這個代碼?以及如何聲明** Item **。 m新手到android firebase ... !! – sam

+0

如果沒有你的原始文章有你的代碼真的不可能。您是否至少顯示來自Firebase的數據,您只需要以相反的順序顯示數據? –

+0

我只是想要反轉數據。 – sam

相關問題