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

12

我有一个包含RelativeLayoutScrollView,这个RelativeLayout里面有几个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);
    }
}

点击“跨国”复选框时,将调用crossCountryCheckboxClicked()方法,并在XML布局文件中的CheckBox元素上指定android:onClick属性。

以下是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>

每当ScrollView的子元素的可见性在View.GONE和View.Visible之间改变时,如何停止ScrollView滚动到顶部?


现在不应用可见性是什么意思? - faridghar
从您的xml文件中删除此内容,android:visibility="gone"。 - RajeshVijayakumar
我已经从布局文件中删除了所有的“android:visibility =”gone“”,并使用您提供的布局文件,但仍然出现相同的问题。此外,当屏幕加载时,我需要某些视图最初处于隐藏状态,这就是为什么我在XML文件中使用了“android:visibility =”gone“”。 - faridghar
@user1692855 我也遇到了同样的问题。你找到解决方案了吗? - Adil Malik
1
抱歉,我很抱歉这个问题。 但是,FYAI,我找到了解决方案。在我们的recyclerView xml中加入android:descendantFocusability="blocksDescendants"即可。 我从https://dev59.com/27Pma4cB1Zd3GeqPvLuw#56041829获得了这个解决方案。 - zihadrizkyef
显示剩余4条评论
2个回答

1
我认为您想要滚动滚动条以查看新的可见视图 crossCountryHoursTextBox,只需像这样滚动您的滚动条即可。
// scrollview at visible textbox
yourscroll.smoothScrollBy(0,crossCountryHoursTextBox.getTop()); 

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>

这没有帮助。相同的行为仍然存在。 - faridghar
我完全复制并粘贴了您当前的答案。我忽略了一个警告,该警告说“此ScrollView布局或其RelativeLayout父级是无用的”,但仍然没有任何变化。您是否怀疑问题出在哪里,还是正在尝试试错方法? - faridghar
2
我刚刚尝试了使用LinearLayout和垂直方向,它可以工作!正如我之前所说,我想让它与RelativeLayout一起工作,所以我会等待您的解决方案。感谢您的帮助。 - faridghar
我刚刚通过将RelativeLayout更改为LinearLayout来使它正常工作。有谁能解释一下RelativeLayout为什么会有这种行为,并且如何在不切换到LinearLayout的情况下使其工作?谢谢。 - gcl1
相对布局在你的安卓编程中非常有用,当你要以这种方式使用时。 - RajeshVijayakumar
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接