6

我在我的应用程序中创建了可扩展的回收视图,如下所示

它有父项目菜单类别,类别有子项目,如图所示

在此处输入图像描述

以下是我的适配器代码的一部分。

 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    if (i > HEADER_COUNT - 1) {
        FoodMenuItem foodMenuItem = foodMenuItems.get(i - HEADER_COUNT);
        MenuViewHolder menuViewHolder = (MenuViewHolder) viewHolder;
        //menuViewHolder.rvMenu.setLayoutManager(new LinearLayoutManager(viewHolder.itemView.getContext(), LinearLayoutManager.VERTICAL, false));
        if (isVegOnly) {
            List<Item> items = new ArrayList<>();
            for (Item item : foodMenuItem.getItems()) {
                if (item.getType().equalsIgnoreCase(Item.FOOD_TYPE_VEG)) {
                    items.add(item);
                }
            }
            menuViewHolder.rvMenu.setAdapter(new FoodMenuChildRecycler(items, foodMenuParentListener, i, isAddAllowed));
            menuViewHolder.tvNumItems.setText("(" + items.size() + " items)");

        } else {
            menuViewHolder.rvMenu.setAdapter(new FoodMenuChildRecycler(foodMenuItem.getItems(), foodMenuParentListener, i, isAddAllowed));
            menuViewHolder.tvNumItems.setText("(" + foodMenuItem.getItems().size() + " items)");

        }
        menuViewHolder.ivArrow.setVisibility(menuViewHolder.rvMenu.getAdapter().getItemCount() > 0 ? View.VISIBLE : View.GONE);
        menuViewHolder.ivArrow.setImageResource(foodMenuItem.isExpanded() ? R.drawable.chev_up : R.drawable.chev_down);
        menuViewHolder.tvTitle.setText(foodMenuItem.getCategoryName());
        menuViewHolder.rvMenu.setNestedScrollingEnabled(false);
        if (foodMenuItem.isExpanded()) {
            menuViewHolder.rvMenu.setVisibility(View.VISIBLE);
        } else {
            menuViewHolder.rvMenu.setVisibility(View.GONE);
        }
    }

}

以下是适配器onBindViewHolder的代码

  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    ChildMenuHolder childMenuHolder = (ChildMenuHolder) viewHolder;
    Context context = viewHolder.itemView.getContext();
    Item item = itemList.get(i);
    childMenuHolder.tvTitle.setText(item.getTitle());
    childMenuHolder.txtQty.setText(String.valueOf(item.getQuatity()));
    childMenuHolder.txtCartInc.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
    childMenuHolder.txtQty.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
    childMenuHolder.txtCartDec.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
    childMenuHolder.btnAdd.setVisibility(!item.isAddedToCart() ? View.VISIBLE : View.GONE);
    childMenuHolder.btnAdd.setBackgroundResource(isAddAllowed ? R.drawable.rounded_bottom_edge_shape_food : R.drawable.rounded_bottom_edge_shape_disable);
    childMenuHolder.btnAdd.setTextColor(isAddAllowed ? ContextCompat.getColor(context, R.color.button_green) : ContextCompat.getColor(context, R.color.gray_a1));
    childMenuHolder.ivType.setColorFilter(item.getType().equalsIgnoreCase(Item.FOOD_TYPE_VEG) ? ContextCompat.getColor(context, R.color.selected_green) : ContextCompat.getColor(context, R.color.app_red));
}

按照 适配器 中的代码 展开折叠

  public MenuViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        tvNumItems = (TextView) itemView.findViewById(R.id.tvNumItems);
        ivArrow = (ImageView) itemView.findViewById(R.id.ivArrow);
        rvMenu = (RecyclerView) itemView.findViewById(R.id.rvMenu);
        rvMenu.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
       // rvMenu.setNestedScrollingEnabled(false);
        rlArrow = itemView.findViewById(R.id.rlArrow);
        rlArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (foodMenuParentListener != null) {
                    foodMenuParentListener.OnExpandClick(getAdapterPosition());
                }
            }
        });
        tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (foodMenuParentListener != null) {
                    foodMenuParentListener.OnExpandClick(getAdapterPosition());
                }
            }
        });
    }

监听器内的 onExpandClick 如下

        @Override
        public void OnExpandClick(int position) {
            FoodMenuRecyclerAdapter foodMenuRecyclerAdapter = (FoodMenuRecyclerAdapter) recyclerView.getAdapter();
            foodMenuRecyclerAdapter.setExpandIndex(position);
            foodMenuRecyclerAdapter.notifyItemChanged(position);
        }

适配器内部的 setExpandIndex 如下

public void setExpandIndex(int expandIndex) {
    foodMenuItems.get(expandIndex - HEADER_COUNT).setExpanded(!foodMenuItems.get(expandIndex - HEADER_COUNT).isExpanded());
}

由于 ChildRecycler 适配器中的项目太多,当子项目太多时,我面临一个很大的滞后问题。当子项的数量较少时它可以正常工作

如何解决这个问题?

此外,由于背景中存在卡片视图,我发现很难拥有单个回收器视图,如图所示,否则我将使用单个回收器视图,而折叠/展开将在某个位置实现添加和删除项目

新注:日期 5 月 5 日

我尝试使用 NestedScrollView 的新解决方案,然后将回收器视图添加到这个嵌套的 RecyclerView

以下是代码

 for (FoodMenuItem item : foodResult.getFoodMenuItems()) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_food_parent, llMenu, false);
            RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rvMenu);
            ((TextView)view.findViewById(R.id.tvtitle)).setText(item.getCategoryName());

            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            view.findViewById(R.id.rlArrow).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    recyclerView.setVisibility(recyclerView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                }
            });
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    recyclerView.setAdapter(new FoodMenuChildRecycler(item.getItems(), getAddItemListener(), i, isFlightAssociatedWithLocation));
                    llMenu.addView(view);
                }
            }, 200);


            i++;

        }

以下是 XML 代码

 <android.support.v4.widget.NestedScrollView
        android:id="@+id/rvParentMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="160dp">

        <LinearLayout
            android:id="@+id/llMenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

    </android.support.v4.widget.NestedScrollView>

我面临的问题是将视图添加到嵌套滚动视图中的线性布局并为回收器视图设置适配器调用的代码几乎冻结了 UI 几秒钟为其添加了处理程序,但似乎不是一个好的解决方案

4

0 回答 0