Revision cc5ec229
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikScoresViewPager.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 |
import android.support.annotation.NonNull;
|
|
22 |
import android.content.Context;
|
|
23 | 23 |
import android.support.v4.app.FragmentActivity; |
24 |
import android.support.v4.view.PagerAdapter; |
|
25 | 24 |
import android.support.v4.view.ViewPager; |
26 |
import android.view.View; |
|
27 |
import android.view.ViewGroup; |
|
25 |
import android.util.AttributeSet; |
|
28 | 26 |
|
29 | 27 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
30 | 28 |
|
31 |
class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader.Receiver
|
|
29 |
class RubikScoresViewPager extends ViewPager implements RubikScoresDownloader.Receiver
|
|
32 | 30 |
{ |
33 |
private FragmentActivity mAct;
|
|
31 |
FragmentActivity mAct; |
|
34 | 32 |
private RubikScoresView[] mViews; |
33 |
private int mSelected; |
|
35 | 34 |
|
36 | 35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
37 | 36 |
|
38 |
RubikScoresViewPager(FragmentActivity act, ViewPager viewPager) |
|
37 |
public RubikScoresViewPager(Context context, AttributeSet attrs) |
|
38 |
{ |
|
39 |
super(context,attrs); |
|
40 |
|
|
41 |
mViews = new RubikScoresView[RubikSize.LENGTH]; |
|
42 |
mSelected = 0; |
|
43 |
|
|
44 |
addOnPageChangeListener(new ViewPager.OnPageChangeListener() |
|
45 |
{ |
|
46 |
@Override |
|
47 |
public void onPageSelected(int position) |
|
48 |
{ |
|
49 |
mSelected = position; |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) |
|
54 |
{ } |
|
55 |
|
|
56 |
@Override |
|
57 |
public void onPageScrollStateChanged(int state) |
|
58 |
{ } |
|
59 |
}); |
|
60 |
} |
|
61 |
|
|
62 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
63 |
|
|
64 |
void startDownload(FragmentActivity act) |
|
39 | 65 |
{ |
40 | 66 |
mAct = act; |
41 |
mViews = new RubikScoresView[getCount()]; |
|
42 |
viewPager.setAdapter(this); |
|
43 |
viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 ); |
|
67 |
|
|
68 |
RubikScoresDownloader downloader = new RubikScoresDownloader(); |
|
69 |
downloader.download(this, mAct.getResources(), mAct.getPackageName()); |
|
70 |
} |
|
71 |
|
|
72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
73 |
|
|
74 |
void setView(int position, RubikScoresView view) |
|
75 |
{ |
|
76 |
if( position>=0 && position<RubikSize.LENGTH ) |
|
77 |
{ |
|
78 |
mViews[position] = view; |
|
79 |
} |
|
44 | 80 |
} |
45 | 81 |
|
46 | 82 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
47 | 83 |
|
48 |
public void receive(final String[][][][] scores)
|
|
84 |
public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
|
|
49 | 85 |
{ |
50 | 86 |
mAct.runOnUiThread(new Runnable() |
51 | 87 |
{ |
52 | 88 |
@Override |
53 | 89 |
public void run() |
54 | 90 |
{ |
91 |
long time1 = System.currentTimeMillis(); |
|
92 |
|
|
55 | 93 |
for(int i=0; i<RubikSize.LENGTH; i++) |
56 | 94 |
{ |
57 |
mViews[i].prepareView(mAct,scores[i]);
|
|
95 |
mViews[i].prepareView(mAct, country[i], name[i], time[i]);
|
|
58 | 96 |
} |
97 |
|
|
98 |
long time2 = System.currentTimeMillis(); |
|
99 |
|
|
100 |
android.util.Log.e("dd", "time="+(time2-time1)); |
|
59 | 101 |
} |
60 | 102 |
}); |
61 | 103 |
} |
... | ... | |
76 | 118 |
} |
77 | 119 |
}); |
78 | 120 |
} |
79 |
|
|
80 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
81 |
|
|
82 |
@Override |
|
83 |
@NonNull |
|
84 |
public Object instantiateItem(@NonNull ViewGroup collection, int position) |
|
85 |
{ |
|
86 |
mViews[position] = new RubikScoresView(mAct,position); |
|
87 |
collection.addView(mViews[position]); |
|
88 |
|
|
89 |
boolean allCreated = true; |
|
90 |
|
|
91 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
92 |
{ |
|
93 |
if( mViews[i]==null ) |
|
94 |
{ |
|
95 |
allCreated = false; |
|
96 |
break; |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
if( allCreated ) |
|
101 |
{ |
|
102 |
RubikScoresDownloader downloader = new RubikScoresDownloader(); |
|
103 |
downloader.download(this); |
|
104 |
} |
|
105 |
|
|
106 |
return mViews[position]; |
|
107 |
} |
|
108 |
|
|
109 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
110 |
|
|
111 |
@Override |
|
112 |
public void destroyItem(ViewGroup collection, int position, @NonNull Object view) |
|
113 |
{ |
|
114 |
collection.removeView((View) view); |
|
115 |
} |
|
116 |
|
|
117 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
118 |
|
|
119 |
@Override |
|
120 |
public int getCount() |
|
121 |
{ |
|
122 |
return RubikSize.LENGTH; |
|
123 |
} |
|
124 |
|
|
125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
126 |
|
|
127 |
@Override |
|
128 |
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) |
|
129 |
{ |
|
130 |
return view == object; |
|
131 |
} |
|
132 | 121 |
} |
Also available in: Unified diff
Downloading High Scores: actually downloading scores!