-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentViewPagerAdapter.java
170 lines (148 loc) · 5.64 KB
/
ContentViewPagerAdapter.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ...
import java.util.ArrayList;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class ContentViewPagerAdapter extends PagerAdapter {
// This holds all the currently displayable mViews, in order from left to
// right.
private volatile ArrayList<View> mViews = new ArrayList<View>();
// -- construction
public ContentViewPagerAdapter() {
}
// -----------------------------------------------------------------------------
// Used by ViewPager. "Object" represents the page; tell the ViewPager where
// the
// page should be displayed, from left-to-right. If the page no longer
// exists,
// return POSITION_NONE.
@Override
public int getItemPosition(Object object) {
// System.err.println("onPage getItemPosition " + object.hashCode() + "
// tag " + ((View) object).getTag(R.id.fragment_tag) + " idx " +
// mViews.indexOf(object));
int index = mViews.indexOf(object);
if (index == -1)
return POSITION_NONE;
else
return index;
}
// -----------------------------------------------------------------------------
// Used by ViewPager. Called when ViewPager needs a page to display; it is
// our job
// to add the page to the container, which is normally the ViewPager itself.
// Since
// all our pages are persistent, we simply retrieve it from our "mViews"
// ArrayList.
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mViews.get(position);
System.err.println(
"onPage instantiateItem " + ((Object) v).hashCode() + " tag " + v.getTag(R.id.fragment_tag) + " pos " +
position);
// Prevent crash on "java.lang.IllegalStateException: The specified
// child already has a parent. You must call removeView() on the child's
// parent first",
// which may occur if we slide forward and back too quick
// try {
container.addView(v);
// } catch (IllegalStateException e) {
// e.printStackTrace();
// }
return v;
}
// -----------------------------------------------------------------------------
// Used by ViewPager. Called when ViewPager no longer needs a page to
// display; it
// is our job to remove the page from the container, which is normally the
// ViewPager itself. Since all our pages are persistent, we do nothing to
// the
// contents of our "mViews" ArrayList.
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
System.err.println("onPage destroyItem " + object.hashCode() + " tag "
+ ((View) object).getTag(R.id.fragment_tag) + " pos " + position);
container.removeView((View) object);
}
// -----------------------------------------------------------------------------
// Used by ViewPager; can be used by app as well.
// Returns the total number of pages that the ViewPage can display. This
// must
// never be 0.
@Override
public int getCount() {
return mViews.size();
}
// -----------------------------------------------------------------------------
// Used by ViewPager.
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
// Add "view" to right end of "mViews".
// Returns the position of the new view.
// The app should call this to add pages; not used by ViewPager.
public int addView(View v) {
return addView(v, mViews.size());
}
// -----------------------------------------------------------------------------
// Add "view" at "position" to "mViews".
// Returns position of new view.
// The app should call this to add pages; not used by ViewPager.
public int addView(View v, int position) {
mViews.add(position, v);
return position;
}
/**
* @return True if oldPosition and newPosition does not exceed View array
* size minus 1, otherwise false
*/
public boolean moveView(int oldPosition, int newPosition) {
if (oldPosition < mViews.size() || newPosition < mViews.size()) {
View view = mViews.get(oldPosition);
mViews.remove(oldPosition);
mViews.add(newPosition, view);
return true;
}
return false;
}
// -----------------------------------------------------------------------------
// Removes "view" from "mViews".
// Retuns position of removed view.
// The app should call this to remove pages; not used by ViewPager.
public int removeView(View v) {
return removeView(mViews.indexOf(v));
}
// -----------------------------------------------------------------------------
// Removes the "view" at "position" from "mViews".
// Retuns position of removed view.
// The app should call this to remove pages; not used by ViewPager.
public int removeView(int position) {
// ViewPager doesn't have a delete method; the closest is to set the
// adapter
// again. When doing so, it deletes all its mViews. Then we can delete
// the view
// from from the adapter and finally set the adapter to the pager again.
// Note
// that we set the adapter to null before removing the view from
// "mViews"
// - that's
// because while ViewPager deletes all its mViews, it will call
// destroyItem which
// will in turn cause a null pointer ref.
// mViewPager.setAdapter(null);
mViews.remove(position);
// notifyDataSetChanged();
// mViewPager.setAdapter(this);
return position;
}
// -----------------------------------------------------------------------------
// Returns the "view" at "position".
// The app should call this to retrieve a view; not used by ViewPager.
public View getView(int position) {
return mViews.get(position);
}
public ArrayList<View> getViews() {
return mViews;
}
}