我想在我的项目中使用FirebaseRecyclerAdapter
,并在之前的Project
FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder> adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, ref) {
public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage, int position) {
chatMessageViewHolder.nameText.setText(chatMessage.getName());
chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
}
};
recycler.setAdapter(mAdapter);
它工作正常,但现在它不再起作用了,FirebaseUi
的新版本中是否有任何更新无法让我现在重复使用此代码?
我在我的项目中尝试了这个
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<BlogModel, BlogListViewHolder> adapter = new FirebaseRecyclerAdapter<BlogModel, BlogListViewHolder>(BlogModel.class, android.R.layout.two_line_list_item, BlogListViewHolder.class, mDatabaseReference) {
public void populateViewHolder(BlogListViewHolder blogListViewHolder, BlogModel blogModel, int position) {
blogListViewHolder.postTitle.setText(blogModel.getPostTitle());
blogListViewHolder.postDesc.setText(blogModel.getPostDesc());
}
};
mBlogList.setAdapter(adapter);
}
并且Android Studio提示您必须实施onCreateViewHolder,onBindViewHolder
方法。
BlogListViewHolder类
public static class BlogListViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDesc;
public BlogListViewHolder(View view) {
super(view);
postImage = view.findViewById(R.id.blog_image_id);
postTitle = view.findViewById(R.id.blog_title_id);
postDesc = view.findViewById(R.id.blog_desc_id);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
我使用此依赖关系
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.google.firebase:firebase-auth:11.4.2'
compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.google.firebase:firebase-storage:11.4.2'
compile 'com.firebaseui:firebase-ui-database:3.1.0'
compile 'com.firebaseui:firebase-ui-firestore:3.1.0'
compile 'com.firebaseui:firebase-ui-auth:3.1.0'
compile 'com.firebaseui:firebase-ui-storage:3.1.0'
testCompile 'junit:junit:4.12'
compile 'com.squareup.picasso:picasso:2.5.2'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:11)
语法已更改。概述电话的doc部分在这里:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
以下是使用Firebase数据库和FirebaseUI版本的应用程序gradle文件:
// FirebaseUI for Firebase Realtime Database
compile 'com.firebaseui:firebase-ui-database:3.1.0'
compile 'com.google.firebase:firebase-database:11.4.2'
我定义了标准的ViewHolder和Model类。为了简洁,我会把它们留下来。
我的Fragment类顶部的实例变量:
private RecyclerView mPeopleRV;
private FirebaseRecyclerAdapter<PersonModel, PeopleVH> mPeopleRVAdapter;
在我片段的onCreateView()中:
mPeopleRV = v.findViewById(R.id.peopleRV);
在我的Fragment的onActivityStarted()中,所有操作都在这里。
onCreateViewHolder是行布局膨胀的地方。然后使用bindViewHolder()填充从Firebase获取的模型数据的行。
适配器的构造函数现在采用您根据查询和模型定义的一组选项。
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("People");
Query personsQuery = personsRef.orderByKey();
mPeopleRV.hasFixedSize();
mPeopleRV.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<PersonModel>().setQuery(personsQuery, PersonModel.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<PersonModel, PeopleVH>(personsOptions) {
@Override
protected void onBindViewHolder(PeopleVH holder, int position, PersonModel model) {
holder.setPersonFirstName(model.getFirstName());
holder.setPersonLastName(model.getLastName());
}
@Override
public PeopleVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_row_people_rv, parent, false);
return new PeopleVH(view);
}
};
mPeopleRV.setAdapter(mPeopleRVAdapter);
稍后添加:
不要忘记在onStart()和onStop()中启动和停止适配器监听器:
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mPeopleRVAdapter.stopListening();
}
答案 1 :(得分:0)
除了@Lucy的答案。 现在,有体系结构组件生命周期所有者。 As per the firestoreui android doc
如果您不想手动开始/停止收听,则可以使用Android体系结构组件自动管理FirestoreRecyclerAdapter
的生命周期。将LifecycleOwner
传递到FirestoreRecyclerOptions.Builder.setLifecycleOwner(...)
,FirebaseUI将自动开始和停止在onStart()
和onStop()
中的监听。
在这种特定情况下,personsOptions.setLifecycleOwner(myFragmentOrActivity.this)