我需要更新片段视图,以便我可以在值从我的数据库实时进入时更新我的回收视图。我目前在 OnCreateView 方法中执行此操作,但这不允许我在切换选项卡时进行更新。
public class UserFragment extends Fragment {
public UserFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(false);
final ArrayList<widgetData> dummie_list = new ArrayList<>();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
final MyAdapter adapter = new MyAdapter(dummie_list);
rv.setAdapter(adapter);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("contacts").child(mAuth.getUid());
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren())
{
if(!dummie_list.contains(new widgetData(ds.getValue(String.class), "FUCK FIREBASE", 0)))
{
dummie_list.add(new widgetData(ds.getValue(String.class), "FUCK FIREBASE", 0));
adapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Update view here
}
}
}
setUserVisibleHint 是导致问题的原因,因为似乎无法更新视图,因为我无权访问 OnCreateView 获取的参数。