我在片段中有一个回收站视图 当我按返回上一个活动然后再返回时,回收者视图变为空
我尝试放置adapter.startListening();到onStart() 还尝试将stopListening()放到onStop()
回收站视图内容显示我何时第一次进入该片段或关闭该应用程序并再次打开它,但是当我按返回按钮并再次返回该片段时,回收站视图的所有内容都为空< / p>
这是我的代码:
public class EnrolledAboutEditFragment extends Fragment {
private FirebaseAuth mAuth;
private String mCurrentUserId;
private DatabaseReference mRootRef;
private RecyclerView mRecycler;
FirebaseRecyclerOptions<ModelAboutEnrolled> options;
FirebaseRecyclerAdapter<ModelAboutEnrolled, ViewHolderAboutEnrolled> adapter;
public EnrolledAboutEditFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
mCurrentUserId = mAuth.getCurrentUser().getUid();
mRootRef = FirebaseDatabase.getInstance().getReference();
mRootRef.keepSynced(true);
mView = inflater.inflate(R.layout.fragment_enrolled_about_edit,container,false);
mRecycler = mView.findViewById(R.id.f_about_recycler_view);
mRecycler.setHasFixedSize(true);
mRootRef.child("Users").child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String username = dataSnapshot.child("username").getValue().toString();
adapterFunction();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
}
public void adapterFunction(){
DatabaseReference reference = mRootRef.child("Contents").child(username);
options = new FirebaseRecyclerOptions.Builder<ModelAboutEnrolled>()
.setQuery(reference,ModelAboutEnrolled.class).build();
adapter = new FirebaseRecyclerAdapter<ModelAboutEnrolled, ViewHolderAboutEnrolled>(options) {
@Override
protected void onBindViewHolder(@NonNull final ViewHolderAboutEnrolled holder, int position, @NonNull final ModelAboutEnrolled model) {
Picasso.get().load(model.getImage()).into(holder.image);
}
@NonNull
@Override
public ViewHolderAboutEnrolled onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_about_enrolled,parent,false);
return new ViewHolderAboutEnrolled(view);
}
};
LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
mRecycler.setLayoutManager(llm);
adapter.startListening();
mRecycler.setAdapter(adapter);
}
}
答案 0 :(得分:0)
删除您的 setHasFixedSize() 方法。