我正在尝试对布局使用多个视图,但始终出现此错误:E / RecyclerView:未连接适配器;跳过布局。
我尝试在SO以及其他地方寻找其他解决方案。我以前使用过Firebase-UI,但是由于没有针对多个视图的解决方案,因此我放弃了这一点。我已经将该代码重写了5次,但我仍然收到完全相同的错误。
这是我调用适配器的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
menuView = inflater.inflate(R.layout.fragment_menu, container, false);
menuItemsArrayList = new ArrayList<>();
bundle = getArguments();
if (bundle != null && bundle.containsKey("restaurantId") && bundle.containsKey("restaurantPath")){
setupRecyclerView();
queryFirestore();
loadMenuFirestore(menuQuery);
}
return menuView;
}
private void queryFirestore(){
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference menuReference = db.collection("restaurantPath");
menuQuery = menuReference.orderBy("levelCode", Query.Direction.ASCENDING);
}
private void loadMenuFirestore(Query query) {
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot querySnapshot: task.getResult()){
MenuItems menuItems = new MenuItems(querySnapshot.getString("headerName"), querySnapshot.getString("levelCode"), querySnapshot.getString("menuItemDescription"), querySnapshot.getString("menuItemImage"), querySnapshot.getString("menuItemName"));
menuItemsArrayList.add(menuItems);
}
MenuItemAdapter menuItemAdapter = new MenuItemAdapter(menuContext, menuItemsArrayList);
menuRecycler.setAdapter(menuItemAdapter);
}
});
}
private void setupRecyclerView() {
menuRecycler = menuView.findViewById(R.id.menu_card_recycler);
menuRecycler.setHasFixedSize(true);
menuRecycler.setLayoutManager(new LinearLayoutManager(menuContext));
}
这是我的适配器:
public class MenuItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
ArrayList<MenuItems> menuItemsArrayList;
Context adapterContext;
private static final int TYPE_ONE = 1;
private static final int TYPE_TWO = 2;
// Constructor class
public MenuItemAdapter(Context adapterContext, ArrayList<MenuItems> menuItemsArrayList) {
this.adapterContext = adapterContext;
this.menuItemsArrayList = menuItemsArrayList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
if (viewType == 1){
View view = LayoutInflater.from(adapterContext).inflate(R.layout.menu_header, viewGroup, false);
return new ViewHolderOne(view);
} else {
View view = LayoutInflater.from(adapterContext).inflate(R.layout.menu_item_card, viewGroup, false);
return new ViewHolderTwo(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder menuRecyclerViewHolder, int i) {
switch (menuRecyclerViewHolder.getItemViewType()){
case TYPE_ONE:
initLayoutOne((ViewHolderOne)menuRecyclerViewHolder, i);
break;
case TYPE_TWO:
initLayoutTwo((ViewHolderTwo)menuRecyclerViewHolder, i);
break;
}
}
private void initLayoutOne(ViewHolderOne holder, int pos) {
holder.tvHeaderName.setText(menuItemsArrayList.get(pos).getHeaderName());
}
private void initLayoutTwo(ViewHolderTwo holder, int pos) {
holder.tvMenuItemName.setText(menuItemsArrayList.get(pos).getMenuItemName());
holder.tvMenuItemDescription.setText(menuItemsArrayList.get(pos).getMenuItemDescription());
Glide.with(adapterContext).load(menuItemsArrayList.get(pos).getMenuItemImage()).apply(new RequestOptions().override(400,200)).placeholder(R.drawable.menu_card_default).into(holder.ivMenuItemImage);
}
@Override
public int getItemCount() {
return menuItemsArrayList.size();
}
@Override
public int getItemViewType(int position) {
MenuItems menuItems = menuItemsArrayList.get(position);
if(Integer.parseInt(menuItems.levelCode) % 2 != 0) {
return TYPE_ONE;
} else {
return TYPE_TWO;
}
}
// Static inner class to initialize the views of rows
static class ViewHolderOne extends RecyclerView.ViewHolder {
public TextView tvHeaderName;
public ViewHolderOne(View itemView) {
super(itemView);
tvHeaderName = itemView.findViewById(R.id.section_name);
}
}
static class ViewHolderTwo extends RecyclerView.ViewHolder {
public TextView tvMenuItemName, tvMenuItemDescription;
public ImageView ivMenuItemImage;
public ViewHolderTwo(View itemView) {
super(itemView);
tvMenuItemName = itemView.findViewById(R.id.menu_item_name);
tvMenuItemDescription = itemView.findViewById(R.id.menu_item_description);
ivMenuItemImage = itemView.findViewById(R.id.image_menu_item);
}
}
}