我使用Android Studio提供的类来进行使用带有ViewPager的操作栏选项卡的选项卡式活动。在此活动中,我尝试使用Firebase数据库中的数据初始化RecyclerView。
问题:在应用首次运行时,RecyclerView为空,如下所示。
如果我从模拟器中关闭并重新打开应用程序,我的RecyclerView会按原样填充,如下所示。
关于为什么会发生这种情况的任何想法?我有一个理论,但我还没有找到解决方案。在尝试阅读FragmentPagerAdapter页面后,我得到的印象是片段必须是静态的(我不知道这可能是什么意思,所以如果有人能够对此有所了解,那么它将是理解)。在应用程序首次运行时,它会初始化RecyclerView。然后它会添加来自Firebase数据库的数据,但由于RecyclerView已经初始化,因此它是空的,并且从未正确更新。我试着调用notify ...方法无济于事。
StudentFragment&onCreateView方法:
private View view;
private Context c;
private RecyclerView mRecyclerView;
private LinearLayoutManager manager;
private Firebase mFirebaseRef;
private FirebaseRecyclerAdapter<Student, ViewHolder> firebaseRecyclerAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_students, container, false);
mFirebaseRef = new Firebase("<your Firebase link here>");
c = getContext();
//Initializes Recycler View and Layout Manager.
mRecyclerView = (RecyclerView) view.findViewById(R.id.studentRecyclerView);
manager = new LinearLayoutManager(c);
mRecyclerView.setHasFixedSize(true);
firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Student, ViewHolder>(
Student.class,
R.layout.single_student_recycler,
ViewHolder.class,
mFirebaseRef
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Student student, int i) {
viewHolder.vFirst.setText(student.getFirst());
viewHolder.vLast.setText(student.getLast());
viewHolder.vDue.setText(Double.toString(student.getCurrentlyDue()));
viewHolder.vRadio.setButtonTintList(ColorStateList.valueOf(Color.parseColor(student.getColor())));
Log.d(TAG, "populateViewHolder called");
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
mRecyclerView.setLayoutManager(manager);
return view;
}
ViewHolder:
public static class ViewHolder extends RecyclerView.ViewHolder {
public final TextView vFirst;
public final TextView vLast;
public final TextView vDue;
public final RadioButton vRadio;
public ViewHolder(View itemView) {
super(itemView);
vFirst = (TextView) itemView.findViewById(R.id.recycler_main_text);
vLast = (TextView) itemView.findViewById(R.id.recycler_sub_text);
vRadio = (RadioButton) itemView.findViewById(R.id.recycler_radio_button);
vDue = (TextView) itemView.findViewById(R.id.recycler_due_text);
}
Homescreen的onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homescreen);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
Firebase上下文设置在Homescreen活动启动后立即启动的另一个应用程序上。任何帮助将不胜感激。
编辑:我正在挖掘FirebaseUI GitHub页面,这是问题最可能出现的地方,并且发现另一个用户遇到完全相同的问题。似乎onBindViewHolder在FirebaseRecyclerAdapter类中的notifyItemInserted之后没有被调用。现在来解决它......
答案 0 :(得分:4)
在我的情况下,这是由mRecyclerView.setHasFixedSize(true);
引起的。如果您注释掉这行代码,列表会正确加载。我从这次讨论中得到了解决方案:https://github.com/firebase/FirebaseUI-Android/issues/204
答案 1 :(得分:0)
让我试试,正如你在问题标题上所说,
RecyclerView未在应用程序启动时显示
所以,
应该在onStart 上声明初始化Recycler View和布局管理器。
@Override
public void onStart() {
super.onStart();
mFirebaseRef = new Firebase("<your Firebase link here>");
firebaseRecyclerAdapter = ...
//and so on
希望它有所帮助!
答案 2 :(得分:0)
firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = firebaseRecyclerAdapter.getItemCount();
int lastVisiblePosition =
linearLayoutManager.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))) {
linearLayoutManager.scrollToPosition(positionStart);
}
}
});
recyclerListIdeas.setAdapter(firebaseRecyclerAdapter);
**只需添加Recyclerview.AdapterDataObserver()即可。为我工作!希望它有帮助:)**
答案 3 :(得分:0)
我有同样的问题,请查看文档:
https://codelabs.developers.google.com/codelabs/firebase-android/#6
通过添加数据观察器修复它:
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition =
mLinearLayoutManager.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))) {
mMessageRecyclerView.scrollToPosition(positionStart);
}
}
});