Android 倒数计时器、ViewPager、广告和持续刷新ListView项目-奇怪的行为
我在布局xml中使用ViewPager和广告(来自Admob)。 我有两个片段(HomeFragment和TeamFragment),一个有倒计时,另一个有ListView。正如我们所知,ViewPager将这两个视图与已创建的视图保持一致,因此这两个视图同时处于活动状态 问题在于:当my CountDonwTimer勾选(调用onTick)时,ListView中的项目(行)将被刷新(调用ListAdapter中的getView()。 有趣的是,只有当广告显示(互联网连接打开)时才会发生。没有广告时,一切正常(没有从ListView刷新) 这不是什么大问题,但会耗尽电池电量 日志: HomeFragment:Android 倒数计时器、ViewPager、广告和持续刷新ListView项目-奇怪的行为,android,android-fragments,android-viewpager,ads,countdowntimer,Android,Android Fragments,Android Viewpager,Ads,Countdowntimer,我在布局xml中使用ViewPager和广告(来自Admob)。 我有两个片段(HomeFragment和TeamFragment),一个有倒计时,另一个有ListView。正如我们所知,ViewPager将这两个视图与已创建的视图保持一致,因此这两个视图同时处于活动状态 问题在于:当my CountDonwTimer勾选(调用onTick)时,ListView中的项目(行)将被刷新(调用ListAdapter中的getView()。 有趣的是,只有当广告显示(互联网连接打开)时才会发生。没有广
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>
我解决了这个问题。
我刚刚在ListView中将layout\u height=“wrap\u content”更改为layout\u height=“match\u parent”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/groupsLV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp" >
</ListView>
有关ListView中的换行内容和匹配父项的详细信息,请参见:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/groupsLV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp" >
</ListView>