为什么片段中的 setVisibility 不起作用?
why setVisibility in fragment doesn't work?
ProgressBar 不会在片段中消失,项目也不会加载到 RecyclerView 中,当代码在主 activity 的 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
在您的 CreateView 中,更改
return inflater.inflate(R.layout.fragment_offers, container, false);
来自
return fragment_layout;
你调用了布局,init recyclerview,但是回忆一下"blank"布局
此外,避免在方法或变量名称中使用下划线,这不是 java 约定
@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
的实例。这很好。
但是在该方法的末尾,您正在膨胀一个新布局并 return 对其进行处理。因此 Android 框架使用 View
而不是 fragment_layout
来设置片段视图。 fragment_layout
中的所有视图都不会显示,因为它从未添加到片段中,因此您在 RecyclerView 中看不到任何变化。
在 onCreateView
方法的最后,你应该 return fragment_layout
.
ProgressBar 不会在片段中消失,项目也不会加载到 RecyclerView 中,当代码在主 activity 的 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
在您的 CreateView 中,更改
return inflater.inflate(R.layout.fragment_offers, container, false);
来自
return fragment_layout;
你调用了布局,init recyclerview,但是回忆一下"blank"布局
此外,避免在方法或变量名称中使用下划线,这不是 java 约定
@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
的实例。这很好。
但是在该方法的末尾,您正在膨胀一个新布局并 return 对其进行处理。因此 Android 框架使用 View
而不是 fragment_layout
来设置片段视图。 fragment_layout
中的所有视图都不会显示,因为它从未添加到片段中,因此您在 RecyclerView 中看不到任何变化。
在 onCreateView
方法的最后,你应该 return fragment_layout
.