我在片段内有回收器视图当我按回去进行上一个活动然后返回时,回收器视图将变为空
我尝试将apapter.startlistening();到onstart()还尝试将戒烟()放在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);
}
}
删除您的sethasfixedsize()方法。