2012-07-20 68 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>