Android RecyclerView ViewType
리사이클러뷰에서 아이템의 타입이 두가지 이상일 때
Item.class 생성
1
2
3
4
5
6
7
8
9
10
11
public class Item<T> {
private T item;
public Item(T item) {
this.item = item;
}
public T getItem() {
return item;
}
}
RecyclerView Adapter
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
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_PAGER = 0;
public static final int TYPE_NORMAL = 1;
private final FragmentManager mFragmentManager;
private List<Item> mItems;
public RecyclerViewAdapter(FragmentManager fm, List<Item> items) {
mFragmentManager = fm;
mItems = items;
}
static class PagerViewHolder extends RecyclerView.ViewHolder {
ViewPager viewPager;
public PagerViewHolder(View itemView) {
super(itemView);
viewPager = (ViewPager) itemView.findViewById(R.id.view_pager);
}
}
static class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
}
}
@Override
public int getItemViewType(int position) {
return position == 0 ? TYPE_PAGER : TYPE_NORMAL;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_PAGER:
return new PagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager, parent, false));
case TYPE_NORMAL:
return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyler, parent, false));
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TYPE_PAGER:
List<Fragment> item = (List<Fragment>) mItems.get(position).getItem();
PagerViewHolder holder1 = (PagerViewHolder) holder;
ViewPagerAdapter adapter = new ViewPagerAdapter(mFragmentManager,item);
holder1.viewPager.setAdapter(adapter);
break;
case TYPE_NORMAL:
String item1 = (String) mItems.get(position).getItem();
ItemViewHolder holder2 = (ItemViewHolder) holder;
holder2.textView.setText(item1);
break;
}
}
@Override
public int getItemCount() {
return mItems.size();
}
}
Item 타입으로 데이터를 생성자를 통해 받고, onBindViewHolder에서 viewType에 따라 데이터를 set 해주면 된다.