FirebaseRecyclerAdapter - populateViewHolder第一次运行时是不是填充了数据?

时间:2016-09-01 10:09:00

标签: firebase android-recyclerview firebase-realtime-database firebaseui

FirebaseRecyclerAdapter - populateViewHolder第一次运行时没有填充数据,但是当我关闭应用并打开它时,数据会绑定在RecyclerView View Holder中。

我不知道为什么数据第一次没有填充,它显示空白屏幕

这是我的MainActivity

mDatabase = FirebaseDatabase.getInstance().getReference();
// [END create_database_reference]

mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
mRecycler.setHasFixedSize(true);
setSupportActionBar(toolbar);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);




// Set up FirebaseRecyclerAdapter with the Query
//Query postsQuery = mDatabase.child("EN").child("Courses") ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("EN").child("Courses");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Toast.makeText(MainActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
    }
});
Log.d("ref", String.valueOf(ref));
//   Log.d("Query", String.valueOf(postsQuery));
mAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(Course.class, R.layout.item_home,
        CourseViewHolder.class, ref) {
    @Override
    protected void populateViewHolder(final CourseViewHolder viewHolder, final Course model, final int position) {
        viewHolder.bindToPost(model);
    }

};
mRecycler.setAdapter(mAdapter);

ViewHolder

public class CourseViewHolder extends RecyclerView.ViewHolder {

    public TextView titleView;
    public TextView descriptionView;
    public ImageView IconView;
    public TextView lessonCountView;


    public CourseViewHolder(View itemView) {
        super(itemView);

        titleView = (TextView) itemView.findViewById(R.id.title);
        descriptionView = (TextView) itemView.findViewById(R.id.description);
        //IconView = (ImageView) itemView.findViewById(R.id.star);
    }

    public void bindToPost(Course post) {
      //  Log.d("Post", String.valueOf(post));
        titleView.setText(post.getName());
        descriptionView.setText(post.getDescription());

    }
}

我的模型Pojo

public class Course {

    public String description;
    public String title;

    public Course() {
        // Default constructor required for calls to DataSnapshot.getValue(Post.class)
    }

    public Course(String title, String description) {
      //  this.description = author;
        this.title = title;
        this.description =description;
    }

    public String getName() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}

2 个答案:

答案 0 :(得分:3)

找到解决方案。

问题是RecyclerView的高度为wrap_content。因此,请确保RecyclerView高度设置为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" />

答案 1 :(得分:2)

如果您不想使用match_parent,则不应致电mRecycler.setHasFixedSize(true);