gpt4 book ai didi

android - 使用 ListAdapter 在 ScrollView 布局中填充 LinearLayout

转载 作者:IT老高 更新时间:2023-10-28 13:06:10 25 4
gpt4 key购买 nike

我面临一个非常常见的问题:我布置了一个 Activity ,现在它应该在这个 ScrollView 中显示一些项目。正常的方法是使用现有的 ListAdapter,将其连接到 ListViewBOOM 我会有我的项目列表.

但是您不应该将嵌套的 ListView 放在 ScrollView 中,因为它会破坏滚动 - 甚至 Android Lint 都会提示它。

这是我的问题:

如何将 ListAdapter 连接到 LinearLayout 或类似的东西?

我知道这个解决方案不会针对很多项目进行扩展,但我的列表非常短(< 10 个项目),因此实际上不需要重用 View 。性能方面,我可以忍受将所有 View 直接放入 LinearLayout

我想出的一个解决方案是将我现有的 Activity 布局放在 ListView 的 headerView 部分。但这感觉像是在滥用这种机制,所以我正在寻找更清洁的解决方案。

想法?

更新:为了激发正确的方向,我添加了一个示例布局来显示我的问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible">


<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/news_detail_layout_side_padding"
android:paddingRight="@dimen/news_detail_layout_side_padding"
android:paddingTop="@dimen/news_detail_layout_vertical_padding"
android:paddingBottom="@dimen/news_detail_layout_vertical_padding"
>

<TextView
android:id="@+id/news_detail_date"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text="LALALA"
android:textSize="@dimen/news_detail_date_height"
android:textColor="@color/font_black"
/>

<Gallery
android:id="@+id/news_detail_image"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingTop="5dip"
android:paddingBottom="5dip"
/>

<TextView
android:id="@+id/news_detail_headline"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text="Some awesome headline"
android:textSize="@dimen/news_detail_headline_height"
android:textColor="@color/font_black"
android:paddingTop="@dimen/news_detail_headline_paddingTop"
android:paddingBottom="@dimen/news_detail_headline_paddingBottom"
/>

<TextView
android:id="@+id/news_detail_content"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Here comes a lot of text so the scrollview is really needed."
android:textSize="@dimen/news_detail_content_height"
android:textColor="@color/font_black"
/>

<!---
HERE I NEED THE LIST OF ITEMS PROVIDED BY THE EXISTING ADAPTER.
They should be positioned at the end of the content, so making the scrollview smaller is not an option.
---->

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

更新 2 我更改了标题以使其更易于理解(投反对票,doh!)。

最佳答案

您可能应该手动将您的项目添加到 LinearLayout:

LinearLayout layout = ... // Your linear layout.
ListAdapter adapter = ... // Your adapter.

final int adapterCount = adapter.getCount();

for (int i = 0; i < adapterCount; i++) {
View item = adapter.getView(i, null, null);
layout.addView(item);
}

编辑:当我需要显示大约 200 个重要的列表项时,我拒绝了这种方法,它非常慢 - Nexus 4 需要大约 2 秒来显示我的“列表”,这是 Not Acceptable .所以我转向了弗洛的带标题的方法。它工作得更快,因为 ListView 是在用户滚动时按需创建的,而不是在创建 View 时。

简历:在布局中手动添加 View 更容易编码(因此可能会减少移动部件和错误),但会遇到性能问题,因此如果您有 50 个或更多 View ,我建议使用标题方法。

示例。基本上 Activity (或 fragment )布局转换为这样的东西(不再需要 ScrollView):

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_top_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

然后在 onCreateView() 中(我将使用带有 fragment 的示例)您需要添加一个标题 View ,然后设置一个适配器(我假设标题资源 ID 是 header_layout ):

ListView listView = (ListView) inflater.inflate(R.layout.my_top_layout, container, false);
View header = inflater.inflate(R.layout.header_layout, null);
// Initialize your header here.
listView.addHeaderView(header, null, false);

BaseAdapter adapter = // ... Initialize your adapter.
listView.setAdapter(adapter);

// Just as a bonus - if you want to do something with your list items:
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// You can just use listView instead of parent casted to ListView.
if (position >= ((ListView) parent).getHeaderViewsCount()) {
// Note the usage of getItemAtPosition() instead of adapter's getItem() because
// the latter does not take into account the header (which has position 0).
Object obj = parent.getItemAtPosition(position);
// Do something with your object.
}
}
});

关于android - 使用 ListAdapter 在 ScrollView 布局中填充 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12405575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com