滚动视图中添加列表视图 [英] Add listview within scrollview

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

问题描述

我想有一个滚动列表还包括列表视图。我到处做研究,没有解决方案尚未:(按低于code,当我添加滚动型,ListView的高度变小(单排)。我想在ListView的高度是动态的(基于内容),也使整个页面滚动的。我怎么做到这一点?还是有方法来创建动态线性/相对列表界面?请建议。

XML code:

 <?XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
<的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:paddingTop =@扪/ activity_horizo​​ntal_margin
    机器人:方向=垂直>        < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin>        <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ kollege_name
        机器人:宽=170dp/>        <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:重力=右
        机器人:文字=@字符串/ GRADE_VALUE
        机器人:宽=120dp/>        <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignRight =@ + ID / textView2
        机器人:layout_below =@ + ID / textView2
        机器人:文字=@字符串/ code_value/>
        < / RelativeLayout的>        <查看
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1DP
        机器人:layout_marginTop =10dp
        机器人:layout_marginBottom =10dp
        机器人:背景=@机器人:彩色/ darker_gray/>
        <的TextView
        机器人:ID =@ + ID / textView4
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ your_fit
        机器人:TEXTSIZE =12SP/>        < ListView控件
        机器人:ID =@ + ID /列表视图
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>        <查看
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1DP
        机器人:layout_marginTop =10dp
        机器人:layout_marginBottom =10dp
        机器人:背景=@机器人:彩色/ darker_gray/>        < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin>        <的TextView
            机器人:ID =@ + ID / textView10
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/学费
            机器人:TEXTSIZE =12SP/>        <的TextView
            机器人:ID =@ + ID / textView11
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentTop =真            机器人:文字=@字符串/ tuition_value/>        < / RelativeLayout的>
        <查看
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1DP
        机器人:layout_marginTop =10dp
        机器人:layout_marginBottom =10dp
        机器人:背景=@机器人:彩色/ darker_gray/>        < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin>        <的TextView
        机器人:ID =@ + ID / textView12
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/展示位置
        机器人:TEXTSIZE =12SP/>        <的TextView
        机器人:ID =@ + ID / textView13
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView12
        机器人:文字=@字符串/ EXTC
        机器人:layout_marginTop =3DP/>        <的TextView
        机器人:ID =@ + ID / textView14
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView13
        机器人:文字=@字符串/ IT
        机器人:layout_marginTop =3DP/>        <的TextView
        机器人:ID =@ + ID / textView15
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView14
        机器人:文字=@字符串/ COMP
        机器人:layout_marginTop =3DP/>        <的TextView
        机器人:ID =@ + ID / textView16
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView15
        机器人:文字=@字符串/ MECH
        机器人:layout_marginTop =3DP/>        <的TextView
        机器人:ID =@ + ID / textView17
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView16
        机器人:文字=@字符串/出师表
        机器人:layout_marginTop =3DP/>
        < / RelativeLayout的>        <查看
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1DP
        机器人:layout_marginTop =10dp
        机器人:layout_marginBottom =10dp
        机器人:背景=@机器人:彩色/ darker_gray/>
        < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin>        <的TextView
        机器人:ID =@ + ID / textView18
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/展示位置
        机器人:TEXTSIZE =12SP/>        <的TextView
        机器人:ID =@ + ID / textView19
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView18
        机器人:文字=@字符串/ EXTC
        机器人:layout_marginTop =3DP/>        <的TextView
        机器人:ID =@ + ID / textView20
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView19
        机器人:文字=@字符串/ IT
        机器人:layout_marginTop =3DP/>
        < / RelativeLayout的>
        < / LinearLayout中>        < /滚动型>< / LinearLayout中>


解决方案

这是更好地设置不同势视图类型ListView的适配器内或使用ListView的页脚和页眉。

您也可以使用此方法,将解决你面临与滚动型的ListView

问题

 公共静态无效updateListViewHeight(ListView控件myListView){
     ListAdapter myListAdapter = myListView.getAdapter();
     如果(myListAdapter == NULL){
              返回;
     }
    //获取ListView的高度
    INT totalHeight = 0;
    INT adapterCount = myListAdapter.getCount()
    对于(INT大小= 0;大小< adapterCount;大小++){
        查看的listItem = myListAdapter.getView(大小,空,myListView);
        listItem.measure(0,0);
        totalHeight + = listItem.getMeasuredHeight();
    }
    //改变的ListView的高度
    ViewGroup.LayoutParams PARAMS = myListView.getLayoutParams();
    params.height = totalHeight +(myListView.getDividerHeight()*(adapterCount - 1));
    myListView.setLayoutParams(PARAMS);
}

I want to have a scrollable list which also includes list view. I did research everywhere and no solution yet :( As per the code below, when I add ScrollView, the listview height becomes smaller (single row). I want the ListView height to be dynamic (based on the content) and also make the complete page scrollable. How do I achieve this? Or is there a way to create dynamic linear / relative list UI? Please suggest.

XML code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:orientation="vertical" >

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">

        <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/kollege_name"
        android:width="170dp" />

        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:text="@string/grade_value"
        android:width="120dp" />

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="@string/code_value" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <TextView
        android:id="@+id/textView4"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/your_fit"
        android:textSize="12sp" />

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

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tuition"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"

            android:text="@string/tuition_value" />

        </RelativeLayout>


        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView12"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView13"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView14"
        android:text="@string/COMP"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView15"
        android:text="@string/MECH"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView16"
        android:text="@string/INST"
        android:layout_marginTop="3dp" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView18"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView19"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />
        </RelativeLayout>
        </LinearLayout>

        </ScrollView>

</LinearLayout>

解决方案

It's better to set diffrent view type inside the ListView Adapter or use the ListView Footer and Header.

Also you can use this Method which will fix the issue you are facing with ListView in ScrollView

public static void updateListViewHeight(ListView myListView) {
     ListAdapter myListAdapter = myListView.getAdapter();
     if (myListAdapter == null) {            
              return;
     }
    //get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount()
    for (int size = 0; size < adapterCount ; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView 
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);
}

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

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