2
我的Firebase控制檯中有以下結構。我正在嘗試讀取值並顯示所有用戶,但populateViewHolder永遠不會被調用。Firebase populateViewHolder永遠不會被調用
users
+OW5BYennVRXvfzOjfKpup9rZEYv2
-email: "[email protected]"
-username: "abc"
而在下面的代碼我收到的項目數爲1
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mAdapter.getItemCount();
int lastVisiblePosition =
mManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
recyclerView.scrollToPosition(positionStart);
}
}
});
這裏是我的代碼
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_home_page, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
Query postsQuery = mDatabase.child("users");
mAdapter = new FirebaseRecyclerAdapter<User, PostViewHolder>(User.class, R.layout.post,
PostViewHolder.class, postsQuery) {
@Override
protected void populateViewHolder(PostViewHolder viewHolder, User model, int position) {
final DatabaseReference postRef = getRef(position);
final String postKey = postRef.getKey();
viewHolder.bindToPost(model);
}
};
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mAdapter.getItemCount();
int lastVisiblePosition =
mManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
recyclerView.scrollToPosition(positionStart);
}
}
});
recyclerView.setAdapter(mAdapter);
}
佔位符:
public class PostViewHolder extends RecyclerView.ViewHolder {
public TextView title, emailt;
public PostViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
emailt = (TextView) view.findViewById(R.id.count);
}
public void bindToPost(User post, View.OnClickListener starClickListener) {
title.setText(post.username);
emailt.setText(post.email + "Likes");
}
public void bindToPost(User post) {
title.setText(post.username);
emailt.setText(post.email + "Likes");
}
}
奇怪,但如果我正在運行示例數據庫應用程序其工作正常。還有一個觀察,如果我正在調試應用程序,並在查詢的調試點等待2-3秒populateViewHolder被調用和視圖顯示..不知道爲什麼發生這種情況 – MRX
我真的不知道爲什麼會發生這種情況。事實證明,我沒有解決我的問題(這是不是在FRAGMETS工作)通過簡單地不使用片段。我也想找到一個解決方案 –
最好的人是這個弗蘭克van puffelen –