- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我面临一个非常常见的问题:我布置了一个 Activity ,现在它应该在这个 ScrollView
中显示一些项目。正常的方法是使用现有的 ListAdapter
,将其连接到 ListView
和 BOOM 我会有我的项目列表.
但是您不应该将嵌套的 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/
我有一个 LinearLayout,上面有几个 View 。所以我想要实现的是复制这个 LinearLayout 并在某个时候使用它。但是,当我对原始 LinearLayout 执行更改(例如方法 r
我试着做一个像这样的简单布局 仅显示第一个 TextView(“hello”)。我做错了什么? 最佳答案 制作内部布局wrap_content的l
我想构建某种 Twitter 应用程序。在 DashboardActivity 中,我需要在每次单击“发布”按钮时添加一个状态框。我的仪表板 xml 如下所示: -->header
我试图在另一个 LinearLayout 中插入一个 LinearLayout。我不知道我的做法对不对。 我需要在不使用通货膨胀的情况下尝试这种方式。 LinearLayout addre
我的 Android UI 中只有一个 ImageView 和一个 TextView。目前 TextView 与 ImageView 在同一个 LinearLayout 中,因此文本有时会跑到 2 行
我正在尝试让“播放”图标的图像显示在我在 LinearLayout 中的列表项的右侧。 但是,由于某种原因,图像出现在
我有两个 LinearLayout 结构如下。 问题是第二个 LinearLayout (id=goi) 没有按预期显示。我尝试将顶部布局更改为 Relativ
我以编程方式创建了很多 LinearLayout 并将它们放在另一个 LinearLayout 上: public void loadList(){ LinearLayout linearLa
我想创建按钮 1 到 9,并且我想循环执行此操作。但在每 3 个计数中,我想创建一个新的 LinearLayout。 final LinearLayout[] ll2 = new LinearLa
我需要创建 X 个 linearLayout - 每个 linearLayout 包含文本(项目名称)、位图(项目位图图像)。 因此,我创建了一些 for 循环来创建动态 linearLayout 和
您好,我是 Android 开发的初学者。我制作了一个 LinearLayout 以美观的方式显示不同的信息。我想要一个带有 ScrollView 的屏幕,我可以在其中添加任意数量的 LinearLa
我是 Android 编程的新手,我正在尝试设置一个包含四个组件的 LinearLayout。 目前看起来是这样的: XML:
首先,这是我的 activity_main.xml 文件的结构: 相对布局 线性布局 fragment (谷歌地图) 线性布局 搜索栏 TextView 图片按钮 我试图将第二个线性布局(不包含 ma
我正在创建一个自定义复合布局,它由一个可点击的水平 LinearLayout 组成,其中包含一个 ImageView 和两个 TextView。最终,我希望能够使用单个字段引用整个内容,并根据用户 A
我在 main.xml 中有一个 LinearLayout: 我制作了另一个 XML 文件,名为 item_box.xml: 基本上,我想通过代码(以编程方式
我有一个 LinearLayout,它应该包含一个项目列表(每个项目都是一个 LinearLayout)。 问题是子项目 (LinearLayouts) 没有显示在另一个下面:只有第一个是可见的。 这
下午好 我正在尝试像这样进行倒计时: 为此,我制作了两个水平的 LinearLayout,一个用于数值,另一个放置在下方,用于标签,例如“DAYS”或“HOURS”。 我的目标是在数值中间对齐标签,如
我得到了这个应用程序(顺便说一句,它和完成的一样好),我需要在其中动态地将 LinearLayout View 添加到 LinearLayout 容器。它的工作方式是用户在 EditText 中写一些
在 Udacity Android 初学者类(class)之后,我想向 list_item View 添加一个音频图标按钮。现在 list_item.xml 有一个父级水平线性布局和一个嵌套的垂直线性
我得到了一个相对布局,其中包含另一个我用来替换“标题”的相对布局,一个稍后用作“控制面板”的线性布局,以及一个 horizontalScrollView,其中 horizontalScroll
我是一名优秀的程序员,十分优秀!