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,显示多行的问题就能得到解决,上一张解决的图片:


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/smileiam/article/details/50512085
个人分类: Android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭