在适配器的onCreateViewHolder方法中,我具有需要作为参数传递的oprShown
变量。
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.layout_shift_container, parent, false);
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams)itemView.getLayoutParams();
lp.width = parent.getMeasuredWidth()/oprShown;
itemView.setLayoutParams(lp);
return new ShiftViewHolder(itemView);
}
适配器的构造函数如下:
private Context context;
private List<ShiftModel> shiftModelList;
int oprShown;
public ShiftMapAdapter(Context context, List<ShiftModel> shiftModelList, int oprShown) {
this.context = context;
this.shiftModelList = shiftModelList;
this.oprShown = oprShown;
}
然后我称呼所有类似以下内容的东西:
int operatorShow = 6; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { shiftsViewModel = ViewModelProviders.of(this).get(ShiftsViewModel.class); setHasOptionsMenu(true); View root = inflater.inflate(R.layout.fragment_shifts, container, false); unbinder = ButterKnife.bind(this, root); initialize(); shiftsViewModel.getCoworkerList().observe(this, new Observer<List<ShiftModel>>() { @Override public void onChanged(List<ShiftModel> shiftModel) { ShiftMapAdapter shiftMapAdapter = new ShiftMapAdapter(getContext(), shiftModel, operatorShow); coworker_recycler.setAdapter(shiftMapAdapter); } }); return root; }
我要做的就是使回收视图的宽度等于父屏幕的1/6。但是,当我运行代码时,出现此错误:
错误:类ShiftMapAdapter中的构造函数ShiftMapAdapter无法应用于给定类型;必需:Context,List,int找到:上下文,列表原因:实际参数和正式参数列表的长度不同
需要任何帮助吗?
在适配器的onCreateViewHolder方法中,我有oprShown变量,需要将其作为参数传递。 @NonNull @Override公共RecyclerView.ViewHolder onCreateViewHolder(@NonNull ...
使用getActivity()代替getContext()