首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

在ScrollView添加一个ListView造成的滚动有关问题的简单解决方法

2012-09-03 
在ScrollView添加一个ListView造成的滚动问题的简单解决办法正常来说,在ScrollView添加一个ListView后在真

在ScrollView添加一个ListView造成的滚动问题的简单解决办法
正常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把ListView的layout_height改成400dip,而不是用match_parent和wrap_content,我发现这样的话ListView就显示的多了很多。所以就产生了把ListView所有的item的高度算出来给ListView设置的想法。下面是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#44444444">
<ScrollView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
    <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:orientation="vertical"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="30dp"
            android:paddingBottom="30dp"
            android:background="#ff888888">
            <TextView
                    android:text="あ"
                    android:textColor="#ffeeeeee"
                    android:textSize="18sp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"></TextView>
            <ListView
                    android:scrollbars="none"
                    android:stackFromBottom="true"
                    android:id="@+id/lv0"
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"></ListView>
                    </LinearLayout>
</ScrollView>
</LinearLayout>
其中的textview是我做的东西要用到的,和方法无关可以不看,然后就是在java中重新设置listview的高度了,目的是把listview“撑”开:
LinearLayout.LayoutParams  lp5 =new LinearLayout.LayoutParam(LayoutParams.FILL_PARENT, listItem.size()*51-1);
其中第一个属性不必说了,第二个是为了计算listview要设置的总高度用的,51是我事先设置好的一行的高度(50)+每行之间的间隔(1)而得来的,listItem.size()是我要显示的行数,用.setLayoutParams(lp5);来重新设置高度,其他别的设置跟以前一样,想要源码我整理完之后贴出来


如果不想写死,那就看下面的文章
Android 解决ListView 和 ScrollView 共存冲突的问题
http://labs.chinamobile.com/mblog/532767_72693?wralxianxrnx
http://blog.liaoxiaoqi.com/?p=503

热点排行