Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RegularAdapterStrategy.java « gallery « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7fc34d03bc8f96ceb0040e85c0fa66c0ffb96238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.mapswithme.maps.gallery;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.ViewGroup;

import java.util.List;

import static com.mapswithme.maps.gallery.Constants.TYPE_MORE;
import static com.mapswithme.maps.gallery.Constants.TYPE_PRODUCT;

public abstract class RegularAdapterStrategy<T extends RegularAdapterStrategy.Item>
    extends AdapterStrategy<Holders.BaseViewHolder<T>, T>
{
  private static final int MAX_ITEMS_BY_DEFAULT = 5;

  public RegularAdapterStrategy(@NonNull List<T> items, @Nullable T moreItem,
                                @Nullable ItemSelectedListener<T> listener)
  {
    this(items, moreItem, listener, MAX_ITEMS_BY_DEFAULT);
  }

  public RegularAdapterStrategy(@NonNull List<T> items, @Nullable T moreItem,
                                @Nullable ItemSelectedListener<T> listener, int maxItems)
  {
    super(listener);
    boolean showMoreItem = moreItem != null && items.size() >= maxItems;
    int size = showMoreItem ? maxItems : items.size();
    for (int i = 0; i < size; i++)
    {
      T product = items.get(i);
      mItems.add(product);
    }
    if (showMoreItem)
      mItems.add(moreItem);
  }

  @NonNull
  @Override
  Holders.BaseViewHolder<T> createViewHolder(@NonNull ViewGroup parent, int viewType)
  {
    switch (viewType)
    {
      case TYPE_PRODUCT:
        return createProductViewHolder(parent, viewType);
      case TYPE_MORE:
        return createMoreProductsViewHolder(parent, viewType);
      default:
        throw new UnsupportedOperationException("This strategy doesn't support specified view type: "
                                                + viewType);
    }
  }

  @Override
  protected void onBindViewHolder(Holders.BaseViewHolder<T> holder, int position)
  {
    holder.bind(mItems.get(position));
  }

  @Override
  protected int getItemViewType(int position)
  {
    return mItems.get(position).getType();
  }

  @NonNull
  protected abstract Holders.BaseViewHolder<T> createProductViewHolder(@NonNull ViewGroup parent,
                                                                       int viewType);
  @NonNull
  protected abstract Holders.BaseViewHolder<T> createMoreProductsViewHolder(@NonNull ViewGroup parent,
                                                                            int viewType);

  public static class Item extends Items.Item
  {
    @Constants.ViewType
    private final int mType;

    public Item(@Constants.ViewType int type, @NonNull String title,
                @Nullable String subtitle, @Nullable String url)
    {
      super(title, url, subtitle);
      mType = type;
    }

    public int getType()
    {
      return mType;
    }
  }
}