Firebase RecyclerView - scroll through the animation when adding a new element

I am using the FirebaseRecyclerAdapter , the documentation located here .

Essentially, I want to scroll to the top of the RecyclerView as elements are added to Firebase. This is tricky, because I am not actually initializing the adapter with an ArrayList or any list. Firebase processes all data using the FirebaseRecyclerAdapter .

Here is my main 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; } 

And my adapter:

  @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); } 

How do I go to the top of RecylcerView as I add items? Please note that I am calling:

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

since I need to reorder the data in Firebase.

+5
source share
1 answer

I really could find it in the Firebase UI documentation - I need to call the .registerAdapterDataObserver( ) method:

  @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); } } }); } 
+4
source

Source: https://habr.com/ru/post/1261536/


All Articles