ListView(ScrollView)嵌套ListView、GridView显示不全,内容大于一行计算的高度不正确的解决办法

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">相信很多人都遇到过ListView或是ScrollView嵌套ListView,ListView(ScrollView)嵌套GridView,内容只显示一行,这个问题网上已有解决办法。</span>

1、对于ListView或是ScrollView嵌套ListView,一般是

利用Utility 类的静态方法setListViewHeightBasedOnChildren()即可实现:

在listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就能正常显示了。

public class LvHeightUtil {
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		  ListAdapter listAdapter = listView.getAdapter();
		  if (listAdapter == null) {
		   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)) +10;
		  listView.setLayoutParams(params);
		 }

}

<span style="font-family:SimSun;"></span>

2、对于ListView或是ScrollView嵌套GridView,一般是:


<pre name="code" class="java">public class NoScrollGridView extends GridView {

	public NoScrollGridView(Context context) {
		super(context);
	}

	public NoScrollGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public NoScrollGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}

}
 

3、对于嵌套的ListView内容如果是TextView内容大于一行显示不全的,则ListView的高度就会计算错误,它只计算了一行的高度,对于TextView可重写它的onMeasure方法,计算高度后,再去重新set

public class ShareAppendixTextView extends TextView {
    private Context context;

    public ShareAppendixTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }



    public ShareAppendixTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public ShareAppendixTextView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Layout layout = getLayout();
        if (layout != null) {
            int height = (int)Math.ceil(getMaxLineHeight(this.getText().toString()))
                    + getCompoundPaddingTop() + getCompoundPaddingBottom();
            int width = getMeasuredWidth();
            setMeasuredDimension(width, height);
        }
    }

    private float getMaxLineHeight(String str) {
        float height = 0.0f;
        float screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
        float paddingLeft = ((LinearLayout)this.getParent()).getPaddingLeft();
        float paddingReft = ((LinearLayout)this.getParent()).getPaddingRight();
//这里具体this.getPaint()要注意使用,要看你的TextView在什么位置,这个是拿TextView父控件的Padding的,为了更准确的算出换行
        int line = (int) Math.ceil( (this.getPaint().measureText(str)/(screenW-paddingLeft-paddingReft)));
        height = (this.getPaint().getFontMetrics().descent-this.getPaint().getFontMetrics().ascent)*line;
        return height;
    }
}

通过这样,ListView嵌套ListView,显示多行的问题就能得到解决,上一张解决的图片:


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值