我正在使用 FirebaseRecyclerAdapter
,文档在这里。
基本上,我希望在 firebase 添加项时滚动到 RecyclerView
的顶部。这很棘手,因为实际上我没有用 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中反转数据的顺序。