ScrollView嵌套ListView,gridView只显示一行解决方案 (以及计算显示高度错误问题)

     今天遇到问题如题:

             最后通过网络找到答案,,,主要是ScrollView问题,,,所以贴出来大家一起共同学习.


           解决方法1:(代码去计算高度,然后在显示)

                     private void setListViewHeightBasedOnChildren(ListView listView) {
ItemListAdapter listAdapter = (ItemListAdapter) 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));
listView.setLayoutParams(params);
}


    解决方法2: (复写listView )

              

publicvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {
2         intmExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
3         super.onMeasure(widthMeasureSpec, mExpandSpec);
4  
5    }
    两个方法都可以,第二个相对而言比较方便点。

    问题出现了: 当listView中每项中有TextView等多行显示的时候,上面的计算方法都是默认单行计算的,这样的话计算高度就不正确了。。。

    解决办法:
        这个问题不在计算函数上,因为我无法获取 TextView  显示高度的一些信息,所以解决问题必须从TextView本身来考虑。。,对了,我这里就是想重写TextView控件,然后在onMeasure把显示高度计算好,这样就能成功的计算出listView的高度了。。
       重写类如下:

public class NewTextView extends TextView {


private Context context;


public NewTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}


public NewTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
}


@SuppressLint("FloatMath")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Layout layout = getLayout();
if (layout != null) {
int height = (int) FloatMath.ceil(getMaxLineHeight(this.getText()
.toString()))
+ getCompoundPaddingTop()
+ getCompoundPaddingBottom();
int width = getMeasuredWidth();
setMeasuredDimension(width, height);
}
}


@SuppressLint("NewApi")
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))) +1 ; //+1是为了让界面更好看
height = (this.getPaint().getFontMetrics().descent - this.getPaint()
.getFontMetrics().ascent) * line;
return height;
}
}


但问题又出现了,你有可能发现程序代码报错了,关键是这一句代码:

float paddingLeft = ((LinearLayout) this.getParent()).getPaddingLeft();
float paddingReft = ((LinearLayout) this.getParent()).getPaddingRight();

可以看出,在使用控件的时候,父布局一定要是 LinearLayout,所以在使用的地方把该控件用LinearLayout封装包裹一下,问题就完美解决了。。。

   如想更多交流,请加android技术群:451370308 (诚心交流,非诚勿扰)








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值