gpt4 book ai didi

java - RecyclerView 没有出现

转载 作者:行者123 更新时间:2023-11-29 23:18:04 25 4
gpt4 key购买 nike

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 的显示方式。我也试过找到类似的问题,但找不到。

最佳答案

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

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

关于java - RecyclerView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54963436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com