android ExpandableListView点击二级菜单中textview怎么实现获取textview中值

android ExpandableListView点击二级菜单中textview怎么实现获取textview中值,第1张

String childName = adaptergetChild(groupPosition, childPosition);

用这个就行了,我已经是过了,正确。

最近项目需求,需要获取Textview的行数,通过行数与TextView的maxLines进行比较来确定是否显示TextView下方的展开按钮是否显示,废话少说直接上代码,mTextViewgetLineCount() ,似乎很美好,安卓有提供这个方法,但是总是返回0,这是为啥呢?官方注释如下:

/

Return the number of lines of text, or 0 if the internal Layout has not

been built

/

也就是说只有内部的Layout创建之后才会返回正确的行数,那怎么保证layout已经构创建了呢?

最后我是这么解决的

mTextViewgetViewTreeObserver()addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

// TODO Auto-generated method stub

Loge(TAG, "行数"+mTextViewgetLineCount());

mTextViewgetViewTreeObserver()removeGlobalOnLayoutListener(this);

if(mTextViewgetLineCount()>0){

mTextViewgetViewTreeObserver()removeOnGlobalLayoutListener(this);

}

}

});

当TeXtView在绘制的时候就会回调这个方法,注意当我们得到了想要的值之后注意移除GlobalOnLayoutListener避免多余的执行,而且我的项目需求是要后面通过改变textview的高度实现平滑展开的动画。附上关键代码

/

折叠效果

/

tempHight = mTextViewgetLineHeight() mTextViewgetLineCount() - startHight; //计算要展开高度

tempHight = mTextViewgetLineHeight() maxLine - startHight;//为负值,收缩的高度

Animation animation = new Animation() {

//interpolatedTime 为当前动画帧对应的相对时间,值总在0-1之间

protected void applyTransformation(float interpolatedTime, Transformation t) {

mTextViewsetHeight((int) (startHight + tempHight interpolatedTime));//原始长度+高度差(从0到1的渐变)即表现为动画效果

}

};

animationsetDuration(1000);

mTextViewstartAnimation(animation);

ID本身是个int类型的,就算设置也只能给一个随机不重复的int数字,但是如果想要通过字符串去实现,用个HashMap把键值对存下来

final static HashMap<String, Integer> listView = new HashMap<String, Integer>();

赋值ID linearLayoutsetId(唯一id);

键值对 listViewput(字符串id, 前边定义的唯一int ID);

获取:

public static Integer getViewIdById(String id){

if (listViewcontainsKey(id))

return listViewget(id);

else

return -1;

}

以上就是关于android ExpandableListView点击二级菜单中textview怎么实现获取textview中值全部的内容,包括:android ExpandableListView点击二级菜单中textview怎么实现获取textview中值、如何在 Android 的 textview 里获取行数、android动态创建的Textview怎么获取或者给他设置一个ID等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9297718.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存