1 回答
TA贡献1858条经验 获得超8个赞
在你的 onCreate 里面:
// Set up your RecyclerView with the appropriate Layout Manager
RecyclerView myRecycler = findViewById(R.id.my_recycler_id);
myRecycler.setLayoutManager(new LinearLayoutManager(this));
// Create your data set
myData = new ArrayList<MyDataType>();
// Create an instance of your adapter passing the data set into the constructor
myAdapter = new MyAdapter(this, myData);
// Set the Adapter on the RecyclerView directly within onCreate
// so that it doesn't get skipped
myRecycler.setAdapter(myAdapter);
在您的事件侦听器回调中:
@Override
public void onDataChange(DataSnapshot snapshot){
// Add the new data to your data set ex. myData.add(newData)
// ...
// After adding to the data set,
// update the data using a custom function you define in your Adapter's class
myAdapter.updateData(myData);
}
在 Adapter 类中,创建一个函数来更新 Adapter 的数据集:
public void updateData(ArrayList<MyDataType> newDataSet){
myAdapterDataSet = newDataSet;
// Let the Adapter know the data has changed and the view should be refreshed
notifyDataSetChanged();
}
添加回答
举报