问题描述
我想在文本视图中显示描述,如果描述超过三行,我想显示一个查看更多"按钮,以展开具有完整描述的文本视图(或具有完整描述的对话框)
我已经介绍了一些链接
1)
这就是我实现所需输出的方式,
我要扩展的MytextView是:tvDescription我有一个名称为btnSeeMore
的查看更多"按钮要检查textview是否有4行以上,我有一个侦听器,如下所示,
tvDescription.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){@Overridepublic void onGlobalLayout(){if(expandable){可扩展=否;如果(tvDescription.getLineCount()> 4){btnSeeMore.setVisibility(View.VISIBLE);ObjectAnimator动画= ObjectAnimator.ofInt(tvDescription,"maxLines",4);animation.setDuration(0).start();}}}});
我保留了一个布尔值,以检查textview是否已扩展,以便在折叠时不会出现任何堆积.
如果textview的行数超过四行,则布尔标志为true,并且按钮可见,因此这是用于扩展和折叠动画的代码.
btnSeeMore.setOnClickListener(new View.OnClickListener(){public void onClick(View v){如果(!expand){expand = true;ObjectAnimator动画= ObjectAnimator.ofInt(tvDescription,"maxLines",40);animation.setDuration(100).start();btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_collapse));;} 别的 {expand = false;ObjectAnimator动画= ObjectAnimator.ofInt(tvDescription,"maxLines",4);animation.setDuration(100).start();btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_expand));}}});
以下评论以获取更多信息
I want to display description in a text view, if the description is more than 3 lines i want to display a View more button that expands the text view with complete description (or a dialog with complete description)
I have referred some links
1) How can I implement a collapsible view like the one from Google Play?
The above solution has an arrow like image view at end of third line of text view but i want a button displaying below the text view if description crosses 3 lines.
2) Add "View More" at the end of textview after 3 lines
The above solution has "View More" unaligned( "View" at 3rd line and "More" at 4th line)
I am looking for something like this.
This is how i have achieved the desired output,
MytextView which i want to expand is: tvDescriptionI have a see more button with name: btnSeeMore
To check if the textview has more than 4 lines i had a listener for it as follows,
tvDescription.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(expandable) {
expandable = false;
if (tvDescription.getLineCount() > 4) {
btnSeeMore.setVisibility(View.VISIBLE);
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
animation.setDuration(0).start();
}
}
}
});
I have kept a boolean value to check if textview is already expanded so that there will not be any hickups while collapsing it.
if textview has more than four lines, boolean flag will be true and the button will be visible so this is the code for expanding and collapsing animation.
btnSeeMore.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!expand) {
expand = true;
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 40);
animation.setDuration(100).start();
btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_collapse));
} else {
expand = false;
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
animation.setDuration(100).start();
btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_expand));
}
}
});
Comment below for further information
这篇关于具有“查看更多"功能的Android展开式文本视图;3行后在中间显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!