如何在scrollview中添加两个listview

时间:2012-03-06 16:26:15

标签: android listview layout scrollview

我在一个滚动视图中添加两个列表视图,因为我的两个列表视图将包含数据,但我面临的问题是当我在滚动视图中添加listview(S)时,它只显示一个项目每个列表视图。

这是图片,

enter image description here

XML代码是:

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/layoutFooter7"
        android:layout_below="@+id/linArrange" >

        <RelativeLayout
            android:id="@+id/linListViews"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/lstviewUnBlockVenues"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
                android:divider="#00000000"
                android:dividerHeight="0dp"
                android:fadeScrollbars="false" >

            </ListView>

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lstviewUnBlockVenues"
                android:background="#ABCDEF"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Blocked Venues"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#061218" />
            </LinearLayout>



            <ListView
                android:id="@+id/lstviewBlockedVenues"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearLayout1"
                android:cacheColorHint="#00000000"
                android:divider="#00000000"
                android:dividerHeight="0dp"
                android:fadeScrollbars="false" >

            </ListView>
        </RelativeLayout>
    </ScrollView>

我想要的是,如果列表视图包含更多项目,滚动视图应向下滚动,因为此时两个列表视图仅显示一个项目:(

4 个答案:

答案 0 :(得分:6)

我使用了“arnp”的回答,并在scrollview中使用3 listView制作了示例。它工作正常。

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

            <ListView
                android:id="@+id/list3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

活动:

package com.example.listviewtest;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list1 = (ListView) findViewById(R.id.list1);
        ListView list2 = (ListView) findViewById(R.id.list2);
        ListView list3 = (ListView) findViewById(R.id.list3);


        String[] items1 = { "Alfa1", "Alfa2", "Alfa3", "Alfa4", "Alfa5", "Alfa6", "Alfa7", "Alfa8", "Alfa9", "Alfa10", "Alfa11", "Alfa12", "Alfa13", "Alfa14", "Alfa15", "Alfa16", "Alfa17"  };
        String[] items2 = { "Beta1", "Beta2", "Beta3", "Beta4", "Beta5", "Beta6", "Beta7", "Beta8", "Beta9", "Beta10", "Beta11", "Beta12", "Beta13", "Beta14", "Beta15", "Beta16", "Beta17" };
        String[] items3 = { "Teta1", "Teta2", "Teta3", "Teta4", "Teta5", "Teta6", "Teta7", "Teta8", "Teta9", "Teta10", "Teta11", "Teta12", "Teta13", "Teta14", "Teta15", "Teta16", "Teta17" };

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items1);
        list1.setAdapter(adapter1);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items2);
        list2.setAdapter(adapter2);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items3);
        list3.setAdapter(adapter3);

        setListViewHeightBasedOnChildren(list1);
        setListViewHeightBasedOnChildren(list2);
        setListViewHeightBasedOnChildren(list3);

    }

     public void setListViewHeightBasedOnChildren(ListView listView) {
         ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter(); 
            if (listAdapter == null) {
                // pre-condition
                return;
            }

            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }


}

答案 1 :(得分:1)

尝试使用 LinearLayout 权重。你的问题是wrap_content不会使ListViews填满屏幕的50%。

这样的事情:

    <LinearLayout
        android:id="@+id/linListViews"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="2"
        >

        <ListView
            android:id="@+id/lstviewUnBlockVenues"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight: 1;
            android:cacheColorHint="#00000000"
            android:divider="#00000000"
            android:dividerHeight="0dp"
            android:fadeScrollbars="false" >

        </ListView>


        <ListView
            android:id="@+id/lstviewBlockedVenues"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:divider="#00000000"
            android:dividerHeight="0dp"
            android:fadeScrollbars="false" >

        </ListView>
    </LinearLayout>

答案 2 :(得分:1)

最好修改UI。

在Android中将一个滚动小部件放在另一个小部件中确实会产生很多问题。有一种解决方法,但这会让滚动成为用户的痛苦。

有关详细信息,您可以点击google search

上显示的任何结果

答案 3 :(得分:0)

您可以找到关于使用ExpandableListView here

的最佳方法的教程