Search code examples
androidandroid-tabhostandroid-scrollview

TabHost inside a ScrollView forces it it to scroll to the bottom


My question is very similar to this unanswered one, with some small differences that I will explain:

TabHost inside of a Scrollview: always scrolls down when a Tab is clicked

My ScrollView initiates scrolled to the bottom, showing the TabHost content instead of the ones on top of it (you can see the screenshots in the linked question, it's very similar).

If I manually scroll up, clicking in different tabs doesn't affect the ScrollView, the problem is only in the first time it's shown. This is the difference between the other question.

Things I tried with no success:

  1. Make the top components inside the ScrollView focusable, so they would get the focus instead of the tab.
  2. Force the ScrollView to scroll to the top in onResume() method with sv.fullScroll(View.FOCUS_UP); or sv.scrollTo(0, 0);. Also, no luck in the initialization, but subsequent calls to onResume() effectively scrolls it to the top.

Any ideas on why this is happening? Tips on how to further investigate are also very welcome.


Solution

  • Probably too late to help you Pedro, but I did figure out a solution that worked for me. I noticed in the TabHost source that it requests Focus if nothing else in the view has focus. So I made sure that the first component in the Scroll view was focusable and requested focus for it.

    // Get the first component and make sure it is focusable. Note you have to use setFocusableInTouchMode and not setFocusable for this to work.
    TextView v = (TextView) view.findViewById(R.id.first_component_in_view);
    v.setFocusableInTouchMode(true);
    v.requestFocus();
    

    Hopefully this will help someone.