我将 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;
}
视图持有者:
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);
}
主屏幕的 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 上下文设置在另一个应用程序上,该应用程序在主屏幕活动启动后立即启动。任何帮助将不胜感激。
编辑:我正在挖掘 FirebaseUI GitHub 页面,这很可能是问题所在,并发现另一个用户遇到了完全相同的问题。在 FirebaseRecyclerAdapter 类中的 notifyItemInserted 之后似乎没有调用 onBindViewHolder 。现在要修复它...