我在布局 xml 中使用 ViewPager 和广告(来自 Admob)。我有两个片段(HomeFragment 和 TeamFragment),一个有 CountDownTimer,另一个有 ListView。正如我们所知,ViewPager 将两者都保存在创建的视图中,因此两者同时处于活动状态。
问题是:当我的 CountDonwTimer 滴答(调用 onTick)时,刷新 ListView 中的项目(行)(调用 ListAdapter 的 getView())。
有趣的是,它仅在广告显示时发生(互联网连接打开)。当没有广告时,一切正常(ListView 没有刷新)。
这不是一个大问题,但它会耗尽电池。
日志:
01-21 15:56:35.150: D/HomeFragment(17366): onTick() in CountDownTimer - just before counterTV.setText(...)
01-21 15:56:35.155: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:35.215: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:35.230: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:35.245: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:35.255: D/ListAdapter in TeamFragment(17366): getView() position: 4
01-21 15:56:35.260: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:35.265: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:35.265: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:35.270: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:35.275: D/ListAdapter in TeamFragment(17366): getView() position: 4
01-21 15:56:40.160: D/HomeFragment(17366): onTick() in CountDownTimer - just before counterTV.setText(...)
01-21 15:56:40.170: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:40.185: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:40.200: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:40.215: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:40.225: D/ListAdapter in TeamFragment(17366): getView() position: 4
01-21 15:56:40.245: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:40.260: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:40.260: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:40.265: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:40.270: D/ListAdapter in TeamFragment(17366): getView() position: 4
01-21 15:56:45.165: D/HomeFragment(17366): onTick() in CountDownTimer - just before counterTV.setText(...)
01-21 15:56:45.170: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:45.185: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:45.200: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:45.215: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:45.225: D/ListAdapter in TeamFragment(17366): getView() position: 4
01-21 15:56:45.245: D/ListAdapter in TeamFragment(17366): getView() position: 0
01-21 15:56:45.260: D/ListAdapter in TeamFragment(17366): getView() position: 1
01-21 15:56:45.265: D/ListAdapter in TeamFragment(17366): getView() position: 2
01-21 15:56:45.270: D/ListAdapter in TeamFragment(17366): getView() position: 3
01-21 15:56:45.270: D/ListAdapter in TeamFragment(17366): getView() position: 4
首页片段:
public class HomeFragment extends SherlockFragment {
(...)
private OpeningCeremonyTimer countDownTimer;
private TextView counterTV;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
(...)
countDownTimer = new OpeningCeremonyTimer(initialTime, 5000);
countDownTimer.start();
return view;
}
private class OpeningCeremonyTimer extends CountDownTimer {
private StringBuilder time = new StringBuilder();
public OpeningCeremonyTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
time.setLength(0);
if (millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if (count > 1)
time.append(count).append(" days ");
else
time.append(count).append(" day ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
Log.d(TAG, "onTick() in CountDownTimer - just before counterTV.setText(...)");
counterTV.setText(time.toString());
}
@Override
public void onFinish() {
counterTV.setText(DateUtils.formatElapsedTime(0));
}
}
}
团队片段:
public class TeamFragment extends SherlockFragment {
(...)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
(...)
TeamFragmentListAdapter matchesListAdapter = new TeamFragmentListAdapter(context, matchesList);
matchesLV.setAdapter(matchesListAdapter);
return view;
}
}
这是我的主要布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="xxx"
ads:loadAdOnCreate="true"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView"/>
</RelativeLayout>