在我的应用程序中,我正在某个活动中使用FirebaseRecyclerAdapter,并且工作正常,但是当我尝试在一个片段中使用它时,则它在下面无法正常工作,我在其中附加了我的活动代码,其中我的活动正常,并且还附加了我的片段。
在我的活动中效果很好:
<a link='https://codepen.io/cathydutton/pen/ldazc?editors=1010'></a>
但是它不是一个片段:
public class Notification extends AppCompatActivity {
private RecyclerView mPeopleRV;
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<News, Notification.NewsViewHolder> mPeopleRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference().child("XXXX");
mDatabase.keepSynced(true);
mPeopleRV = (RecyclerView) findViewById(R.id.myRecycleView);
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("XXXX");
Query personsQuery = personsRef.orderByKey();
mPeopleRV.hasFixedSize();
LinearLayoutManager mLayoutManager = new LinearLayoutManager(Notification.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mPeopleRV.setLayoutManager(mLayoutManager);
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Notification.NewsViewHolder holder, final int position, final News model) {
holder.setTitle(model.getName());
}
@Override
public Notification.NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_row, parent, false);
return new Notification.NewsViewHolder(view);
}
};
mPeopleRV.setAdapter(mPeopleRVAdapter);
}
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mPeopleRVAdapter.stopListening();
}
public static class NewsViewHolder extends RecyclerView.ViewHolder {
View mView;
public NewsViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
String output = title.substring(0, 1).toUpperCase() + title.substring(1);
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(output);
}
}