目前,我的RecyclerView适配器正在实现两个不同的视图持有者(一个用于主标题,另一个用于隐藏子项)。我从这里开始实施这种类型的安排:http://anandbose.github.io/android_recyclerview_expandablelistview.html
我想知道的是: 在上述帖子之前,我正在为ViewHolder扩展布局,如下所示:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_maincategory_one,parent,false);
return new ListHeaderViewHolder(view);
}
这是一种非常基本的方式。在上面提到的帖子中,上面的内容是这样实现的:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater headerInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = headerInflater.inflate(R.layout.include_expandable_recyclerview_header, parent, false);
return new ListHeaderViewHolder(view);}
我想知道,两者背后的区别是什么? 因为,第一种方法不能按照我想要的方式工作(并且在所提到的帖子中应该如此)
简短的实施片段(来自帖子):
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType)
{
case CIRCLE_TITLE:
LayoutInflater headerInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = headerInflater.inflate(R.layout.include_expandable_recyclerview_header, parent, false);
return new ListHeaderViewHolder(view);
case CIRCLE_ADMINS:
LayoutInflater childInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = childInflater.inflate(R.layout.include_expandable_recyclerview_child,parent,false);
return new ListChildViewHolder(view);
}
return null;
}
谢谢。