在适配器的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 找到:上下文,列表 原因:实际和正式论据列表的长度不同
有什么帮助吗?
答案 0 :(得分:1)
使用getActivity()代替getContext()
ShiftMapAdapter shiftMapAdapter = new ShiftMapAdapter(getActivity(), shiftModel, operatorShow);
coworker_recycler.setAdapter(shiftMapAdapter);