JakubW
JakubW

Reputation: 1121

Hide view every 5 seconds with postDelayed

PROBLEM

I have a horizontalListView that is showing user all the images loaded to ViewPager. Its situated on the bottom of the screen. What I want to do is for horizontalListView to hide when its not being used for more then 5 sec.

How it should work:

How its working right now:

CODE

HorizontalListView and Runnable

    horizontalListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            hideViewHandler.removeCallbacks(mRunnable);
            hideViewHandler.postDelayed(mRunnable,5*1000);
            return true;
        }
    });

    mRunnable = new StoppableRunnable() {
        @Override
        public void stoppableRun() {
            hideAnimation();
        }
    };

TapListener that is set on Image responsible for showing horizontalListView

  private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        hListView.clearAnimation();
        if (((ViewGroup.MarginLayoutParams) hListView.getLayoutParams()).bottomMargin < 0) {
            expandAnimation();
            hideViewHandler.postDelayed(mRunnable,5*1000);
        } else {
            hideAnimation();
        }
        return true;
    }
}

Upvotes: 3

Views: 341

Answers (4)

JakubW
JakubW

Reputation: 1121

I have managed to fix the problem myself.

FIX

The main reason why my onTouchListener did not work as intended was that it had not been triggered at all. OnClickListener inside horizontalListView adapter was consuming the trigger.

Now I am passing the info that the click occurred inside onClickListener and do all the things in different method.

Upvotes: 0

niktszczegolny
niktszczegolny

Reputation: 53

I think the source of you problem might be calling removeCallbacks before postDelayed. The thread itself is fired after u clear the queue.

Upvotes: 0

shalafi
shalafi

Reputation: 3996

You have to check inside the listener for the MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL to only trigger the runnable after them.

Your current code is posting the runnable on any event, but you want to do it only when the user has stopped using the view.

BTW: If you return true from the TouchListener it means that you have consumed the event and the event chain will stop. Most likely the ScrollView will not even scroll, since the event will not be propagated to it.

Upvotes: 1

Sjd
Sjd

Reputation: 1261

You can do something like this..

   yourScreenLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
            if (horizontalListView.getVisibility() == View.INVISIBLE) {
                horizontalListView.setVisibility(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (horizontalListView.getVisibility() == View.VISIBLE)
                            horizontalListView.setVisibility(View.INVISIBLE);
                    }
                }, 5000);
            } else {
                horizontalListView.setVisibility(View.INVISIBLE);
            }

        }
    });

Upvotes: 0

Related Questions