延迟隐藏并显示ActionBar?

时间:2013-05-31 20:41:11

标签: android android-actionbar actionbarsherlock

我想在活动开始时隐藏操作栏,当用户触摸屏幕时显示它几秒钟,然后再次隐藏它。

我想出的就是下面,但我想知道我能做的更好(忽略一些风格问题:魔术数字,逻辑重复等)。

我正在使用actionbarsherlock。

由于


Runnable hideActionbarRunnable = new Runnable() {
        @Override
        public void run() {
            ActionBar bar = getSupportActionBar();
            bar.hide();
        }
    };

    Runnable showActionbarRunnable = new Runnable() {
        @Override
        public void run() {
            ActionBar bar = getSupportActionBar();
            bar.show();
        }
    };

    Runnable animateActionBarHide = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(hideActionbarRunnable,3000);
        }
    };

    Runnable animateActionBarShow = new Runnable() {
        @Override
        public void run() {
            handler.post(showActionbarRunnable);
            handler.postDelayed(hideActionbarRunnable,3000);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(MainActivity.TAG,"CameraActivity:  onResume called");

        Thread t = new Thread(animateActionBarHide);
        t.start();
    }
 @Override
    public boolean onTouchEvent(MotionEvent event) {


        ActionBar bar = getSupportActionBar();
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            Thread t = new Thread(animateActionBarShow);
            t.start();
        }
        else
        {
            Thread t = new Thread(animateActionBarHide);
            t.start();
        }
        return super.onTouchEvent(event);
    }

1 个答案:

答案 0 :(得分:3)

Handler类有一个方法postDelayed(Runnable r, long delayMillis)。这将使它更短,更优雅。