9

オブザーバー メソッドが呼び出されていません。ViewPagerCycler私は古い学校の方法でテストし、完璧に機能しました。手伝ってくれてありがとう。

public final class ViewPagerCycler implements LifecycleObserver {

    private static final int PERIOD = 3000;

    private Timer mTimer;
    private TimerTask mTask;
    private Activity mActivity;
    private ViewPager mPager;

    public ViewPagerCycler(Activity activity, ViewPager pager) {
        mActivity = activity;
        mPager = pager;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void start() {
        int count = mPager
            .getAdapter()
            .getCount();

        mTimer = new Timer();
        mTask = new TimerTask() {
            @Override public void run() {
                mActivity.runOnUiThread(new TimerTask() {
                    @Override public void run() {
                        mPager.setCurrentItem((mPager.getCurrentItem() + 1) % count, true);
                    }
                });
            }
        };
        mTimer.schedule(mTask, PERIOD, PERIOD);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void stop() {
        mTask.cancel();
        mTimer.cancel();
        mTimer.purge();
    }
}

活動は要約するだけです。私はsを追加しませんでしたfindViewById

public class SummariziedActivity extends Activity implements LifecycleOwner {
   private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);

   @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       mRegistry.addObserver(new ViewPagerCycler(mPager));
   }
}

build.gradle

compile "android.arch.lifecycle:runtime:1.0.0-alpha2"
compile "android.arch.lifecycle:extensions:1.0.0-alpha2"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha2"
4

4 に答える 4