surbhi choudhary
surbhi choudhary

Reputation: 1

Android TabHost Auto Scroll vertically in side ScrollView on TabChange?

I am using TabHost inside ScrollView in my Activity but when ever I select tab it automatically scrolls my view vertically to end.

enter image description here

Upvotes: 0

Views: 646

Answers (2)

Andy
Andy

Reputation: 161

Based on Er Pragati Singh's answer I did not override requestChildFocus(View child, View focused) but computeScrollDeltaToGetChildRectOnScreen(Rect rect).

Overriding requestChildFocus will also prevent activating the on screen keyboard when touching an EditText which already has focus, while computeScrollDeltaToGetChildRectOnScreen is only used to calculate the delta scroll inside requestChildFocus to bring the View in sight. So overriding this function keeps all other routines intact.

Java:

public class MyScrollView extends ScrollView {
    public MyScrollView(Context context) {
        super(context);

    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        // This function calculates the scroll delta to bring the focused view on screen.
        // -> To prevent unsolicited scrolling to the focued view we'll just return 0 here.
        //
        return 0;
    }
}

XML:

<YOUR.PAKAGE.NAME.MyScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
</YOUR.PAKAGE.NAME.MyScrollView>

Upvotes: 1

Pragati Singh
Pragati Singh

Reputation: 2523

In this case child view getting focus due to that it get scrolled upward.

for resolve this you need to create custom ScrollView that extend ScrollView. code snipt will look like this.

public class MyScrollView extends ScrollView {


    public MyScrollView(Context context) {
        super(context);

    }



    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }


    @Override
    public void requestChildFocus(View child, View focused) {
       // if (focused instanceof TabHost)   // here 
            return;
        //super.requestChildFocus(child, focused);
// here you need to return instead of **super.requestChildFocus(child, focused);**
    }

and xml looks like this

  <com.views.widget.MyScrollView
        android:focusable="false"
        android:focusableInTouchMode="false"
    android:id="@+id/root_scroll_view"
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="wrap_content">

</com.views.widget.MyScrollView >

Upvotes: 1

Related Questions