2012-07-20 69 views
3

我有一个ScrollView作为父项。里面有LinearLayoutImageViewTabHost。我通过活动更改了TabHost的内容。在更改内容时,Tabhost向下滚动到它自己的选项卡,标题不再可见。我怎样才能防止这一点?滚动查看中的TabHost滚动内容更改

我的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    android:id="@+id/mainScroll" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    android:gravity="center_horizontal" > 


    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="800px" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:padding="5dp" > 

      <ImageView 
       android:id="@+id/header" 
       android:layout_width="800px" 
       android:layout_height="200px" 
       android:src="@drawable/header" /> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="wrap_content" 
       android:layout_height="50px" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="5dp" /> 
     </LinearLayout> 

    </TabHost> 
</ScrollView> 

我的动态改变内容:

public class UeberActivity extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ueber); 
    } 
} 

我怎样才能避免这种滚动?

回答

1

还有另一个类似question我发布了一个答案。基本上我扩展了ScrollView并覆盖computeScrollDeltaToGetChildRectOnScreen

不需要的滚动的原因是,在requestChildFocus默认情况下,ScrollView滚动到焦点视图(例如TabHost)。 computeScrollDeltaToGetChildRectOnScreen用于计算delta滚动以使视图可见。

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