编辑 4
我放弃了下面的代码,它是垃圾。:( 请不要使用它。我重写了Firebase-UI 的数据库部分,所以请改用它。
编辑 1、2 和 3
由于addListenerForSingleValueEvent
意味着数据将是静态的(除非您更改键的值),这是我在不更改键值的情况下防止静态数据的方法:
创建事件侦听器和持有者对象的列表:
private Map<DatabaseReference, ValueEventListener> mValueEventListeners = new ArrayMap<>();
private List<TeamInfo> mTeamInfoList = new ArrayList<>();
FirebaseRecyclerAdapter
:_
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.content_main_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Long, TeamHolder>(
Long.class,
R.layout.activity_main_row_layout,
TeamHolder.class,
Utils.getDatabase()
.getReference()
.child(Constants.FIREBASE_TEAM_INDEXES)
.child(mUser.getUid())
.orderByValue()) {
@Override
public void populateViewHolder(final TeamHolder teamHolder,
Long teamNumberDoNotUse,
final int position) {
DatabaseReference databaseReference = Utils.getDatabase()
.getReference()
.child(Constants.FIREBASE_TEAMS)
.child(getRef(position).getKey());
if (!mValueEventListeners.containsKey(databaseReference)) {
ValueEventListener valueEventListener = new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
TeamInfo teamInfo = dataSnapshot.getValue(TeamInfo.class);
if (position >= mTeamInfoList.size()) {
String teamNumber = teamInfo.getNumber();
teamHolder.setTeamNumber(teamNumber);
teamHolder.setTeamName(teamInfo.getName(),
MainActivity.this.getString(R.string.no_name));
teamHolder.setTeamLogo(teamInfo.getMedia(), MainActivity.this);
teamHolder.setListItemClickListener(teamNumber,
MainActivity.this,
dataSnapshot.getKey());
teamHolder.setCreateNewScoutListener(teamNumber,
MainActivity.this,
dataSnapshot.getKey());
mTeamInfoList.add(teamInfo);
} else {
mTeamInfoList.set(position, teamInfo);
notifyItemChanged(position);
}
} else {
// This should never happen since we are caching this offline
FirebaseCrash.report(new IllegalStateException(
"MainActivity RecyclerView event listener had null value (ref: " + dataSnapshot.getRef() + "pointed to non existent data)"));
}
}
public void onCancelled(DatabaseError databaseError) {
FirebaseCrash.report(databaseError.toException());
}
};
databaseReference.addValueEventListener(valueEventListener);
mValueEventListeners.put(databaseReference, valueEventListener);
} else {
String teamNumber = mTeamInfoList.get(position).getNumber();
teamHolder.setTeamNumber(teamNumber);
teamHolder.setTeamName(mTeamInfoList.get(position).getName(),
MainActivity.this.getString(R.string.no_name));
teamHolder.setTeamLogo(mTeamInfoList.get(position).getMedia(), MainActivity.this);
teamHolder.setListItemClickListener(teamNumber, MainActivity.this, getRef(position).getKey());
teamHolder.setCreateNewScoutListener(teamNumber, MainActivity.this, getRef(position).getKey());
}
}
};
recyclerView.setAdapter(mRecyclerViewAdapter);
在你的OnDestroy
:
@Override
protected void onDestroy() {
super.onDestroy();
if (mRecyclerViewAdapter != null) {
mRecyclerViewAdapter.cleanup();
}
for (DatabaseReference databaseReference : mValueEventListeners.keySet()) {
databaseReference.removeEventListener(mValueEventListeners.get(databaseReference));
}
}