如何添加两个列表视图中滚动视图 [英] How to add two listview in scrollview

查看:120
本文介绍了如何添加两个列表视图中滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am adding two list view in one scroll view, as my both listview(s) will contains data, but the problem I am facing is that when I add listview(S) in scroll view, it only shows one item of each list view.

下面是图像,

中的XML code是:

The XML code is :

<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>

我要的是滚动视图应该向下滚动,如果列表视图中包含更多的项目,因为在这一刻两个列表视图显示一个项目只有:(

All I want is that scroll view should scroll down if the list view contains more items, as at this moment both list views are displaying one item only :(

推荐答案

我用arnp的回答,并提出和示例3 ListView的滚动型。它工作正常。

I used "arnp" 's answer and made and example with 3 listView in scrollview. It works fine.

布局:

<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();
        }


}

这篇关于如何添加两个列表视图中滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆