简体   繁体   English

RecyclerView不会在使用FirebaseRecyclerAdapter的首次启动中加载数据

[英]RecyclerView doesn't load data in first launch using FirebaseRecyclerAdapter

I'm using FirebaseRecyclerAdapter to populate a RecyclerView in a Fragment . 我正在使用FirebaseRecyclerAdapterFragment填充RecyclerView

Here's my code 这是我的代码

mDatabase = FirebaseDatabase.getInstance().getReference();
myAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,
        R.layout.product_item,ProductViewHolder.class,
        mDatabase.child("clothes")) {
    @Override
    protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
        mProgressBar.setVisibility(ProgressBar.INVISIBLE);
        viewHolder.name.setText(model.name);
        viewHolder.price.setText(model.price);
        Glide.with(getActivity()).load(model.imageUri).into(viewHolder.thumbnail);
        Log.d("NAME", model.name);
    }
};

recyclerView.setAdapter(myAdapter);

The problem is, the ProgressBar keeps moving in the first launch, it never hides and the RecyclerView never shows itself but If I exit the app and launch again, the RecyclerView is properly populated, even if the screen locks itself and I unlock it, the RecyclerView is populated. 问题是, ProgressBar在第一次启动时一直在移动,它永远不会隐藏,并且RecyclerView永远不会显示自己,但是如果我退出应用程序并再次启动,即使屏幕锁定了自己并且我对其进行了解锁, RecyclerView也会正确填充。 RecyclerView已填充。 I'm confused. 我糊涂了。

Remove the recyclerView.setHasFixedSize(true) from your code and then check if the code works fine now. 从代码中删除recyclerView.setHasFixedSize(true) ,然后检查代码现在是否可以正常工作。

And for dismissing the ProgressBar , its good to set the visibility to GONE . 对于关闭ProgressBar ,将可见性设置为GONE很有好处。

mProgressBar.setVisibility(View.GONE);

For more information you can see this Github link . 有关更多信息,请参见此Github链接 I think the same issue is reported here. 我认为这里报告了同样的问题。

In your activity.xml file, set ProgressBar property 在您的activity.xml文件中,设置ProgressBar属性

android:visibility="invisible"

and in your populateViewHolder method, set mProgress.setVisibility(View.GONE); 在您的populateViewHolder方法中,设置mProgress.setVisibility(View.GONE); after setting data to TextViews & ImageView 将数据设置为TextViews和ImageView之后

 protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
        viewHolder.name.setText(model.name);
        viewHolder.price.setText(model.price);
        Glide.with(getActivity()).load(model.imageUri).into(viewHolder.thumbnail);
        mProgress.setVisibility(View.GONE);
        Log.d("NAME", model.name);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM