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

来源:互联网 发布:onavo mac版 编辑:程序博客网 时间:2024/04/30 19:53

     今天遇到问题如题:

             最后通过网络找到答案,,,主要是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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宜家水壶盖子有水怎么办 背滤鱼缸除油膜怎么办 书多了没地方放怎么办 学生在教室内丢手机怎么办 新车尾箱坏了怎么办 放书的箱子烂了怎么办 车钥匙锁后备箱里怎么办 布的收纳箱有味怎么办 车漆清漆层掉了怎么办 副驾驶储物箱卡子断了怎么办 玛莎拉蒂车门打不开怎么办 新买的水杯漏水怎么办 泰迪小狗掉毛怎么办 手机jlc调用接口状态异常怎么办 进门和厕所对着怎么办 p过的照片有竖条怎么办 当照片p出竖条纹怎么办 月子里落下脚心怕风怕凉怎么办 鞋胶把手粘住了怎么办 凉鞋魔术贴长了怎么办 新买的狗一直叫怎么办 刚买的幼犬老叫怎么办 狗狗什么都不吃怎么办 新买的吊扇风小怎么办 夜市卖果汁没电怎么办 榻榻米太长2米45怎么办 木质桌子黏黏的怎么办 白色塑料桌子染色了怎么办 3dmax模型变透明了怎么办 刚养的兔子不吃怎么办 熊猫兔不吃下喝怎么办 熊猫兔感冒了一直打喷嚏怎么办 兔子后腿骨断了怎么办 兔子的腿肿了怎么办 仓鼠喝了牛奶该怎么办 宠物兔不吃不喝怎么办 兔子把木屑吃了怎么办? 小车司机碰瓷大车司机怎么办 在淘宝买到假的护肤品怎么办 淘宝购物发现是假的怎么办 电脑键盘灯不亮不能打字怎么办