ScrollView 嵌入Listview,当Listview中的Textview文本多行,高度如何重新计算

如果Listview的Item高度是动态的,例如包含Textview并且超过一行的情况下,如果继续用以上两种方法,那么计算的只是一行TextView的高度

所以这里给了一个很简单的方法,如下:

// 动态改变listView的高度  ,解决TextView超过一行,高度只计算一行的问题
public int setListViewHeightBasedOnChildren(ListView listView) {  

// 获取ListView对应的Adapter 
        ListAdapter listAdapter = listView.getAdapter();  
        if (listAdapter == null) {  
            return 0;  
        }  
        int totalHeight = 0;  
        int listViewWidth = ConstValue.screenWidth (屏幕宽度)- Tool.convertDpToPx(this, 12)(dp转PX);//listView在布局时的宽度  ,screenWidth - 左右的padding - 左右的margin
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);  
        for (int i = 0; i < listAdapter.getCount(); i++) {  
            View listItem = listAdapter.getView(i, null, listView);  

  // 计算子项View 的宽高 
            listItem.measure(widthSpec, 0);  
             // 统计所有子项的总高度     
            int itemHeight = listItem.getMeasuredHeight();  
            totalHeight += itemHeight;  
            
            System.out.println("listView.totalHeight: " + totalHeight + " itemHeight: " + itemHeight);
        }    
        int historyHeight = totalHeight + (listView.getDividerHeight() * listAdapter.getCount() - 1);  

// listView.getDividerHeight()获取子项间分隔符占用的高度     
        System.out.println("listViewHeight = " + historyHeight);
        return historyHeight;
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android应用开发过程,有时候我们需要实现全屏滚动的效果,或者在滚动页面嵌入ListView等组件。下面就来介绍一下如何实现这些效果。 一、全屏滚动 实现全屏滚动需要用到Android系统提供的ScrollView组件。ScrollView可以包含多个子视图,并且可以在垂直方向上进行滚动。下面是一个简单的例子: ``` <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第二行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </ScrollView> ``` 上面的代码,我们将ScrollView作为根布局,然后在ScrollView内部添加了一个垂直方向的LinearLayout,这个LinearLayout包含了多个TextView,每个TextView显示一行文本。运行这个应用,可以看到整个页面可以在垂直方向上滚动。 二、在滚动页面嵌入ListView 有时候我们需要在滚动页面嵌入ListView,这时候可以使用Android系统提供的NestedScrollView组件。NestedScrollViewScrollView的子类,可以包含多个子视图,并且可以在垂直方向上进行滚动。和ScrollView不同的是,NestedScrollView可以嵌套其他可滚动的组件,例如ListView等。 下面是一个简单的例子: ``` <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@null" android:dividerHeight="0dp" android:scrollbars="none"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> ``` 上面的代码,我们将NestedScrollView作为根布局,然后在NestedScrollView内部添加了一个垂直方向的LinearLayout。这个LinearLayout包含了多个TextView和一个ListView。由于ListView也可以滚动,所以我们需要将它的滚动条隐藏掉,然后就可以在滚动页面嵌入ListView了。 以上就是Android开发如何实现全屏滚动和在滚动页面嵌入ListView的方法。希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值