0
我正在使用Android Studio提供的類,該選項卡式活動使用ActionBar選項卡與ViewPager。在此活動中,我正嘗試使用Firebase數據庫中的數據初始化CardView的RecyclerView &。片段中的Android FirebaseRecyclerAdapter
問題:RecyclerView爲空,如下所示。
空屏幕,但滾動工作正常。
,這是我的代碼
public class Produce extends Fragment {
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<ProduceSetting, ProduceViewHolder> mFirebaseAdapter;
private RecyclerView mProduceList = null;
private View rootView;
private Context c;
private LinearLayoutManager manager;
public Produce() {}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_produce, container, false);
c = getContext();
mProduceList = (RecyclerView) rootView.findViewById(R.id.produce_recycler_view);
manager = new LinearLayoutManager(c);
mProduceList.setHasFixedSize(true);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Produce");
mFirebaseAdapter = new FirebaseRecyclerAdapter<ProduceSetting, ProduceViewHolder>(
ProduceSetting.class, R.layout.card_item_produce, ProduceViewHolder.class, mDatabase
) {
@Override
protected void populateViewHolder(ProduceViewHolder viewHolder, ProduceSetting model, int position) {
viewHolder.setName(model.getGameName());
viewHolder.setType(model.getGameType());
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition =
manager.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))) {
mProduceList.scrollToPosition(positionStart);
}
}
});
mProduceList.setLayoutManager(manager);
mProduceList.setAdapter(mFirebaseAdapter);
return rootView;
}
public static class ProduceViewHolder extends RecyclerView.ViewHolder {
View mView;
public ProduceViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView post_name = (TextView) mView.findViewById(R.id.post_name);
post_name.setText(name);
}
public void setType(String type) {
TextView post_type = (TextView) mView.findViewById(R.id.post_type);
post_type.setText(type);
}
public void setImage(Context ctx, String image) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
}
,這裏是我的getter和setter
public class ProduceSetting {
private String GameName;
private String GameType;
private String image;
public ProduceSetting(){}
public ProduceSetting(String gameName, String gameType, String image) {
GameName = gameName;
GameType = gameType;
this.image = image;
}
public String getGameName() {
return GameName;
}
public void setGameName(String gameName) {
GameName = gameName;
}
public String getGameType() {
return GameType;
}
public void setGameType(String gameType) {
GameType = gameType;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
請幫助我。