Android 中 Horizo​​ntalScrollView 中的 ListView

发布于 2024-08-14 07:58:29 字数 1118 浏览 3 评论 0原文

我在 Horizo​​ntalScrollView 中有一个 ListView,除了初始加载之外,它工作得很好。我正在从 Web 服务加载列表视图的数据,因此我创建了一个线程来执行此操作。获得数据后,只需调用 _myListAdapter.notifyDataSetChanged();,数据就会在 ListView 中可见。但如果 ListView 距离屏幕较远,则包含的 Horizo​​ntalScrollView 将自动滚动以使该 ListView 可见。如何在不使 ListView 滚动到当前视图的情况下调用 notifyDataSetChanged

以下是我如何拥有布局 XML 文件的想法:

<HorizontalScrollView 
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:scrollbars="none">

        <LinearLayout android:id="@+id/my_layout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

                <ListView android:id="@+id/my_list"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1" />

        </LinearLayout>

</HorizontalScrollView>

I have an ListView inside a HorizontalScrollView and it works perfectly fine, except for the initial load. I'm loading the data for the list view from a web service so I created a thread to do that. Once I have the data, I simply call _myListAdapter.notifyDataSetChanged(); and the data becomes visible in the ListView. But if the ListView is far off the screen, the containing HorizontalScrollView will automatically scroll to make this ListView visible. How can I call notifyDataSetChanged without making the ListView scroll into the current view?

Here's an idea of how I have my layout XML file:

<HorizontalScrollView 
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:scrollbars="none">

        <LinearLayout android:id="@+id/my_layout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

                <ListView android:id="@+id/my_list"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1" />

        </LinearLayout>

</HorizontalScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-08-21 07:58:29

我会尝试让您的 ListView 不可见,直到您调用 notifyDataSetChanged() 为止。

<ListView android:id="@+id/my_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:visibility="gone"/>

然后在代码中:

_myListAdapter.notifyDataSetChanged();
ListView listView = (ListView) findViewById(R.id.my_list);
listView.setVisibility(View.VISIBLE);

不确定这是否有效......不过值得一试。

I would try making your ListView invisible until after you've called notifyDataSetChanged().

<ListView android:id="@+id/my_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:visibility="gone"/>

Then in the code:

_myListAdapter.notifyDataSetChanged();
ListView listView = (ListView) findViewById(R.id.my_list);
listView.setVisibility(View.VISIBLE);

Not sure if this will work... worth a shot, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文