我有一个RecyclerView,其中包含Horizontal RecyclerView和Vertical Recyclerview。我对如何同时管理两者的数据模型感到困惑。这是我的代码,后跟此example
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_DESIGN_TYPES = 0;
private final int VIEW_TYPE_DESIGNS = 1;
private Context context;
private ArrayList<Object> feeds;
public HomeAdapter(Context context, ArrayList<Object> feeds) {
this.context = context;
this.feeds = feeds;
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return VIEW_TYPE_DESIGN_TYPES;
if (position == 1)
return VIEW_TYPE_DESIGNS;
return -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
RecyclerView.ViewHolder holder;
switch (viewType) {
case VIEW_TYPE_DESIGN_TYPES:
view = inflater.inflate(R.layout.item_home_design_types, parent, false);
holder = new DesignTypesViewHolder(view);
break;
case VIEW_TYPE_DESIGNS:
view = inflater.inflate(R.layout.item_home_designs, parent, false);
holder = new DesignsViewHolder(view);
break;
default:
view = inflater.inflate(R.layout.item_home_designs, parent, false);
holder = new DesignsViewHolder(view);
break;
}
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == VIEW_TYPE_DESIGN_TYPES)
designTypesView((DesignTypesViewHolder) holder);
else if (holder.getItemViewType() == VIEW_TYPE_DESIGNS)
designsView((DesignsViewHolder) holder);
}
private void designTypesView(DesignTypesViewHolder holder) {
HomeAdapterDesignTypes homeAdapterDesignTypes = new HomeAdapterDesignTypes(getDesignTypes());
holder.recyclerView.setAdapter(homeAdapterDesignTypes);
}
private void designsView(DesignsViewHolder holder) {
HomeAdapterDesigns homeAdapterDesigns = new HomeAdapterDesigns(getDesigns());
holder.recyclerView.setAdapter(homeAdapterDesigns);
}
@Override
public int getItemCount() {
return feeds.size();
}
public void updateFeeds(ArrayList<Object> mFeeds) {
feeds.addAll(mFeeds);
notifyDataSetChanged();
}
public class DesignTypesViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
DesignTypesViewHolder(View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.rvHomeDesignTypes);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
recyclerView.addItemDecoration(itemDecoration);
}
}
public class DesignsViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
DesignsViewHolder(View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.rvHomeDesigns);
recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
int spanCount = 2;
int spacing = 15;
boolean includeEdge = true;
recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
}
}
private ArrayList<DesignType> getDesignTypes() {
ArrayList<DesignType> singleHorizontals = new ArrayList<>();
for(int i = 0 ; i < feeds.size(); i++) {
Object object = feeds.get(i);
if(object instanceof DesignType) {
singleHorizontals.add((DesignType) object);
}
}
return singleHorizontals;
}
private ArrayList<Design> getDesigns() {
ArrayList<Design> singleVerticals = new ArrayList<>();
for(int i = 0 ; i < feeds.size(); i++) {
Object object = feeds.get(i);
if(object instanceof Design) {
singleVerticals.add((Design) object);
}
}
return singleVerticals;
}
}
现在,我的Horizontal Recyclerview位于位置0,并且具有静态数据项。 Vertical RecyclerView是动态的,我想为其实现无限滚动。我不知道如何同时管理和更新数据模型(POJO类)。因为两个Recyclerview的对象类型都不同。我应该为两者创建一个通用的POJO类吗?
我面临的另一个问题是,当列表末尾滚动时会有一个混蛋。
答案 0 :(得分:1)
看起来您的父回收站视图仅包含两项:
@Override
public int getItemViewType(int position) {
if (position == 0)
return VIEW_TYPE_DESIGN_TYPES;
if (position == 1)
return VIEW_TYPE_DESIGNS;
return -1;
}
但是从这里我可以看到,如果您有两个以上的项目-您最终将所有后续的所有者都创建为DesignsViewHolder
:
default:
view = inflater.inflate(R.layout.item_home_designs, parent, false);
holder = new DesignsViewHolder(view);
break;
如果打算-在网格项目中具有水平滚动的顶层回收者视图和下一个回收者视图,那么在一个布局中仅包含两个回收者视图是可以的。
<ConstraintLayout or ScrollView> - as u wish
<RecyclerView\> - horizontal one
<RecyclerView\> - vertical one, or with grid layout manager
<\ConstraintLayout or ScrollView>
编辑:在您的问题link的附件中,反之亦然。 垂直滚动,最后一项水平滚动。在这种情况下,您可以切换回收者视图的顺序。
如果您想在回收站中使用真正不同的物品,请考虑使用Adapter Delegates
模式。
注意:在回收者视图中有很多不同的滚动方向并不容易维护。有时会出现diff错误,这些错误很难修复。在某些情况下,更容易以 在回收站视图中具有滚动视图 , 而不是 回收站视图内的回收站视图 。 RecyclerView的内部结构非常复杂,可以优化性能和项目可重复使用性。在大多数情况下,将这种布局与垂直滚动混合起来,您需要5-10个水平滚动的简单对象。 这里有很多要讨论的内容,具体取决于大小写。
看看Joe's problem和一个不错的GitHub lib,它们应该有助于Adapters Delegate模式的理解。祝你好运!