简体   繁体   中英

Android: Hiding a view off the top edge of the screen

Would it be possible to hide a view off the top edge of the screen, and only have it appear if the user scrolls upwards?

My first attempt used a scrollview, but it seems that scrollTo() doesn't work unless I used postDelayed (it doesn't even work with Post()). I tried adding it to the scrollview's view tree observer onPreDraw() event and it still doesn't work unless I delay it, so there is an ugly glitch when the activity is first launched.

The second issue is that if the onscreen keyboard is minimized, the view no longer needs to scroll so hiding things by using a scroll offset no longer works. I thought about manipulating the height in code, but this seems pretty hackish.

Is there a better way to do this than by using a scrollview? Alternatively, Does anyone have any tips on the best place to place the scrollTo (the end of onCreate does not work nor the other places I have tried) so I don't need to use postDelayed? That would at least eliminate one glitch.

Thanks!

This is the code I'm using right now, which is the least glitchy but I don't understand why it doesn't work until the third time onPreDraw() is called.

mScrollView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            final int fieldYStart = mFieldIWantAtTheTop.getTop();

            if (mFieldIWantAtTheTopYStart != fieldYStart
             || mScrollView.getScrollY() < 10)
            {
                mFieldIWantAtTheTopYStart = fieldYStart;

                mScrollView.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Log.v("Testing", "scrolling!");
                        mScrollView.scrollTo(0, mFieldIWantAtTheTopYStart);
                        Log.v("Testing", "scroll is now=" + mScrollView.getScrollY());
                    }
                });
            }


            return true;
        }
    });

I also tried using a custom scrollview as mentioned below, but this does not solve the issue of the graphical glitch:

@Override
public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
    super.onMeasure(measureWidthSpec, measureHeightSpec);
    Log.v("Testing", "Scrolling");
    post(
            new Runnable()
            {
                public void run()
                {
                    scrollTo(0, 100);
                    Log.v("Testing", "ScrollY = " + getScrollY()); 
                }
            });       
}

This code works as does the onPreDraw() code above but there is still a glitch when the activity is launched because the activity is first drawn with the scroll at 0.

I haven't tried this, but you may want to create a custom ScrollView and override onMeasure :

ScrollView scroll = new ScrollView(this) {
    @Override
    public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
        super.onMeasure(measureWidthSpec, measureHeightSpec);
        scrollTo(...);
    }
};

It seems like this would be the earliest point that scrollTo would be valid.

Edit - I found this answer , which apparently worked for the asker. Is this the method you tried?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM