使用Firebase的数据在片段中制作RecycleView
时遇到问题。我希望该应用程序在单击按钮以从一个片段更改为RecycleView
片段后显示RecycleView
,但是它确实更改了所显示的片段,但未显示任何内容。
我知道有很多这样的问题,但是我似乎没有找到解决该问题的正确方法。
我已经制作了Firebase RecyclerView所需的所有内容,并且还尝试将其构建在活动而不是片段内,并且确实可以,但是不能与片段一起使用。 我已经尝试在onCreateView方法,onActivityCreated和onViewCreated方法中初始化适配器和recyclerview,但它们似乎都不起作用。
这是我的片段代码:
private KidAdapter adapter;
private RecyclerView recyclerView;
Button button;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.uangsaku_menu_fragment, container, false);
button = view.findViewById(R.id.btn_add);
button.setOnClickListener(this);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
setUpRecyclerView();
return view;
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_add){
Intent intent = new Intent(getActivity(), Register.class);
startActivity(intent);
}
}
public void setUpRecyclerView(){
Query query = FirebaseDatabase.getInstance().getReference("kids");
FirebaseRecyclerOptions<kidData> options = new FirebaseRecyclerOptions.Builder<kidData>()
.setQuery(query, kidData.class)
.build();
adapter = new KidAdapter(options);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
if (adapter != null) {
adapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
适配器类
public class KidAdapter extends FirebaseRecyclerAdapter<kidData, KidAdapter.KidViewHolder> {
public KidAdapter(@NonNull FirebaseRecyclerOptions<kidData> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull KidViewHolder holder, int position, @NonNull kidData model) {
holder.nama.setText(model.getKidName());
holder.balance.setText(model.getKidBalance());
holder.limit.setText("Limit: "+model.getKidLimit());
holder.spending.setText("Spending xxx.xxx");
}
@NonNull
@Override
public KidViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.kids_card,
viewGroup, false);
return new KidViewHolder(v);
}
public class KidViewHolder extends RecyclerView.ViewHolder {
TextView nama, balance, limit, spending;
public KidViewHolder(@NonNull View itemView) {
super(itemView);
nama = itemView.findViewById(R.id.tv_nama);
balance = itemView.findViewById(R.id.tv_balance);
limit = itemView.findViewById(R.id.tv_dailylimit);
spending = itemView.findViewById(R.id.tv_dailyspending);
}
}
}
kidData模型类
public class kidData {
String kidName, kidEmail, kidDoB, kidLimit, kidBalance;
public kidData(){
}
public kidData(String kidName, String kidEmail, String kidDoB, String kidLimit, String kidBalance) {
this.kidName = kidName;
this.kidEmail = kidEmail;
this.kidDoB = kidDoB;
this.kidLimit = kidLimit;
this.kidBalance = kidBalance;
}
public String getKidName() {
return kidName;
}
public String getKidEmail() {
return kidEmail;
}
public String getKidDoB() {
return kidDoB;
}
public String getKidLimit() {
return kidLimit;
}
public String getKidBalance() {
return kidBalance;
}
}
答案 0 :(得分:2)
您的代码中的问题是使用以下代码行:
recyclerView.setHasFixedSize(true);
这是因为使用最新版本的Firebase-UI
库时,无需将RecyclerView
的大小设置为固定值。解决此问题的方法是简单地删除/注释上述代码行。就是这样!