Search code examples
androidfirebasefirebase-realtime-databaseandroid-databindingfirebaseui

FirebaseRecyclerAdapter doesnt work with databindings


I'm using RecyclerView with databindings but when I run the app the first time nothing is showing up then after update some content or update the app via instant Run the content appears.

my ViewHolder:

class MyViewHolder extends RecyclerView.ViewHolder {
    private ItemBinding mBinding;

    public MyViewHolder(View itemView) {
        super(itemView);
        mBinding = DataBindingUtil.bind(itemView);
    }

    ItemBinding getBinding() {
        return mBinding;
    }
}

my Adapter:

public class MyAdapter extends FirebaseRecyclerAdapter<MyModel, MyViewHolder> {
    public MyAdapter(Query ref) {
        super(MyModel.class, R.layout.my_item, MyViewHolder.class, ref);
    }

    @Override
    protected void populateViewHolder(MyViewHolder viewHolder, MyModel model, int position) {
        ItemBinding binding = viewHolder.getBinding();
        binding.setMyModel(model);
        binding.executePendingBindings();
    }
}

I found in some other question I need call binding.executePendingBindings() that was I did without success.

Edit

I just added a log call:

Log.d(BuildConfig.TAG, "called populateViewHolder " + position);

on the populateViewHolder method. The log is never printed.

Edit 2

The way how I'm initializing my recyclerView:

// onCreate
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view)
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(llm);
mRef = FirebaseDatabase.getInstance().getReference().child(CHILD_TREE)    

// onStart    
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);

Solution

  • From my comment:

    Have you already looked at the firebase/FirebaseUI-Android issue..? Setting the RecyclerView height from wrap_contentto match_parent solved the problem there.

    Apparently, there really is a problem with the height set to wrap_content in the RecyclerView when using the FirebaseRecyclerAdapter with DataBinding.

    Change it to

    android:layout_height="match_parent"
    

    and it should work.

    The FirebaseUI-Android Team thinks that this is a problem with the RecyclerView itself and has closed the issue. This is the crosspost from the Github Issue.