1 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超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);
在您的事件偵聽器回調(diào)中:
@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 類中,創(chuàng)建一個函數(shù)來更新 Adapter 的數(shù)據(jù)集:
public void updateData(ArrayList<MyDataType> newDataSet){
myAdapterDataSet = newDataSet;
// Let the Adapter know the data has changed and the view should be refreshed
notifyDataSetChanged();
}
添加回答
舉報