【发布时间】:2011-10-20 08:44:37
【问题描述】:
让自己陷入困境,试图在同一个活动中挤入两个 ListView。它使用包含在标准(垂直)LinearLayout 中的两个单独的 ListFragment 来工作。
问题是,这两个列表加起来比屏幕长,因此第二个列表被部分隐藏了。从视觉上看,用户希望向上拖动整个屏幕并显示第二个列表。但是这两个列表有自己的内部滚动,它们不允许整个屏幕作为一个整体滚动。
幸运的是,这些列表实际上包含的项目很少(平均每个 5 个)。因此,理论上,我可以填充几个 LinearLayouts 容器。问题是,列表显示的数据来自游标并且是动态的。虽然我知道 CursorAdapter 的 newView() 和 bindView() 方法,但我不太明白如何将适配器连接到 LinearLayout 容器而不是 ListViews。 IE。 CursorAdapter 如何知道它必须从它在光标中找到的 5 个项目中创建 5 个行项目?在哪里创建循环遍历光标项并在 LinearLayout 容器中创建项?当 Cursor 中的数据发生变化时,如何刷新 LinearLayout 的内容?我发现的所有示例都将这些问题巧妙地包装到 ListActivity 提供的 ListView 中,但我不能使用 ListViews!
我很困惑!
手动
编辑:这是遵循 breceivemail 建议时 (Fragment)Activity 的 xml 布局。在 breceivemail 的建议之前,注释掉的是原始的 LinearLayout 容器。还应该注意的是,整个活动又包含在 TabHost 中,但我不知道这是否对手头的问题有任何影响。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!--
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
-->
<TextView android:id="@+id/title"
android:textSize="36sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/SelectPlayer"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Playing"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#999999"/>
<fragment android:name="com.myDomain.myApp.PlayerListFragment"
android:id="@+id/playing"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Reserve"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#999999"/>
<fragment android:name="com.myDomain.myApp.PlayerListFragment"
android:id="@+id/reserve"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
【问题讨论】:
-
只创建布局不是更好的解决方案,让两个 ListView 适合屏幕大小吗?
-
这是一个公平的评论@KasperMoerch,但列表项的高度设计得异常大。列表项需要快速点击,尤其是第一个列表中的项,这是 UI 原则,即越大的东西越快到达并与之交互。
-
这很公平 - 但 ListView 的一般要点是它是可滚动的,这意味着您可以滚动到列表中所需的点,这也意味着如果您在一个屏幕上有两个 ListView,它们每个只需要占据一半的屏幕。我觉得你正在制作某种游戏,这很酷。您不应该将此视为批评或我很固执——我只是想挑战您对 UI 设计的选择。可能/可能没有更好的解决方案。
-
不用担心@KasperMoerch,我有点卡在这个问题上,从不同的方向看很有用,所以我没有将您的评论视为批评,也没有想到您很固执。我实际上并不是在制作游戏,而是在编写一个应用程序来为我的球队/俱乐部获取篮球统计数据(在此过程中顺便学习了 java+android)。
-
第一个列表是场上球员的名单(通常是 5 个,但理论上可能会降到 2 个)第二个列表是作为替补席位的球员,介于 0 之间7. 当某事发生时,用户点击一名球员并被引导到另一个活动,他可以在其中选择发生了什么,即2分球或犯规。从理论上讲,似乎只有实际比赛的人才能触发事件,这表明只有在场球员名单是必要的。在实践中,可能需要增加关于一个人作为备用坐姿的统计数据。
标签: android android-layout android-listview android-cursoradapter