[英]Organize views in RecyclerView by their Position
So this is my FeedAdapter: 这是我的FeedAdapter:
public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_VERTICAL = 1;
private final int VIEW_TYPE_HORIZONTAL = 2;
private final int VIEW_TYPE_AD = 3;
private Context context;
private ArrayList<Object> items;
private List<Vertical> listVertical;
private List<Horizontal> listHorizontal;
private List<Adlist> listAd;
public FeedAdapter(Context context, ArrayList<Object> items, List<Vertical> listVertical,List<Horizontal> listHorizontal, List <Adlist> adList) {
this.context = context;
this.items = items;
this.listVertical = listVertical;
this.listHorizontal = listHorizontal;
this.listAd = adList;
}
I have a horizontal, vertical and a alternative AD view (this has nothing to do with Goggle Ad Service btw) in my Feed. 我的Feed中有一个水平,垂直和另一个AD视图(这与Goggle Ad Service btw无关)。 Items = three different items, so three different views Items =三个不同的项目,因此三个不同的视图
Fot a better understanding: this the onCreate Method: Fot更好地理解:这个onCreate方法:
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
RecyclerView.ViewHolder holder = null;
switch (viewType) {
case VIEW_TYPE_VERTICAL:
view = inflater.inflate(R.layout.newsfeed_vertical,viewGroup,false);
holder = new VerticalViewHolder(view);
break;
case VIEW_TYPE_HORIZONTAL:
view = inflater.inflate(R.layout.newsfeed_horizontal,viewGroup,false);
holder = new HorizontalViewHolder(view);
break;
case VIEW_TYPE_AD:
view = inflater.inflate(R.layout.newsfeed_vertical,viewGroup,false);
holder = new AdViewHolder(view);
break;
an this the onBindViewHolder: 一个这个onBindViewHolder:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == VIEW_TYPE_VERTICAL)
verticalView((VerticalViewHolder) holder);
else if (holder.getItemViewType() == VIEW_TYPE_HORIZONTAL)
horizontalView((HorizontalViewHolder) holder);
else if (holder.getItemViewType() == VIEW_TYPE_AD)
adView((AdViewHolder) holder);
}
public void verticalView (VerticalViewHolder holder ){
VerticalScrollAdapter adapter_v = new VerticalScrollAdapter(listVertical);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setAdapter(adapter_v);
}
public void horizontalView (HorizontalViewHolder holder) {
HorizontalScrollAdapter adapter_h = new HorizontalScrollAdapter(listHorizontal);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
holder.recyclerView.setAdapter(adapter_h);
}
public void adView (AdViewHolder holder) {
AdScrollAdapter adapter_ad = new AdScrollAdapter(adList);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setAdapter(adapter_ad);
}
So I'm using the getItemViewType to understand which view I'm parsing: 所以我使用getItemViewType来理解我正在解析哪个视图:
@Override
public int getItemCount() {
Log.e("items.size", String.valueOf(items.size()));
return items.size();
}
@Override
public int getItemViewType(int position) {
if (items.get(position) instanceof Vertical)
return VIEW_TYPE_VERTICAL;
if (items.get(position) instanceof Horizontal)
return VIEW_TYPE_HORIZONTAL;
if(items.get(position) instanceof Adlist)
return VIEW_TYPE_AD;
return -1;
}
But with this my Feed get's the following structure: 但有了这个我的Feed获得了以下结构:
I'd like to organize my Feed like this 我想像这样组织我的Feed
VERTIAL VIEW [0] = only 1 view (a box basically) with the first position of the LinearLayout Manager
HORIZONTAL VIEW = full list of my horizontal Views here / second position of my LinearLayoutManager
AD VIEW [0] = at the third position
VERTICAL VIEW [1-5]
AD VIEW [1] = @ position 6 in the LinearLayout
VERTICAL VIEW [5- ** ]
So I know I have to work with findViewHolderForAdapterPosition and findViewHolderForLayoutPosition, but: Where do I have put these in my Code? 所以我知道我必须使用findViewHolderForAdapterPosition和findViewHolderForLayoutPosition,但是:我在哪里把它们放在我的代码中?
Sort/construct your items
list so that it has the items in the correct order. 对items
列表进行排序/构建,使其具有正确顺序的项目。
Ideally, you'd start with your three types partitioned (ie one list of Vertical
items, a second list of Horizontal
items, and a third list of Ad
items). 理想情况下,您将从分区的三种类型开始(即一个Vertical
项列表,第二个Horizontal
项列表和第三个Ad
项列表)。 Then you could build your overall list like this: 然后你可以像这样构建你的整体列表:
this.items = new ArrayList<>();
if (listVertical.size() > 0) {
items.add(listVertical.remove(0));
}
if (listHorizontal.size() > 0) {
items.add(listHorizontal.remove(0));
}
while (listVertical.size() > 0 || listAd.size() > 0) {
if (listAd.size() > 0) {
items.add(listAd.remove(0));
}
int count = 5;
while (count > 0 && listVertical.size() > 0) {
items.add(listVertical.remove(0));
}
}
This will add one vertical view, then one horizontal view, and then one ad view + five vertical views in a loop until you "run out" of both ad views and vertical views. 这将添加一个垂直视图,然后是一个水平视图,然后在循环中添加一个广告视图+五个垂直视图,直到您“广告”广告视图和垂直视图为止。 I believe that matches your desired structure. 我相信这符合你想要的结构。
At the end, your three lists will be empty (because you will have remove()
d all items from them). 最后,您的三个列表将为空(因为您将从中remove()
d所有项目)。 If you need to avoid this, you can create copies: 如果您需要避免这种情况,可以创建副本:
List<Vertical> verticalCopy = new ArrayList<>(listVertical);
// and so on
And then you can just use verticalCopy
instead of listVertical
in the above code. 然后你可以在上面的代码中使用verticalCopy
而不是listVertical
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.