这个问题在这里有一个答案Recyclerview is not updated with new Data,但它对我不起作用。我不明白为什么有时RecyclerView显示项目,但有时它不会显示。我正在使用viewPager的四个框架。这不是互联网问题,因为其他片段显示来自互联网的数据。我尝试使用firebaseRecycler适配器,自定义适配器甚至listView代替recyclerView。 这是我的法典代码。请帮帮我。
public class FragmentTips extends Fragment {
public RecyclerView recyclerListView;
public MyRecyclerAdapter myAdapter;
DatabaseReference databaseReference;
TextView loading;
View v;
SwipeRefreshLayout refresher;
List<Model> listPosts = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_home, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference();
creatingLayouts();
final NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView);
adView.loadAd(new AdRequest.Builder().build());
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
}
});
return v;
}
public void creatingLayouts() {
refresher = (SwipeRefreshLayout) v.findViewById(R.id.refresher);
//refresher.setRefreshing(true);
refresher.setColorSchemeResources(R.color.blue, R.color.lightBlue, R.color.deepPurple, R.color.purple, R.color.pink, R.color.orange, R.color.red);
refresher.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (myAdapter != null) {
updateAdapter();
}
}
});
loading = (TextView) v.findViewById(R.id.loading);
recyclerListView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerListView.setHasFixedSize(false);
recyclerListView.setLayoutManager(new LinearLayoutManager(getActivity()));
myAdapter = new MyRecyclerAdapter(getContext());
updateAdapter();
recyclerListView.setAdapter(myAdapter);
}
//update adapter
public void updateAdapter() {
listPosts.clear();
databaseReference.child("winbet").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
listPosts.add(dataSnapshot.getValue(Model.class));
displayPosts(listPosts);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), "Canceled", Toast.LENGTH_SHORT).show();
}
});
}
public void displayPosts(List<Model> ls) {
refresher.setRefreshing(false);
loading.setVisibility(View.GONE);
recyclerListView.setVisibility(View.VISIBLE);
myAdapter.setData(ls);
myAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:1)
谢谢你们。我终于找到了解决方案。它与代码无关。如果有人遇到过这样的问题。问题是我使用的是免费的火力计划,并且允许的连接只有100个,所以它已达到峰值,因为我有很多用户。解决方案是升级