Android 竖直排列显示两个ListView

本文介绍了如何在Android中实现两个ListView竖直排列并随页面滑动的效果,包括三种常见实现方式:动态改变ListView高度、将一个ListView作为另一个的header或footer、使用ExpandableListView。示例代码展示了动态调整高度的方法。
摘要由CSDN通过智能技术生成

如题,想要实现下面的形式,ListView是随着整个页面滑动而滑动的:


网上搜索了很多,大体有一下几种方式:

1.动态改变ListView的高度

2.把其中一个ListView插入到另一个的header或footer中

3.用ExpandableListView实现

stackoverflow上面的回答基本都是动态调整高度的。方法很简单,调用一个方法即可。代码如下:

布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollojt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        androi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中使用两个 ListView 配合使用的方法如下: 1. 首先,在你的布局文件(例如 activity_main.xml)中添加两个 ListView 组件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/listView2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> ``` 2. 在你的 Activity 类中,找到这两个 ListView 组件并设置它们的适配器: ```java public class MainActivity extends AppCompatActivity { private ListView listView1; private ListView listView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到 ListView 组件 listView1 = findViewById(R.id.listView1); listView2 = findViewById(R.id.listView2); // 创建适配器对象 ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getData1()); ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getData2()); // 设置适配器 listView1.setAdapter(adapter1); listView2.setAdapter(adapter2); } // 获取第一个 ListView 的数据 private List<String> getData1() { List<String> data = new ArrayList<>(); // 添加数据到 data 列表中 return data; } // 获取第二个 ListView 的数据 private List<String> getData2() { List<String> data = new ArrayList<>(); // 添加数据到 data 列表中 return data; } } ``` 3. 在 `getData1()` 和 `getData2()` 方法中,你可以根据需要添加数据到两个 ListView 中。可以通过调用 `data.add("your data")` 将数据添加到列表中。 这样,你就可以在 Android Studio 中使用两个 ListView 组件,并根据需要设置它们的数据和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值