没有出现RecyclerView

时间:2019-03-02 21:59:49

标签: java android firebase android-recyclerview

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);
}}

您能告诉我为什么没有出现RecyclerView吗?只是在打开键盘并关闭它时出现,我找不到原因。如果有必要,我可以通过隐藏键盘来记录RecyclerView的显示方式。我也尝试找到类似的问题,但找不到。

1 个答案:

答案 0 :(得分:1)

使用Firebase数据库和RecyclerView时,Firebase数据库要求将RecyclerView的固定大小设为false。我认为,因为Firebase数据库是异步的,这意味着RecyclerView将首先获得一个空适配器,并且在从数据库中获取数据之后,它将填充数据。

这会影响大小,因此如果将固定大小设置为true,它将无法计算大小,因此这就是为什么它需要固定大小的false的原因。因此解决方案是:mRecyclerView.setHasFixedSize(false);