CountDownTimer,ViewPager,Ad和常量刷新ListView项 - 奇怪的行为

时间:2013-01-22 15:17:40

标签: android android-fragments android-viewpager ads countdowntimer

我在布局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

HomeFragment:

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));

        }

    }
}

TeamFragment:

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>

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 我刚刚在我的ListView中将layout_height =“wrap_content”更改为layout_height =“match_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>

有关ListViews中wrap_conent和match_parent的更多信息,请访问:http://www.youtube.com/watch?v=wDBM6wVEO70