当应用程序从后台进入前台时,片段recyclerview不会保存其状态

时间:2019-05-21 19:35:52

标签: android android-fragments firebase-realtime-database android-recyclerview bottomnavigationview

您好,我正在使用Firebase Recycler适配器在片段中填充Recyclerview视图

注意:该片段位于底部导航视图中。 从firebase实时数据库中加载内容是可以的,但是当应用程序进入后台并进入前台时,在recyclerview中进行一些滚动之后,recyclerview从第一项开始。

public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView.setHasFixedSize(true);
        mUserRef = FirebaseDatabase.getInstance().getReference().child("users");
        mRootRef = FirebaseDatabase.getInstance().getReference().child("images");
        options = new FirebaseRecyclerOptions.Builder<Model>()
                .setQuery(mRootRef, Model.class)
                .setLifecycleOwner(this)
                .build();

        firebaseRecyclerAdapter = new
                FirebaseRecyclerAdapter<Model, MyViewHolder>(options) {
                    @Override
                    public long getItemId(int position) {
                        return position;
                    }

                    @Override
                    public int getItemViewType(int position) {
                        return position;
                    }

                    @Override
                    protected void onBindViewHolder(@NonNull final MyViewHolder holder, int position, @NonNull final Model model) {
                        holder.loadImage(model.getUrl());

                        holder.downloadLink.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                    holder.downloadImage(getActivity(), model.getNumber(), model.getUrl());

                                }
                            }
                        });


                        }
                        holder.shareLink.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                holder.shareImage(getActivity(), model.getNumber(), model.getUrl());
                            }
                        });
                        holder.likeStatus(model.getNumber());
                        holder.downloadStatus(model.getNumber());
                        holder.favoriteLink.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                isLiked = true;
                                mUserRef.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                        if (isLiked.equals(true)) {
                                            if (dataSnapshot.child(user.getUid()).hasChild(model.getNumber())) {
                                                mUserRef.child(user.getUid()).child(model.getNumber()).child("postnumber").removeValue();
                                                isLiked = false;
                                            } else {
                                                mUserRef.child(user.getUid()).child(model.getNumber()).child("postnumber").setValue(model.getNumber());
                                                isLiked = false;
                                            }
                                        }
                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                    }
                                });
                            }
                        });
                    }

                    @NonNull
                    @Override
                    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                        View holderView = LayoutInflater.from(context).inflate(R.layout.row_item, viewGroup, false);
                        return new MyViewHolder(holderView);
                    }
                };
        linearLayoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(firebaseRecyclerAdapter);
        return view;
    }


}

请提出建议,为什么它不保存状态或我选择的过程有误?

0 个答案:

没有答案