简体   繁体   English

带有多个ListView的LinearLayout可滚动

[英]LinearLayout scrollable with multiple ListView

This is my layout: 这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="@string/section_1" />

    <ListView
        android:id="@+id/lv_section_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="@string/section_2" />

    <ListView
        android:id="@+id/lv_section_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

</LinearLayout>

The problem is that when first section has many items and it takes up the whole screen, I can not scroll to see second section . 问题是,当第一部分有很多项目并且占据了整个屏幕时,我无法滚动查看第二部分 With a quick search I just discovered that I can not use ListView within ScollView . 通过快速搜索,我发现无法在ScollView使用ListView

Is there any way to leave this LinearLayout scrollable so I can see all sections that can be added? 有什么办法可以使LinearLayout可滚动状态,以便可以看到所有可以添加的部分? I need something similar to the iOS UITableView , several sections and headers. 我需要类似于iOS UITableView的内容 ,包括几个部分和标题。

Thank you in advance. 先感谢您。

Ok, just to have a list with multiple sections, what I could do to solve my problem was quite simple: 好的,只要有一个包含多个部分的列表,我可以做的解决问题的方法很简单:

I left just one ListView and created a class CustomAdapter . 我只剩下一个ListView并创建了一个类CustomAdapter And added items with different types: 并添加了不同类型的项目:

ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();

HashMap map = new HashMap<String, String>();
map.put("type", "section");
map.put("title", "Section 1");
listItems.add(map);

map = new HashMap<String, String>();
map.put("type", "item");
map.put("title", "Item A");
map.put("detail", "Detail A");
listItems.add(map);

Set adapter to my ListView : 将适配器设置为我的ListView

CustomAdapter adapter = new CustomAdapter(context, R.layout.result_list, listItems);
ListView lv = (ListView) view.findViewById(R.id.resultlist);
lv.setAdapter(adapter);

In my CustomAdapter I set a different style for each type, section or item. 在我的CustomAdapter我为每种类型,部分或项目设置了不同的样式。 Note that I just want something to differentiate items in a ListView . 请注意,我只需要一些东西来区分ListView项目。

Still accept suggestions for this problem if my solution is too ugly :) 如果我的解决方案太难看了,仍然接受有关此问题的建议:)

Use layout_weight="1" and layout_height="0dp" for both ListViews which will split your entire screen into two parts, so that you can see both the sections. 对两个ListView都使用layout_weight =“ 1”和layout_height =“ 0dp”,这会将您的整个屏幕分为两部分,以便您可以看到这两个部分。 If you have to show TextViews as Headers on top of listviews, then take two linear layouts with above said weights and inside Linear Layout, place textview and listview with Vertical orientation. 如果必须在列表视图的顶部将TextViews显示为Header,则采用上述权重上方的两个线性布局,并在Linear Layout内部,将Textview和Listview放置为Vertical方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM