在子视图上调用setVisibility(View.VISIBLE)后,Android ScrollView会滚动到顶部

时间:2012-11-01 00:48:12

标签: android scroll scrollview visibility

我的ScrollView里面有一个RelativeLayout。这个RelativeLayout里面有几个View个。这几个View中的一个是CheckBox。当CheckBox被选中/取消选中时,其他一些View应相应地显示/消失。出现和消失的工作正常,但每次目标View出现或消失时,ScrollView一直滚动到顶部,我被迫向下滚动以查看发生了什么。

我用来控制可见性的代码是:

public void crossCountryCheckboxClicked(View view)
{
    CheckBox crossCountryCheckBox = (CheckBox)findViewById(R.id.checkbox_cross_country);
    EditText crossCountryHoursTextBox = (EditText)findViewById(R.id.cross_country_hours);
    if (crossCountryCheckBox.isChecked())
    {
        crossCountryHoursTextBox.setVisibility(View.VISIBLE);
    }
    else
    {
        crossCountryHoursTextBox.setVisibility(View.GONE);
    }
}

并通过在XML布局文件中的crossCountryCheckboxClicked()元素上指定android:onClick属性来调用CheckBox方法。

这是XML布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/logbook.app.activities"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/flight_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/aeroplane_type"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/flight_date"
        android:hint="@string/aeroplane_type" />

    <EditText
        android:id="@+id/aeroplane_registration"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/aeroplane_type"
        android:hint="@string/aeroplane_registration" />

    <EditText
        android:id="@+id/pilot_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/aeroplane_registration"
        android:hint="@string/pilot_name" />

    <EditText
        android:id="@+id/copilot_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pilot_name"
        android:hint="@string/copilot_name" />

    <EditText
        android:id="@+id/from_aerodome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/copilot_name"
        android:hint="@string/from_aerodome" />

    <EditText
        android:id="@+id/to_aerodome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/copilot_name"
        android:layout_toRightOf="@id/from_aerodome"
        android:hint="@string/to_aerodome" />

    <EditText
        android:id="@+id/remarks"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/from_aerodome"
        android:hint="@string/remarks" />

    <Spinner
        android:id="@+id/aircraft_engine_type_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/remarks" />

    <Spinner
        android:id="@+id/time_of_day_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/aircraft_engine_type_spinner" />

    <Spinner
        android:id="@+id/pilot_role_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/time_of_day_spinner" />

    <TextView
        android:id="@+id/on_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pilot_role_spinner"
        android:hint="@string/on_time_hint" />

    <TextView
        android:id="@+id/takeoff_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/on_time"
        android:hint="@string/takeoff_time_hint" />

    <TextView
        android:id="@+id/landing_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/takeoff_time"
        android:hint="@string/landing_time_hint" />

    <TextView
        android:id="@+id/off_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/landing_time"
        android:hint="@string/off_time_hint" />

    <Button
        android:id="@+id/button_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/off_time"
        android:onClick="recordTime"
        android:padding="5dp"
        android:text="@string/button_record" />

    <Button
        android:id="@+id/button_reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/off_time"
        android:layout_toRightOf="@id/button_record"
        android:onClick="resetAllTimes"
        android:padding="5dp"
        android:text="@string/button_reset" />

    <CheckBox
        android:id="@+id/checkbox_ifr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_record"
        android:text="@string/checkbox_ifr"
        android:onClick="ifrCheckboxClicked" />

    <CheckBox
        android:id="@+id/checkbox_cross_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_record"
        android:layout_toRightOf="@id/checkbox_ifr"
        android:text="@string/checkbox_cross_country"
        android:onClick="crossCountryCheckboxClicked" />

    <Spinner
        android:id="@+id/ifr_type_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox_ifr"
        android:visibility="gone" />

    <TextView
        android:id="@+id/label_ifr_approaches"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ifr_type_spinner"
        android:hint="@string/ifr_landings_label"
        android:visibility="gone" />

    <logbook.app.custom.widgets.NumberSelector
        android:id="@+id/ifr_landings_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/label_ifr_approaches"
        android:visibility="gone"
        custom:orientation="vertical" />

    <EditText
        android:id="@+id/ifr_hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ifr_landings_selector"
        android:hint="@string/ifr_hours_hint"
        android:inputType="number"
        android:visibility="gone" />

    <EditText
        android:id="@+id/cross_country_hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ifr_hours"
        android:hint="@string/cross_country_hours_hint"
        android:inputType="number"
        android:visibility="gone" />

    <TextView
        android:id="@+id/label_takeoffs_landings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cross_country_hours"
        android:hint="@string/takeoffs_landings_label" />

    <logbook.app.custom.widgets.NumberSelector
        android:id="@+id/landings_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/label_takeoffs_landings"
        custom:orientation="horizontal" />

    <Button
        android:id="@+id/button_create_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/landings_selector"
        android:onClick="createLog"
        android:padding="5dp"
        android:text="@string/button_create_log" />

</RelativeLayout>

每当ScrollViewView.GONE之间的某个孩子的可见度发生变化时,如何阻止View.Visible滚动到顶部?

2 个答案:

答案 0 :(得分:0)

试试这个:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation = "vertical" >

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
   android:layout_height="wrap_content" >

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/flight_date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/aeroplane_type"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/flight_date"
    android:hint="@string/aeroplane_type" />

<EditText
    android:id="@+id/aeroplane_registration"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/aeroplane_type"
    android:hint="@string/aeroplane_registration" />

<EditText
    android:id="@+id/pilot_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/aeroplane_registration"
    android:hint="@string/pilot_name" />

<EditText
    android:id="@+id/copilot_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/pilot_name"
    android:hint="@string/copilot_name" />

<EditText
    android:id="@+id/from_aerodome"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/copilot_name"
    android:hint="@string/from_aerodome" />

<EditText
    android:id="@+id/to_aerodome"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/copilot_name"
    android:layout_toRightOf="@id/from_aerodome"
    android:hint="@string/to_aerodome" />

<EditText
    android:id="@+id/remarks"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/from_aerodome"
    android:hint="@string/remarks" />

<Spinner
    android:id="@+id/aircraft_engine_type_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/remarks" />

<Spinner
    android:id="@+id/time_of_day_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/aircraft_engine_type_spinner" />

<Spinner
    android:id="@+id/pilot_role_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/time_of_day_spinner" />

<TextView
    android:id="@+id/on_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/pilot_role_spinner"
    android:hint="@string/on_time_hint" />

<TextView
    android:id="@+id/takeoff_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/on_time"
    android:hint="@string/takeoff_time_hint" />

<TextView
    android:id="@+id/landing_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/takeoff_time"
    android:hint="@string/landing_time_hint" />

<TextView
    android:id="@+id/off_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/landing_time"
    android:hint="@string/off_time_hint" />

<Button
    android:id="@+id/button_record"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/off_time"
    android:onClick="recordTime"
    android:padding="5dp"
    android:text="@string/button_record" />

<Button
    android:id="@+id/button_reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/off_time"
    android:layout_toRightOf="@id/button_record"
    android:onClick="resetAllTimes"
    android:padding="5dp"
    android:text="@string/button_reset" />

<CheckBox
    android:id="@+id/checkbox_ifr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button_record"
    android:text="@string/checkbox_ifr"
    android:onClick="ifrCheckboxClicked" />

<CheckBox
    android:id="@+id/checkbox_cross_country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button_record"
    android:layout_toRightOf="@id/checkbox_ifr"
    android:text="@string/checkbox_cross_country"
    android:onClick="crossCountryCheckboxClicked" />

<Spinner
    android:id="@+id/ifr_type_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/checkbox_ifr"
    android:visibility="gone" />

<TextView
    android:id="@+id/label_ifr_approaches"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ifr_type_spinner"
    android:hint="@string/ifr_landings_label"
    android:visibility="gone" />

<logbook.app.custom.widgets.NumberSelector
    android:id="@+id/ifr_landings_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/label_ifr_approaches"
    android:visibility="gone"
    custom:orientation="vertical" />

<EditText
    android:id="@+id/ifr_hours"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ifr_landings_selector"
    android:hint="@string/ifr_hours_hint"
    android:inputType="number"
    android:visibility="gone" />

<EditText
    android:id="@+id/cross_country_hours"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ifr_hours"
    android:hint="@string/cross_country_hours_hint"
    android:inputType="number"
    android:visibility="gone" />

<TextView
    android:id="@+id/label_takeoffs_landings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/cross_country_hours"
    android:hint="@string/takeoffs_landings_label" />

<logbook.app.custom.widgets.NumberSelector
    android:id="@+id/landings_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/label_takeoffs_landings"
    custom:orientation="horizontal" />

<Button
    android:id="@+id/button_create_log"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/landings_selector"
    android:onClick="createLog"
    android:padding="5dp"
    android:text="@string/button_create_log" />

</RelativeLayout>
</ScrollView>
</LinearLayout>

答案 1 :(得分:0)

我想你想在新的可见视图{@ 1}}滚动滚动条 为此,只需滚动你的滚动条

crossCountryHoursTextBox