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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.home;
import org.mozilla.gecko.home.HomeConfig.ItemType;
import org.mozilla.gecko.home.HomeConfig.ViewConfig;
import org.mozilla.gecko.home.PanelLayout.FilterManager;
import org.mozilla.gecko.R;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
class PanelViewAdapter extends CursorAdapter {
private static final int VIEW_TYPE_ITEM = 0;
private static final int VIEW_TYPE_BACK = 1;
private final ViewConfig viewConfig;
private FilterManager filterManager;
private final Context context;
public PanelViewAdapter(Context context, ViewConfig viewConfig) {
super(context, null, 0);
this.context = context;
this.viewConfig = viewConfig;
}
public void setFilterManager(FilterManager manager) {
this.filterManager = manager;
}
@Override
public final int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return super.getCount() + (isShowingBack() ? 1 : 0);
}
@Override
public int getItemViewType(int position) {
if (isShowingBack() && position == 0) {
return VIEW_TYPE_BACK;
} else {
return VIEW_TYPE_ITEM;
}
}
@Override
public final View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = newView(parent.getContext(), position, parent);
}
bindView(convertView, position);
return convertView;
}
private View newView(Context context, int position, ViewGroup parent) {
if (getItemViewType(position) == VIEW_TYPE_BACK) {
return new PanelBackItemView(context, viewConfig.getBackImageUrl());
} else {
return PanelItemView.create(context, viewConfig.getItemType());
}
}
private void bindView(View view, int position) {
if (isShowingBack()) {
if (position == 0) {
final PanelBackItemView item = (PanelBackItemView) view;
item.updateFromFilter(filterManager.getPreviousFilter());
return;
}
position--;
}
final Cursor cursor = getCursor(position);
final PanelItemView item = (PanelItemView) view;
item.updateFromCursor(cursor);
}
private boolean isShowingBack() {
return filterManager != null && filterManager.canGoBack();
}
private final Cursor getCursor(int position) {
final Cursor cursor = getCursor();
if (cursor == null || !cursor.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
return cursor;
}
@Override
public final void bindView(View view, Context context, Cursor cursor) {
// Do nothing.
}
@Override
public final View newView(Context context, Cursor cursor, ViewGroup parent) {
return null;
}
}
|