<declare-styleablename="CollapsibleTextView"> <attrname="suffixColor"format="color"/> <attrname="collapsedLines"format="integer"/> <attrname="collapsedText"format="string"/> <attrname="expandedText"format="string"/> <attrname="suffixTrigger"format="boolean"/> </declare-styleable>
publicCollapsibleTextView(Context context, AttributeSet attrs,intdefStyleAttr){
super(context, attrs, defStyleAttr);
TypedArray attributes = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.CollapsibleTextView, defStyleAttr, 0);
mSuffixColor = attributes.getColor(R.styleable.CollapsibleTextView_suffixColor, 0xff0000ff);
mCollapsedLines = attributes.getInt(R.styleable.CollapsibleTextView_collapsedLines, 1);
mCollapsedText = attributes.getString(R.styleable.CollapsibleTextView_collapsedText);
if (TextUtils.isEmpty(mCollapsedText)) mCollapsedText = " Show All";
mExpandedText = attributes.getString(R.styleable.CollapsibleTextView_expandedText);
if (TextUtils.isEmpty(mExpandedText)) mExpandedText = " Hide";
mSuffixTrigger = attributes.getBoolean(R.styleable.CollapsibleTextView_suffixTrigger, false);
this.mText = getText() == null ? null : getText().toString();
setMovementMethod(LinkMovementMethod.getInstance());
super.setOnClickListener(mClickListener);
}
private OnClickListener mClickListener = new OnClickListener() {
@Override
publicvoidonClick(View v){
if (!mSuffixTrigger) {
mExpanded = !mExpanded;
applyState(mExpanded);
}
if (mCustomClickListener != null) {
mCustomClickListener.onClick(v);
}
}
};
@Override
publicvoidsetOnClickListener(OnClickListener l){
mCustomClickListener = l;
}
str.setSpan(mClickSpanListener,
note.length(),
note.length() + suffix.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
// ClickableSpan
private ClickableSpan mClickSpanListener
= new ClickableSpan() {
@Override
publicvoidonClick(View widget){
if (mSuffixTrigger) {
mExpanded = !mExpanded;
applyState(mExpanded);
}
}
@Override
publicvoidupdateDrawState(TextPaint ds){
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
privatevoidapplyState(booleanexpanded){
if (TextUtils.isEmpty(mText)) return;
String note = mText, suffix;
if (expanded) {
suffix = mExpandedText;
} else {
if (mCollapsedLines - 1 < 0) {
throw new RuntimeException("CollapsedLines must equal or greater than 1");
}
int lineEnd = getLayout().getLineEnd(mCollapsedLines - 1);
suffix = mCollapsedText;
int newEnd = lineEnd - suffix.length() - 1;
int end = newEnd > 0 ? newEnd : lineEnd;
TextPaint paint = getPaint();
int maxWidth = mCollapsedLines * (getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
while (paint.measureText(note.substring(0, end) + suffix) > maxWidth)
end--;
note = note.substring(0, end);
}
final SpannableString str = new SpannableString(note + suffix);
if (mSuffixTrigger) {
str.setSpan(mClickSpanListener,
note.length(),
note.length() + suffix.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
str.setSpan(new ForegroundColorSpan(mSuffixColor),
note.length(),
note.length() + suffix.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
post(new Runnable() {
@Override
publicvoidrun(){
setText(str);
}
});
}
@Override
protectedvoidonLayout(booleanchanged,intleft,inttop,intright,intbottom){
super.onLayout(changed, left, top, right, bottom);
if (mShouldInitLayout && getLineCount() > mCollapsedLines) {
mShouldInitLayout = false;
applyState(mExpanded);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有