如何创建按类别显示数据的多回收视图
how to create multi recyclerview that displays data per category
在我的项目中,我想按类别查询显示从 firebase 到 recycleview 的信息,例如:
布局 1 包含菜单查询 A
布局 2 包含一个菜单查询 B
等等
这是我的代码
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_post_list);
// ActionBar actionBar = getSupportActionBar ();
// actionBar.setTitle ("post list");
mRecyclerView = findViewById (R.id.recyclerView);
mRecyclerView.setHasFixedSize (true);
mRecyclerView.setLayoutManager (new LinearLayoutManager (this));
mFirebaseDatabase = FirebaseDatabase.getInstance ();
mRef = mFirebaseDatabase.getReference ("Data / Menu1"); //this section takes queries from firebase addresses
}
this is my firebase query
如何在不制作很多的情况下制作多个回收视图类?
Android 开发教程 - Recycler View in Recycler View like Google Play using Firebase Realtime Database.
希望本视频对您有所帮助。
https://www.youtube.com/watch?v=K2FSLZzYnnQ&t=1s
要按类别显示数据,您可以使用分段适配器。请看一个例子
https://github.com/luizgrp/SectionedRecyclerViewAdapter
您可以试试这个 activity 作为入门示例
SampleActivity.java
public class SampleActivity extends AppCompatActivity {
DatabaseReference mRef;
FirebaseDatabase mFirebaseDatabase;
RecyclerView mRecyclerView;
ArrayList<InnerDataModel> innerDataModelsMenu1ForAdapter;
ArrayList<InnerDataModel> innerDataModelsMenu2ForAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mRecyclerView = findViewById(R.id.recyclerView);
innerDataModelsMenu1ForAdapter = new ArrayList<>();
innerDataModelsMenu2ForAdapter = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
getDataFromFirestore();
}
private void getDataFromFirestore() {
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data/");
//Get all the data from the Firbase Realtime Database
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String menuType = snapshot.getKey();
if (!TextUtils.isEmpty(menuType)) {
//Write logic for each different menu type
ArrayList<InnerDataModel> innerDataModelsMenu1 = new ArrayList<InnerDataModel>();
ArrayList<InnerDataModel> innerDataModelsMenu2 = new ArrayList<InnerDataModel>();
if (menuType.equals("Menu1")) {
for (DataSnapshot snapshotInner : dataSnapshot.child("Menu1").getChildren()) {
innerDataModelsMenu1.add(snapshotInner.getValue(InnerDataModel.class));
}
runCodeForMenu1(innerDataModelsMenu1);
}
if (menuType.equals("Menu2")) {
for (DataSnapshot snapshotInner : dataSnapshot.child("Menu2").getChildren()) {
innerDataModelsMenu2.add(snapshotInner.getValue(InnerDataModel.class));
}
runCodeForMenu2(innerDataModelsMenu2);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//No Data present
}
});
}
private void runCodeForMenu2(ArrayList<InnerDataModel> children) {
innerDataModelsMenu2ForAdapter.addAll(children);
Log.d("SampleActivity", innerDataModelsMenu2ForAdapter.toString() + "");
}
private void runCodeForMenu1(ArrayList<InnerDataModel> children) {
innerDataModelsMenu1ForAdapter.addAll(children);
Log.d("SampleActivity", innerDataModelsMenu1ForAdapter.toString() + "");
}
}
InnerDataModel.java
public class InnerDataModel {
private String image;
private String title;
private String description;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
从 FireBase 接收并处理数据后,您可以根据两个全局 ArrayLists
innerDataModelsMenu1ForAdapter
和 innerDataModelsMenu2ForAdapter
处理您的适配器,并在您的 activity 显示 CardView
中的数据
在我的项目中,我想按类别查询显示从 firebase 到 recycleview 的信息,例如:
布局 1 包含菜单查询 A 布局 2 包含一个菜单查询 B 等等
这是我的代码
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_post_list);
// ActionBar actionBar = getSupportActionBar ();
// actionBar.setTitle ("post list");
mRecyclerView = findViewById (R.id.recyclerView);
mRecyclerView.setHasFixedSize (true);
mRecyclerView.setLayoutManager (new LinearLayoutManager (this));
mFirebaseDatabase = FirebaseDatabase.getInstance ();
mRef = mFirebaseDatabase.getReference ("Data / Menu1"); //this section takes queries from firebase addresses
}
this is my firebase query
如何在不制作很多的情况下制作多个回收视图类?
Android 开发教程 - Recycler View in Recycler View like Google Play using Firebase Realtime Database.
希望本视频对您有所帮助。 https://www.youtube.com/watch?v=K2FSLZzYnnQ&t=1s
要按类别显示数据,您可以使用分段适配器。请看一个例子 https://github.com/luizgrp/SectionedRecyclerViewAdapter
您可以试试这个 activity 作为入门示例
SampleActivity.java
public class SampleActivity extends AppCompatActivity {
DatabaseReference mRef;
FirebaseDatabase mFirebaseDatabase;
RecyclerView mRecyclerView;
ArrayList<InnerDataModel> innerDataModelsMenu1ForAdapter;
ArrayList<InnerDataModel> innerDataModelsMenu2ForAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mRecyclerView = findViewById(R.id.recyclerView);
innerDataModelsMenu1ForAdapter = new ArrayList<>();
innerDataModelsMenu2ForAdapter = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
getDataFromFirestore();
}
private void getDataFromFirestore() {
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data/");
//Get all the data from the Firbase Realtime Database
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String menuType = snapshot.getKey();
if (!TextUtils.isEmpty(menuType)) {
//Write logic for each different menu type
ArrayList<InnerDataModel> innerDataModelsMenu1 = new ArrayList<InnerDataModel>();
ArrayList<InnerDataModel> innerDataModelsMenu2 = new ArrayList<InnerDataModel>();
if (menuType.equals("Menu1")) {
for (DataSnapshot snapshotInner : dataSnapshot.child("Menu1").getChildren()) {
innerDataModelsMenu1.add(snapshotInner.getValue(InnerDataModel.class));
}
runCodeForMenu1(innerDataModelsMenu1);
}
if (menuType.equals("Menu2")) {
for (DataSnapshot snapshotInner : dataSnapshot.child("Menu2").getChildren()) {
innerDataModelsMenu2.add(snapshotInner.getValue(InnerDataModel.class));
}
runCodeForMenu2(innerDataModelsMenu2);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//No Data present
}
});
}
private void runCodeForMenu2(ArrayList<InnerDataModel> children) {
innerDataModelsMenu2ForAdapter.addAll(children);
Log.d("SampleActivity", innerDataModelsMenu2ForAdapter.toString() + "");
}
private void runCodeForMenu1(ArrayList<InnerDataModel> children) {
innerDataModelsMenu1ForAdapter.addAll(children);
Log.d("SampleActivity", innerDataModelsMenu1ForAdapter.toString() + "");
}
}
InnerDataModel.java
public class InnerDataModel {
private String image;
private String title;
private String description;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
从 FireBase 接收并处理数据后,您可以根据两个全局 ArrayLists
innerDataModelsMenu1ForAdapter
和 innerDataModelsMenu2ForAdapter
处理您的适配器,并在您的 activity 显示 CardView