【发布时间】:2016-04-02 10:11:30
【问题描述】:
有关此“问题”的答案,请参阅本条目的底部。
在我的应用程序中,我扩充了一些 xml 视图并将它们添加到 ScrollView 内的 LinearLayout 列表中。 XML 如下所示:
<ScrollView
android:layout_width="0dp"
android:layout_weight="19"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
然后,我尝试将重点放在我放入“列表”中的其中一个视图上。这是通过使用以下代码完成的:
view.getParent().requestChildFocus(view, view);
如果上面的代码导致向下滚动,则焦点视图位于屏幕底部,如果导致向上滚动,则焦点视图位于屏幕顶部。
如果列表的长度允许,有没有办法让焦点视图始终位于屏幕顶部?
编辑:这行得通!查看已接受的答案。
XML
<ScrollView
android:id="@+id/scroll_list"
android:layout_width="0dp"
android:layout_weight="19"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
将示例代码放在您的活动或片段中的某个位置
view = findViewById(get_view_that_should_have_focus);
ScrollView scrollView = findViewById(R.id.scroll_list);
scrollView.smoothScrollTo(0, view.getTop());
【问题讨论】:
-
您能否提供更多细节,也许是正在发生的事情的图片?
标签: android android-linearlayout android-scrollview