Android TabHost Auto在TabChange的侧面ScrollView中垂直滚动?

时间:2016-03-31 12:02:32

标签: android android-tabhost android-scrollview

我在我的活动TabHost中使用ScrollView但是当我选择标签时,它会自动垂直滚动我的视图结束。

enter image description here

2 个答案:

答案 0 :(得分:1)

在这种情况下,子视图会因为向上滚动而获得焦点。

要解决此问题,您需要创建扩展ScrollView的自定义ScrollView。 代码片段看起来像这样。

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);**
    }

和xml看起来像这样

  <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 >

答案 1 :(得分:1)

根据Er Pragati Singh的回答,我没有覆盖requestChildFocus(View child, View focused),而是computeScrollDeltaToGetChildRectOnScreen(Rect rect)

覆盖requestChildFocus也会阻止在触摸已经具有焦点的EditText时激活屏幕键盘,而computeScrollDeltaToGetChildRectOnScreen仅用于计算requestChildFocus内的增量滚动以带来视图在望。因此,覆盖此函数可以保持所有其他例程的完整性。

爪哇:

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>