我有一个RecyclerView
可以有X个项目,每个项目可能不同
所以我这样实现了它:
public override int GetItemViewType(int position) {
QuestionType type = get_item(position).question_type;
switch (type) {
case QuestionType.caption:
return VIEW_TYPE_CAPTION;
case QuestionType.text:
return VIEW_TYPE_TEXT;
case QuestionType.check:
case QuestionType.radio:
case QuestionType.boolean:
case QuestionType.list:
return VIEW_TYPE_LIST;
case QuestionType.date:
case QuestionType.date_range:
return VIEW_TYPE_DATE;
case QuestionType.slider:
return VIEW_TYPE_SLIDER;
case QuestionType.range_slider:
return VIEW_TYPE_RANGE;
case QuestionType.poi:
return VIEW_TYPE_POI;
case QuestionType.grid:
return VIEW_TYPE_GRID;
default:
return VIEW_TYPE_SECTION;
}
}
在我的oncreateviewholder
方法中,它是这样的:
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.From(parent.Context);
switch (viewType) {
case VIEW_TYPE_CAPTION:
return new CaptionViewHolder(inflater.Inflate(CaptionViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_TEXT:
return new TextViewHolder(inflater.Inflate(TextViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_LIST:
return new ListViewHolder(inflater.Inflate(ListViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_DATE:
return new DateViewHolder(inflater.Inflate(DateViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_SLIDER:
return new SliderViewHolder(inflater.Inflate(SliderViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_RANGE:
return new RangeViewHolder(inflater.Inflate(RangeViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_POI:
return new PoiViewHolder(inflater.Inflate(PoiViewHolder.Layout(_minified), parent, false), _provider);
case VIEW_TYPE_GRID:
return new GridViewHolder(inflater.Inflate(GridViewHolder.Layout(_minified), parent, false), _provider);
default:
return new SectionViewHolder(inflater.Inflate(SectionViewHolder.Layout(), parent, false));
}
除非我创建ListViewHolder
列表视图持有者里面有一个recyclerview
,其中包含一个项目列表,它初始化如下:
public ListViewHolder(View view, QuestionInteraction provider) : base(view, provider) {
list = view.FindViewById<RecyclerView>(Resource.Id.question_answer_list);
adapter = new ChoiceAdapter(ItemView.Context, value_changed);
list.SetAdapter(adapter);
}
我在原始适配器的OnBindViewHolder中有一个更新方法,它在ListViewHolder中调用它:
public override void update_data(QuestionVO q) {
_initializing = true;
choices = q.choices;
values = q.selected_choices.ToDictionary(c => c, c => (object)true);
if (q.choice_type == QuestionChoiceType.box && q.sub_type != QuestionSubType.integer) {
int spanCount = ItemView.Resources.GetInteger(Resource.Integer.tiles_per_row);
manager = new GridLayoutManager(ItemView.Context, spanCount);
} else {
manager = new LinearLayoutManager(ItemView.Context, q.horizontal ? LinearLayoutManager.Horizontal : LinearLayoutManager.Vertical, false);
}
manager.Orientation = (int)(q.horizontal ? Orientation.Horizontal : Orientation.Vertical);
list.SetLayoutManager(manager);
adapter.initialize(choices, q.horizontal, values, q.other_text, q.choice_type, q.sub_type, q.has_master_list);
}
这个新的适配器(ChoiceAdapter)有一个自己的GetItemViewType方法
public override int GetItemViewType(int position) {
if (_type == QuestionSubType.integer && _horizontal)
return VIEW_TYPE_VALUE_HORIZONTAL;
else if (_type == QuestionSubType.integer)
return VIEW_TYPE_VALUE;
else if (_horizontal)
return VIEW_TYPE_BAR_HORIZONTAL;
else if (_choice_type == QuestionChoiceType.box)
return VIEW_TYPE_BOX;
else if (_choice_type == QuestionChoiceType.bar)
return VIEW_TYPE_BAR;
else if (_choice_type == QuestionChoiceType.tile)
return VIEW_TYPE_TILE;
return -1;
}
我的问题是永远不会调用此方法,而OnBindViewHolder
方法始终使用0作为viewType
有人能告诉我我做错了什么吗?为什么不调用内部适配器的GetitemViewType
方法?