在recyclerview中加载Firestore数据时,如何显示进度栏布局?

时间:2019-06-02 20:12:43

标签: android android-recyclerview pagination google-cloud-firestore

我正在从Firestore提取数据并将其显示在具有2种不同布局的recyclerview中。就其本身而言,它可以正常工作。但是,当我添加第三个布局时,仅在提取数据时填充了进度条,回收器视图完全停止工作。我正在努力实现这一目标:http://www.androidlearningtutorials.com/blog_details.php?article_id=12

这是我的适配器:

public class MenuItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private ArrayList<MenuItem> menuItemArrayList;
    public HashMap<String, Integer> selectionItemsHashMap;
    private LinearLayoutManager linearLayoutManager;

    private Context adapterContext;
    private static final String TAG = MenuItemAdapter.class.getSimpleName();

    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private boolean isLoading;
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;

    private OnAddToCartListener addToCartListener;

    private OnLoadMoreListener onLoadMoreListener; // create listener variable

    public interface OnLoadMoreListener { // define listener interface
        void onLoadMore();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnAddToCartListener {
        void onAddToCart(HashMap<String, Integer> selectionItemsHashMap);
    }

    public void setOnAddToCartListener(OnAddToCartListener addToCartListener) {
        this.addToCartListener = addToCartListener;
    }

    // Constructor class
    public MenuItemAdapter(Context adapterContext, ArrayList<MenuItem> menuItemArrayList, HashMap<String, Integer> selectionItemsHashMap, RecyclerView menuRecycler) {
        this.adapterContext = adapterContext;
        this.menuItemArrayList = menuItemArrayList;
        this.selectionItemsHashMap = selectionItemsHashMap;

        // Load more on scrolling
        linearLayoutManager = (LinearLayoutManager) menuRecycler.getLayoutManager();
        menuRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                    }
                    isLoading = true;
                }
            }
        });
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        final RecyclerView.ViewHolder holder;
        View view;
        switch (viewType) {

            case R.layout.menu_item_card:
                view = LayoutInflater.from(adapterContext).inflate(R.layout.menu_item_card, viewGroup, false);
                holder = new DishViewHolder(view);
                break;

            case R.layout.section_header:
                view = LayoutInflater.from(adapterContext).inflate(R.layout.section_header, viewGroup, false);
                holder = new SectionViewHolder(view);
                break;

            case R.layout.item_loading:
                view = LayoutInflater.from(adapterContext).inflate(R.layout.item_loading, viewGroup, false);
                holder = new LoadViewHolder(view);
                break;

            default:
                view = LayoutInflater.from(adapterContext).inflate(R.layout.item_loading, viewGroup, false);
                holder = new LoadViewHolder(view);
                break;
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        if (holder instanceof SectionViewHolder) {
            ((SectionViewHolder)holder).tvHeaderName.setText(menuItemArrayList.get(i).getHeaderName());
            ((SectionViewHolder)holder).tvHeaderDescription.setText(menuItemArrayList.get(i).getHeaderDescription());
        } else if (holder instanceof DishViewHolder) {
            ((DishViewHolder)holder).tvMenuItemPrice.setText("$"+menuItemArrayList.get(i).getMenuItemPrice());
            ((DishViewHolder)holder).tvMenuItemName.setText(menuItemArrayList.get(i).getMenuItemName());
            ((DishViewHolder)holder).tvMenuItemDescription.setText(menuItemArrayList.get(i).getMenuItemDescription());
            Glide.with(adapterContext).load(menuItemArrayList.get(i).getMenuItemImage()).placeholder(R.drawable.menu_card_default).into(((DishViewHolder) holder).ivMenuItemImage);
        } else if (holder instanceof LoadViewHolder) {
            ((LoadViewHolder)holder).progressBar.setIndeterminate(true);
        }
    }

    @Override
    public int getItemCount() {
        return menuItemArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        int levelCode = Integer.parseInt(menuItemArrayList.get(position).getLevelCode());
        if (menuItemArrayList.get(position) != null) {
            if (levelCode % 2 == 0) {
                return R.layout.menu_item_card;
            } else {
                return R.layout.section_header;
            }
        }
        else {
                return R.layout.item_loading;
            }
    }

    private void showToast(View parent) {
        LayoutInflater inflater = (LayoutInflater) adapterContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate(R.layout.toast_add_cart, (ViewGroup) parent.findViewById(R.id.toast_root));

        Toast toast = new Toast(adapterContext);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0,600);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    public void setLoaded() {
        isLoading = false;
    }

    public class DishViewHolder extends RecyclerView.ViewHolder{

        public TextView tvMenuItemName, tvMenuItemDescription, tvMenuItemPrice;
        public ImageView ivMenuItemImage;
        public Button btnInitialAdd;

        public DishViewHolder(@NonNull 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);
            tvMenuItemPrice = itemView.findViewById(R.id.menu_item_price);
            btnInitialAdd = itemView.findViewById(R.id.button_initial_add);
            btnInitialAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    String itemNameCheck = menuItemArrayList.get(position).getMenuItemName();
                    if (selectionItemsHashMap != null) {
                        if (selectionItemsHashMap.containsKey(itemNameCheck)) {
                            int currentQuantity = selectionItemsHashMap.get(itemNameCheck);
                            currentQuantity++;
                            selectionItemsHashMap.put(itemNameCheck,currentQuantity);
                        } else {
                            selectionItemsHashMap.put(menuItemArrayList.get(position).getMenuItemName(), 1);
                        }
                    } else {
                        selectionItemsHashMap = new HashMap<String, Integer>();
                        selectionItemsHashMap.put(menuItemArrayList.get(position).getMenuItemName(), 1);
                    }
                    addToCartListener.onAddToCart(selectionItemsHashMap);
                    showToast(view);
                }
            });

        }
    }

    public class SectionViewHolder extends RecyclerView.ViewHolder {

        public TextView tvHeaderName, tvHeaderDescription;

        public SectionViewHolder(@NonNull View itemView) {
            super(itemView);

            tvHeaderName = itemView.findViewById(R.id.section_header_name);
            tvHeaderDescription = itemView.findViewById(R.id.section_header_description);

        }
    }

    public class LoadViewHolder extends RecyclerView.ViewHolder {

        public ProgressBar progressBar;

        public LoadViewHolder(@NonNull View itemView) {
            super(itemView);

            progressBar = itemView.findViewById(R.id.progress_bar);
        }
    }
}

这是我主要片段中的相关代码:

    @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);
        Button backButton = menuView.findViewById(R.id.button_back);
        Button nextButton = menuView.findViewById(R.id.button_next);
        tvTotalAmount = menuView.findViewById(R.id.total_amount);

        bundle = getArguments();

        db = FirebaseFirestore.getInstance();
        menuItemArrayList = new ArrayList<>();
        bundle = getArguments();

        // Get data
        if (bundle != null && bundle.containsKey("restaurantId") && bundle.containsKey("restaurantPath")) {
            restaurantPath = bundle.getString("restaurantPath");
            restaurantId = bundle.getString("restaurantId");

            if (bundle.containsKey("cartContent")) {
                Serializable storedCart = bundle.getSerializable("cartContent");
                cartItemsHashmap =  (HashMap<String, Integer>) storedCart;
                reinstateCart();
            } else {
                cartItemsHashmap = null;
            }

        }
        restaurantHeader();
        setupMenuRecycler();
        lastResult = null;

        if (menuItemArrayList.size() > 0) {
            menuItemArrayList.clear();
        }
        fetchData();

        // Setup recycler
        menuItemAdapter = new MenuItemAdapter(menuContext, menuItemArrayList, cartItemsHashmap, menuRecycler);
        menuRecycler.setAdapter(menuItemAdapter);

        // Load more
        menuItemAdapter.setOnLoadMoreListener(new MenuItemAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (menuItemArrayList.size() < 3) {
                    menuItemArrayList.add(null);
                    menuItemAdapter.notifyItemInserted(menuItemArrayList.size() - 1);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            menuItemArrayList.remove(menuItemArrayList.size() - 1);
                            menuItemAdapter.notifyItemRemoved(menuItemArrayList.size());

                            // Fetch more data
                            fetchData();

                            menuItemAdapter.notifyDataSetChanged();
                            menuItemAdapter.setLoaded();
                        }
                    }, 400);
                }
            }
        });

        // Using the adapter interface to add items to cart and adding up total amount
        menuItemAdapter.setOnAddToCartListener(new MenuItemAdapter.OnAddToCartListener() {
            @Override
            public void onAddToCart(final HashMap selectionItemsHashMap) {

                OnUpdateCartListener onUpdateCartListener = new OnUpdateCartListener() {
                    @Override
                    public int getAmount() {
                        return totalAmount;
                    }

                    @Override
                    public void onUpdateCart(int totalAmount) {
                        String stringTotalAmount = Integer.toString(totalAmount);
                        tvTotalAmount.setText(stringTotalAmount);
                        finalTotalAmount = totalAmount;
                    }
                };

                setupSelectionRecycler(menuView);
                totalAmount = mapToArray(selectionItemsHashMap);
                if (cartItemsHashmap==null) {
                    selectionAdapter = new SelectionAdapter(selectionItemArrayList, selectionItemsHashMap, totalAmount, onUpdateCartListener);
                } else {
                    selectionAdapter = new SelectionAdapter(selectionItemArrayList, cartItemsHashmap, totalAmount, onUpdateCartListener);
                }

                selectionRecycler.setAdapter(selectionAdapter);
                cartItemsHashmap = selectionItemsHashMap;

                selectionAdapter.setUpdateCartListener(new SelectionAdapter.OnUpdateCartListener() {
                    @Override
                    public void onUpdateCart(int totalAmount) {
                        String stringTotalAmount = Integer.toString(totalAmount);
                        tvTotalAmount.setText(stringTotalAmount);
                        finalTotalAmount = totalAmount;
                    }
                });
            }

        });
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bundle.putBoolean("direction", false);
                bundle.remove("cartContent");
                bundle.remove("cartSum");
                cartFilledListener.onCartFilled(bundle);
            }
        });

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int sentTotalAmount = Integer.parseInt(tvTotalAmount.getText().toString());
                if (sentTotalAmount > 0) {
                    bundle.putSerializable("cartContent", cartItemsHashmap);
                    Log.d(TAG, "cart content:" + cartItemsHashmap);
                    bundle.putInt("cartSum", sentTotalAmount);
                    bundle.putBoolean("direction", true);
                    cartFilledListener.onCartFilled(bundle);
                } else {
                    showToast(view);
                }
            }
        });

        return menuView;
    }

    private void fetchData() {
        if (lastResult == null) {
            menuQuery = db.collection(restaurantPath).orderBy("levelCode", Query.Direction.ASCENDING).limit(3);
        } else {
            menuQuery = db.collection(restaurantPath).orderBy("levelCode", Query.Direction.ASCENDING).limit(3).startAfter(lastResult);
        }
        menuQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    MenuItem menuItems = new MenuItem(documentSnapshot.getString("menuItemDescription"),
                            documentSnapshot.getString("menuItemName"),
                            documentSnapshot.getString("levelCode"),
                            documentSnapshot.getString("menuItemImage"),
                            documentSnapshot.getString("headerName"),
                            documentSnapshot.getString("menuItemPrice"),
                            documentSnapshot.getString("headerDescription"));
                    menuItemArrayList.add(menuItems);
                }
                if (queryDocumentSnapshots.size() > 0) {
                    lastResult = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
                }
            }
        });
    }

这是请求的堆栈跟踪;我之前收到过跳过布局的消息,但是回收站工作得很好:

06-03 00:56:08.022 31054-31054/com.bvb09.shoppingcart E/RecyclerView: No adapter attached; skipping layout
06-03 00:56:08.742 1471-1471/? D/StatusBar.NetworkController: refreshViews connected={ wifi } level=0 combinedSignalIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mobileLabel= wifiLabel="MVP Factory" emergencyOnly=false combinedLabel="MVP Factory" mAirplaneMode=true mDataActivity=0 mPhoneSignalIconId=0x0/(null) mQSPhoneSignalIconId=0x0/(null) mDataDirectionIconId=0x0/(null) mDataSignalIconId=0x0/(null) mDataTypeIconId=0x0/(null) mQSDataTypeIconId=0x0/(null) mNoSimIconId=0x0/(null) mWifiIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mQSWifiIconId=0x7f02032b/com.android.systemui:drawable/qs_tile_wifi_signal_4 mWifiActivityIconId=0x7f0205fc/com.android.systemui:drawable/stat_sys_signal_inout mBluetoothTetherIconId=0x7f020625/com.android.systemui:drawable/stat_sys_tether_bluetooth
06-03 00:56:08.742 1471-1471/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
06-03 00:56:08.752 1471-1471/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
06-03 00:56:08.752 1471-1471/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
06-03 00:56:08.752 1471-1471/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
06-03 00:56:08.992 31943-31943/? E/TZ_CCM_SERVER: Only 'CCM' are supported
06-03 00:56:08.992 31943-31943/? I/TZ_CCM_SERVER: ###Exit TZ CCM Server
06-03 00:56:08.992 889-1664/? E/Watchdog: !@Sync 10961 [06-03 00:56:08.997]

0 个答案:

没有答案