Android使用2个listview链接在一起

标签 android android-layout listview android-arrayadapter

我是 Android 新手,目前正在阅读大量文档以了解如何开始。我在此处提问之前尝试过搜索,但找不到相关信息。

我的问题是在同一个 Activity 中有 2 个 ListView 。

带有类别列表的 Listview A

带有详细数据的 Listview B(启动时显示的值基于 listviewA 的类别 1)

当点击 listviewA 的一个项目时,listViewB 的数据发生变化以反射(reflect)新的选择并显示新的详细信息列表。

有人可以给我正确的方向或指向涵盖该主题的教程的链接吗? 抱歉,我目前无法发布任何代码。

最佳答案

我不确定您尝试制作的布局类型是否对用户友好。如果您更改为 ExpandableListView,它将为用户提供更丰富的 UI 体验。 .您可以查看教程 here .使用它可以让您有机会通过互动吸引用户。

enter image description here

无论如何,如果您确定要使用 ListView,那么只需创建一个根 LinearLayout 并在其中使用两个 ListView。但是把第二个当成listview就没有意义了。相反,不是将第二个控件设为 ListView,而是采用 TextView 来显示详细信息。提供两个布局的高度(ListViewTextView) 根据您的要求。

看看这个 tutorial

2 ListView :

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>
    </LinearLayout>

1 个 ListView 1 个 TextView :

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>
            <TextView android:id="@+id/TextView1" android:text="details"/>
    </LinearLayout>

更新:

请详细解释一下您的问题。因为,在您的场景中, ListView 之间的数据是相互连接的。比如说,如果 ListView A 是关于产品的,那么 ListView B 是关于 ProductDetails 的。因此,您需要声明一个类变量,例如 ProductID,它是两个数据之间的公共(public)实体,并在 ListView A< 的 ``getView()` 中设置此变量 setOnClickListener/strong> 并在此之后获取与此相关的数据,并使用此查询的结果(可能是 SQL 数据库或 RESTful 服务器)设置 ListView B 的适配器。

关于Android使用2个listview链接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295303/

相关文章:

android - 与从大型数据库中选择所有行以填充 ListView 相关的性能问题

java - Android ListView 适配器崩溃问题/重复

java - Android在ListView中加载图片

java - 从 Android Studio 上的 sqlite 表中删除行不起作用

java - 处理 FirebaseRecyclerAdapter 中的位置

java - 随机变量导致的运行时错误

android - 使用 appcompat V21 主题覆盖按钮样式

android - 使用 RxJava 和 Retrofit 的周期性 HTTP 请求

android - 全屏隐藏标题(AppCompatActivity)

android - 如图所示的水平 ScrollView