添加项目装饰后,适配器未在以编程方式创建的回收器视图中创建视图

Adapter not creating views in programmatically created recycler view after adding item decoration

我正在以编程方式创建 RecyclerView 为其分配适配器并添加项目装饰..但适配器未创建视图。当我移除物品装饰时,它的建筑很好。项目装饰没有问题,因为我在 xml 创建 RecyclerView 时使用了那段代码,没有任何问题。

示例代码如下:

RecyclerView recyclerView = new RecyclerView(context);
LinearLayout.LayoutParams recyclerViewLayoutParams =
        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
recyclerViewLayoutParams.gravity = Gravity.START;
recyclerView.setLayoutParams(recyclerViewLayoutParams);
recyclerView.setClipToPadding(false);
ll_parent_layout.addView(recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getParent());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

// recyclerView.removeItemDecoration(gridSpacingItemDecoration);
// recyclerView.addItemDecoration(gridSpacingItemDecoration);

Adapter adapter = new Adapter(Activity.this, ResponseMap);

// linearSpacingItemDecoration = new LinearSpacingItemDecoration(R.dimen.margin8, adapter.getItemCount());
// recyclerView.removeItemDecoration(linearSpacingItemDecoration);
// recyclerView.addItemDecoration(linearSpacingItemDecoration);
recyclerView.setAdapter(adapter);

问题在于项目装饰器 class 是如何初始化和重新初始化的。一段时间后我想通了