RecyclerView doesn't load data in first launch using FirebaseRecyclerAdapter

为君一笑 提交于 2019-12-20 03:33:15

问题


I'm using FirebaseRecyclerAdapter to populate a RecyclerView in a Fragment.

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. I'm confused.


回答1:


Remove the recyclerView.setHasFixedSize(true) from your code and then check if the code works fine now.

And for dismissing the ProgressBar, its good to set the visibility to GONE.

mProgressBar.setVisibility(View.GONE);

For more information you can see this Github link. I think the same issue is reported here.




回答2:


In your activity.xml file, set ProgressBar property

android:visibility="invisible"

and in your populateViewHolder method, set mProgress.setVisibility(View.GONE); after setting data to 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);
    }


来源:https://stackoverflow.com/questions/41429574/recyclerview-doesnt-load-data-in-first-launch-using-firebaserecycleradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!