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.
source share