ProgressBar不会在片段中消失,也不会在RecyclerView中加载项目,当代码位于主要活动的onCreate主体中时,它就可以工作
我正在制作包含ProgressBar和RecyclerView的片段,并从firebase数据库中检索数据,我在onDataChange方法的末尾添加了setVisibility,以便在获取所有数据并将其存储在数组列表中之后,ProgressBar消失
RecyclerView offersRecycler;
ArrayList<offer> offers;
OffersAdapter offersAdapter;
View fragment_layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
private void offers_init() {
DatabaseReference db_offers = db_ref().child("offers");
db_offers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
ProgressBar loadingOffers = fragment_layout.findViewById(R.id.loadingOffers);
loadingOffers.setProgress(10);
for (DataSnapshot offer_item : dataSnapshot.getChildren()) {
String id = offer_item.getKey();
String title = offer_item.child("title").getValue().toString();
String rating = offer_item.child("rating").getValue().toString();
String orders_number = offer_item.child("orders_number").getValue().toString();
offer offer_object = new offer(id,title, rating, orders_number);
offers.add(offer_object);
offersAdapter.notifyDataSetChanged();
}
Toast.makeText(getContext(),"title",Toast.LENGTH_LONG).show();
loadingOffers.setVisibility(View.GONE);
}catch (Exception ex){
Toast.makeText(getContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
我希望ProgressBar隐藏并且将数组加载到RecyclerView
答案 0 :(得分:2)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
^这是您的代码段。您正在做的是-扩大布局fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false)
并使用fragment_layout
获取RecyclerView
的实例。很好。
但是在方法的最后,您要膨胀一个新的布局并返回它。因此,Android框架使用该View
而不是fragment_layout
来设置Fragment视图。 fragment_layout
中的所有视图都不会显示,因为它从未添加到片段中,因此您在RecyclerView中看不到任何更改。
在onCreateView
方法的末尾,您应该只返回fragment_layout
。
答案 1 :(得分:1)
在您的CreateView中,更改
return inflater.inflate(R.layout.fragment_offers, container, false);
作者
return fragment_layout;
您将布局称为init recyclerview,但调用了“空白”布局
此外,请避免在方法或变量名下加下划线,这不是Java约定