我在应用中创建了可扩展的回收视图,如下所示
它具有父项菜单类别,而“类别”具有子项,如图所示
以下是我的父适配器代码的一部分。
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几乎几秒钟,为此添加了处理程序,但这似乎不是一个很好的解决方案