gpt4 book ai didi

android - 永远不会调用 Firebase populateViewHolder

转载 作者:太空狗 更新时间:2023-10-29 13:56:57 24 4
gpt4 key购买 nike

我的 firebase 控制台中有以下结构。我正在尝试读取值并显示所有用户,但从未调用 populateViewHolder。

   users
+OW5BYennVRXvfzOjfKpup9rZEYv2
-email: "abc@gmail.com"
-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");
}
}

最佳答案

问题是 RecyclerView 的高度为 WrapContent ,因此请确保您的回收站高度设置为 Match_Parent。这将解决此问题。

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>

关于android - 永远不会调用 Firebase populateViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com