在RecyclerView中按位置组织视图 [英] Organize views in RecyclerView by their Position

查看:48
本文介绍了在RecyclerView中按位置组织视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的FeedAdapter:

So this is my 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;
    }

我的Feed中有一个水平,垂直和备用的AD视图(与Goggle广告服务无关). 项目=三个不同的项目,所以三个不同的视图

I have a horizontal, vertical and a alternative AD view (this has nothing to do with Goggle Ad Service btw) in my Feed. Items = three different items, so three different views

需要更好的理解:这是onCreate方法:

Fot a better understanding: this the onCreate Method:

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;

这是onBindViewHolder:

an this the 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);

}

所以我正在使用getItemViewType来了解我正在解析的视图:

So I'm using the getItemViewType to understand which view I'm parsing:

@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;
}

但是我的Feed有了以下结构:

But with this my Feed get's the following structure:

  • 垂直视图
  • 水平视图
  • 广告观看

我想像这样组织我的Feed

I'd like to organize my Feed like this

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- ** ] 

所以我知道我必须使用findViewHolderForAdapterPosition和findViewHolderForLayoutPosition,但是:在我的代码中应该放在哪里?

So I know I have to work with findViewHolderForAdapterPosition and findViewHolderForLayoutPosition, but: Where do I have put these in my Code?

推荐答案

对您的items列表进行排序/构造,以使其具有正确顺序的项目.

Sort/construct your items list so that it has the items in the correct order.

理想情况下,您将从三种类型的分区开始(即,一个Vertical项列表,第二个Horizontal项列表和第三个Ad项列表).然后,您可以像这样建立整体列表:

Ideally, you'd start with your three types partitioned (i.e. one list of Vertical items, a second list of Horizontal items, and a third list of Ad items). 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));
    }
}

这将循环添加一个垂直视图,然后一个水平视图,然后一个广告视图+ 5个垂直视图,直到您用尽"广告视图和垂直视图.我相信这符合您想要的结构.

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.

最后,您的三个列表将为空(因为您将remove() d个项目全部从其中剔除).如果需要避免这种情况,可以创建副本:

At the end, your three lists will be empty (because you will have remove()d all items from them). If you need to avoid this, you can create copies:

List<Vertical> verticalCopy = new ArrayList<>(listVertical);
// and so on

然后您可以只使用verticalCopy代替上面的代码中的listVertical.

And then you can just use verticalCopy instead of listVertical in the above code.

这篇关于在RecyclerView中按位置组织视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆