simpller
simpller

Reputation: 317

RecyclerView doesn't appear

Fragment.java

public class RuteEfectuateFragment extends Fragment {

View v;
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
private FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter;
final Context context = this.getContext();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    ((ProfileActivity) Objects.requireNonNull(getActivity())).setActionBarTitle("Rute efectuate");

    v = inflater.inflate(R.layout.fragment_rute_efectuate, container, false);

    mRecyclerView = v.findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mRef = mFirebaseDatabase.getReference("Rute");

    return v;
}

@Override
public void onStart() {
    super.onStart();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.card_layout, ViewHolder.class, mRef){
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, Model model, final int position) {
            viewHolder.setDetails(getContext(), model.getTaraPlecare(), model.getTaraDestinatie());

            viewHolder.itemView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    // Intent intent = new Intent(Fridge.this, Show.class);
                    //  startActivity(intent);
                }
            });
        }
    };

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}}

Viewholder.java

public class ViewHolder extends RecyclerView.ViewHolder {

private View mView;

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

    mView = itemView;
}

public void setDetails(Context ctx, String TaraPlecare, String TaraDestinatie){
    TextView mTaraPlecare = mView.findViewById(R.id.rTitleTv);
    TextView mTaraDestinatie = mView.findViewById(R.id.rDateTv);

    mTaraPlecare.setText(TaraPlecare);
    mTaraDestinatie.setText(TaraDestinatie);
}}

Can you tell me please why RecyclerView doesn't appear? It appears just when keyboard is opened and I close it and I can't find the reason. If it is necessary I can record how the RecyclerView appears by hiding the keyboard. I tried also tu find a similar question, but I couldn't find.

Upvotes: 1

Views: 54

Answers (1)

Yupi
Yupi

Reputation: 4470

When using Firebase database and RecyclerView, Firebase database requires to fixed size of RecyclerView be false. I think because Firebase Database is asynchronous and that means RecyclerView will get first an empty adapter and after data is fetched from database, it will be than populated with data.

That affects the size, so if you put fixed size to true it will fail to calculate the size, so that is why it needs false in fixed size. So solution is: mRecyclerView.setHasFixedSize(false);

Upvotes: 1

Related Questions