【问题标题】:Firebase populateViewHolder is never calledFirebase populateViewHolder 永远不会被调用
【发布时间】:2016-12-07 03:48:00
【问题描述】:

我的 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");
}
}

【问题讨论】:

  • 奇怪,但如果我正在运行示例数据库应用程序,它工作正常。如果我正在调试应用程序并在 Query 的调试点等待 2-3 秒 populateViewHolder 被调用并且视图显示..不知道为什么会这样
  • 我真的不知道为什么会这样。事实证明,我并没有通过简单地不使用片段来解决我的问题(这在 FRAGMETS 中不起作用)。我也想找到解决办法
  • 最好的人选是 Frank van Puffelen
  • 你成功了@MRX请帮助stackoverflow.com/questions/39268663/…

标签: android firebase-realtime-database firebaseui


【解决方案1】:

问题是 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"
            />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多