简体   繁体   English

如何以编程方式滚动ScrollView?

[英]How to scroll programatically a ScrollView?

I have a little problem with a ScrollView. 我对ScrollView有一点问题。 I have a layout for an activity which is made with a ScrollView. 我有一个使用ScrollView进行的活动的布局。 This scrollview contains two ListViews. 此滚动视图包含两个ListView。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootViewGroup" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:scrollbars="vertical">

<LinearLayout android:orientation="vertical"
    android:gravity="top" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="@drawable/cmb_bg">

    <ListView android:id="@+id/accountsListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:scrollbars="none" />

    <ListView android:id="@+id/cardsListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:scrollbars="none" />

</LinearLayout>

In the onCreate method of my activity I compute ListViews height according their contents. 在我的活动的onCreate方法中,我根据其内容计算ListViews的高度。

During execution, on activity launch, ScrollView is already scrolled a bit. 在执行期间,在活动启动时,ScrollView已经滚动了一点。

So I tried, at the end of onCreate to call method scrollTo(0, 0) , but it does not change anything. 因此,我尝试在onCreate的末尾调用方法scrollTo(0, 0) ,但是它没有任何改变。

Any ideas? 有任何想法吗?

AHHH!!! 啊! Remove ScrollView and leave only ONE ListView in your activity. 删除ScrollView并在活动中仅保留一个ListView。 It's not intended to work this way .If you want to have different content in your ONE listView - modify your adapter, or change the UI But never, ever put different scrollable containers on the same screen/inside each other here is a related post. 如果您想在ONE listView中包含不同的内容,请修改适配器或更改UI,但是永远不要将不同的可滚动容器放在同一屏幕上/彼此内部,这是相关的文章。 http://groups.google.com/group/android-developers/browse_thread/thread/e7c8df374fe31733# In theory you can slap two listview on the screen, but expect things to be weird. http://groups.google.com/group/android-developers/browse_thread/thread/e7c8df374fe31733#从理论上讲,您可以在屏幕上拍两个listview,但希望情况会很奇怪。 And i would really redesign it with with either separators or expanders or just with one ListView. 我真的会使用分隔符或扩展器或仅使用一个ListView重新设计它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM