ScorllView嵌套ListView时动态设置高度时的问题

一、今天遇到了ListView在自适应扩充高度时候出现的问题.
1.网上查的很多,都是说写个Util类,来自扩充Listview的高度.但是,一旦这个Listview是嵌套在ScorllView里的,那么ListView和ScorllView的滑动会冲突.导致Listview只显示一小行.
最初的时候,我是给Listview的Adapter设置了Minheight.但是Item里的文本(TextView一旦多了.)就会导致显示文字不全.
在这里要说明下,要想全部高度显示,textview和父布局文件必须:wrap_content

废话不多说了,上代码了.
网上有很多说明了,但是作为小白,可能不知道怎么做.
一、,就是要重新自定义一个Listview.继承ListView.重写其中的onMeasure方法.

因为ListView扩展时因为很多原因,可能达不到完美的效果.

*在写第三个MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}的时候,AS会提示API兼容的问题.*
自动调整下就OK了.

<code class="hljs java has-numbering">
<span class="hljs-keyword">import</span> android.content.Context;
<span class="hljs-keyword">import</span> android.util.AttributeSet;
<span class="hljs-keyword">import</span> android.widget.ListView;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyListView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ListView</span>{</span>
    <span class="hljs-keyword">public</span> <span class="hljs-title">MyListView</span>(Context context) {
        <span class="hljs-keyword">super</span>(context);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-title">MyListView</span>(Context context, AttributeSet attrs) {
        <span class="hljs-keyword">super</span>(context, attrs);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-title">MyListView</span>(Context context, AttributeSet attrs, <span class="hljs-keyword">int</span> defStyleAttr) {
        <span class="hljs-keyword">super</span>(context, attrs, defStyleAttr);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-title">MyListView</span>(Context context, AttributeSet attrs, <span class="hljs-keyword">int</span> defStyleAttr, <span class="hljs-keyword">int</span> defStyleRes) {
        <span class="hljs-keyword">super</span>(context, attrs, defStyleAttr, defStyleRes);
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMeasure</span>(<span class="hljs-keyword">int</span> widthMeasureSpec, <span class="hljs-keyword">int</span> heightMeasureSpec) {
        <span class="hljs-keyword">int</span> expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> <span class="hljs-number">2</span>,
                MeasureSpec.AT_MOST);
        <span class="hljs-keyword">super</span>.onMeasure(widthMeasureSpec, expandSpec);
    }
}
</code>

这是在ListViewUtil里写的方法.

<code class="hljs java has-numbering"> <span class="hljs-javadoc">/**
     * 动态设置ListView的高度
     *
     *<span class="hljs-javadoctag"> @param</span> listView
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setListViewHeightBasedOnChildren</span>(ListView listView, ListAdapter adapter) {
        <span class="hljs-keyword">if</span> (listView == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">return</span>;
        <span class="hljs-keyword">if</span> (adapter == <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">return</span>;
        }
        <span class="hljs-keyword">int</span> totalHeight = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, <span class="hljs-keyword">null</span>, listView);
            listItem.measure(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>);<span class="hljs-comment">//计算每项   Item的高度</span>
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - <span class="hljs-number">1</span>));
        <span class="hljs-comment">//循环完之后,要计算出getDividerHeight(空白处占得height)   才是Listview的高度</span>
        listView.setLayoutParams(params);

    }</code>

然后是最后一步:
一定要在获取数据后:

<code class="hljs cs has-numbering"> adapter = <span class="hljs-keyword">new</span> MyListAdapter(<span class="hljs-keyword">this</span>, CommentmData);
            mListView.setAdapter(adapter);
  ListViewUtil.setListViewHeightBasedOnChildren(mListView, adapter);</code>

还可能牵扯到更新Adapter后的数据变动,也要调用该方法.

<code class="hljs java has-numbering">ListViewUtil.setListViewHeightBasedOnChildren(mListView, adapter);
<span class="hljs-javadoc">mListView 就是你布局里那个Listview
adapter就是适配Listview的Adapter</span></code>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值