【问题标题】:TabHost in Scrollview scroll on content changeScrollview 中的 TabHost 滚动内容更改
【发布时间】:2012-07-20 05:38:08
【问题描述】:

我有一个ScrollView 作为父母。里面有一个LinearLayoutImageViewTabHost。我通过活动更改了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);
    }
}

如何避免这种滚动?

【问题讨论】:

    标签: java android scrollview android-tabhost


    【解决方案1】:

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

    不需要滚动的原因是,requestChildFocus ScrollView 默认滚动到焦点视图(例如 TabHost)。 computeScrollDeltaToGetChildRectOnScreen 用于计算增量滚动以显示视图。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多