滚动时,RecyclerView会搞砸

时间:2017-12-30 21:31:05

标签: java android android-recyclerview

正如您在屏幕截图中看到的,我的项目包含RecyclerView(对于食物类别),其中包含更多RecyclerView s(对于成分)。但是我遇到了问题,我RecyclerView搞乱了订单。我对项目进行了调试,参数很好,但RecyclerView显示错误。如图所示,水果成分显示在乳制品类别中。

image which display the problem of the RecyclerView messed up

IngredientSectionAdapter.Java (主适配器,包含更多RecyclerView s)

class SectionViewHolder extends RecyclerView.ViewHolder {
    private TextView sectionBtn;
    private RecyclerView itemRecyclerView;

    public SectionViewHolder(View itemView) {
        super(itemView);
        sectionBtn = (TextView) itemView.findViewById(R.id.text_category);
        itemRecyclerView = (RecyclerView) itemView.findViewById(R.id.ingredientsRecycler);
    }
}

private Context context;
private ArrayList<IngredientSectionModel> sectionModelArrayList;
ArrayList<IngredientItemAdapter> adapters;

public IngredientSectionAdapter(Context context, ArrayList<IngredientSectionModel> sectionModelArrayList) {
    this.context = context;
    this.sectionModelArrayList = sectionModelArrayList;
    adapters = new ArrayList<IngredientItemAdapter>();
}

@Override
public SectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.parent_list,null);
    return new SectionViewHolder(v);
}

@Override
public void onBindViewHolder(SectionViewHolder holder, int position) {
    final IngredientSectionModel sectionModel = sectionModelArrayList.get(position);
    holder.itemRecyclerView.setTag(holder.itemRecyclerView.getVisibility());
    final RecyclerView sectionList = holder.itemRecyclerView;
    holder.sectionBtn.setText(sectionModel.getSectionLabel());

    //recycler view for items
    holder.itemRecyclerView.setHasFixedSize(true);
    holder.itemRecyclerView.setNestedScrollingEnabled(false);

    /* set layout manager on basis of recyclerview enum type */

    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,1);
    adapters.add(new IngredientItemAdapter(context, sectionModel.getItemArrayList()));
    int resId = R.anim.grid_layout_animation_from_bottom;
    //LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(context, resId);
    holder.itemRecyclerView.setLayoutManager(staggeredGridLayoutManager);
    holder.itemRecyclerView.setAdapter(adapters.get(position));
    //holder.itemRecyclerView.setLayoutAnimation(animation);

    //toggle visibilty of inner RecyclerView (ingredients, not categories)
    holder.sectionBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (sectionList.getVisibility() == View.VISIBLE){
                sectionList.setVisibility(View.GONE);
            }
            else
            {
                sectionList.setVisibility(View.VISIBLE);
            }
        }
    });

}

导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:1)

每次调用onBindViewHolder时,您都会创建一个新的IngredientItemAdapter并将其添加到adapters,然后拨打holder.itemRecyclerView.setAdapter(adapters.get(position))。但是,adapters.get(position)不是您刚刚创建的适配器。你的适配器会变得越来越大。试试这个

IngredientItemAdapter adapter = adapters.get(position);
adapter.setIngredients(sectionModel.getItemArrayList());
holder.itemRecyclerView.setAdapter(adapter);