在RecyclerView中按位置组织视图

时间:2019-05-26 19:32:03

标签: java android android-recyclerview

这是我的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广告服务无关)。 项目=三个不同的项目,所以三个不同的视图

需要更好的理解:这是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;

这是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来了解要解析的视图:

@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的结构如下:

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

我想要这样组织我的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- ** ] 

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

1 个答案:

答案 0 :(得分:0)

排序/构造您的items列表,以便其顺序正确。

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

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

这将循环添加一个垂直视图,一个水平视图,一个广告视图和五个垂直视图,直到您“用尽”广告视图和垂直视图。我相信这符合您想要的结构。

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

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

然后您只需在上面的代码中使用verticalCopy而不是listVertical