[英]Dynamic number of tabs with firebase android
I have a group of category strings stored in FirebaseFirestore.
我在FirebaseFirestore.
存储了一组类别字符串FirebaseFirestore.
Now, i want to show this list of strings with tabs using a tabLayout.
现在,我想使用tabLayout.
显示带有选项卡的字符串列表tabLayout.
This means that the number of tabs would be dynamic which would be set according to whats present on the database.这意味着选项卡的数量将是动态的,这将根据数据库中存在的内容进行设置。 For now, i know only how to show fixed number of tabs using this line of code:现在,我只知道如何使用这行代码显示固定数量的选项卡:
TabLayout tabLayout = findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
So, how do i make the tabs dynamic such that the text and number of tabs are set from whats coming from the list of strings from my database?那么,我如何使选项卡动态化,以便根据来自我的数据库中的字符串列表的内容来设置选项卡的文本和数量? Please help.请帮忙。
Say that each tab is stored in a document in Firestore in a collection named tabs
.假设每个选项卡都存储在 Firestore 中名为tabs
的集合中的文档中。 You can then get all documents in that collection and create a tab with the ID of each document with:然后,您可以获取该集合中的所有文档,并使用每个文档的 ID 创建一个选项卡:
db.collection("tabs")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
tabLayout.addTab(tabLayout.newTab().setText(document.getId()));
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.