Firebase RecyclerView - 添加新项目时滚动动画

时间:2016-12-20 03:17:33

标签: android firebase android-recyclerview firebase-realtime-database firebaseui

我正在使用位于hereFirebaseRecyclerAdapter文档。

基本上,我想滚动到RecyclerView的顶部,因为项目已添加到Firebase中。这很棘手,因为我实际上并没有使用ArrayList或任何列表初始化适配器。 Firebase通过FirebaseRecyclerAdapter处理所有数据管理。

这是我的基本onCreateView()

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.fragment_new, container, false);
    Log.v("TAG", "ON CREATE CALLED FROM NEW");

    mRecyclerview = (RecyclerView) v.findViewById(R.id.list);
    mRecyclerview.setItemAnimator(new FadeInUpAnimator());


    mLayoutManager = new LinearLayoutManager(getContext());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);

    mRecyclerview.setLayoutManager(mLayoutManager);

    return v;
}

我的适配器:

    @Override
public void onStart() {
    super.onStart();

    mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
        @Override
        protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) {
            mRecyclerview.scrollToPosition(position);
            viewHolder.mPollQuestion.setText(model.getQuestion());
            Picasso.with(getActivity().getApplicationContext())
                    .load(model.getImage_URL())
                    .fit()
                    .into(viewHolder.mPollImage);
            Log.v("TAG", model.getQuestion());
            Log.v("TAG", model.getImage_URL());
        }
    };

    mRecyclerview.setAdapter(mFireAdapter);
}

如果添加了项目,我将如何滚动到RecylcerView的顶部?请注意,我打电话:

mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

因为我需要撤消Firebase中数据的顺序。

1 个答案:

答案 0 :(得分:4)

我实际上能够在Firebase用户界面文档中找到 - 有必要调用.registerAdapterDataObserver()方法:

  @Override
public void onStart() {
    super.onStart();

    mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
        @Override
        protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) {
            viewHolder.mPollQuestion.setText(model.getQuestion());
            Picasso.with(getActivity().getApplicationContext())
                    .load(model.getImage_URL())
                    .fit()
                    .into(viewHolder.mPollImage);
            Log.v("TAG", model.getQuestion());
            Log.v("TAG", model.getImage_URL());
        }
    };

    mRecyclerview.setAdapter(mFireAdapter);

    mFireAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = mFireAdapter.getItemCount();
            int lastVisiblePosition = mLayoutManager.findLastCompletelyVisibleItemPosition();

            // If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
            // to the bottom of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (friendlyMessageCount - 1) && lastVisiblePosition == (positionStart - 1))) {
                mRecyclerview.scrollToPosition(positionStart);
            }
        }
    });
}