public class ExpandTextView extends LinearLayout {
public static final int DEFAULT_MAX_LINES = 3;
private EmojiTextView contentText;
private TextView textPlus;
private static final String TAG = "ExpandTextView";
private int showLines;
private ExpandStatusListener expandStatusListener;
private boolean isExpand;
public ExpandTextView(Context context) {
super(context);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initView();
}
private void initView() {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.layout_magic_text, this);
contentText = (EmojiTextView) findViewById(R.id.contentText);
if (showLines > 0) {
contentText.setMaxLines(showLines);
}
textPlus = (TextView) findViewById(R.id.textPlus);
textPlus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String textStr = textPlus.getText().toString().trim();
if ("全文".equals(textStr)) {
contentText.setMaxLines(Integer.MAX_VALUE);
textPlus.setText("收起");
setExpand(true);
} else {
contentText.setMaxLines(showLines);
textPlus.setText("全文");
setExpand(false);
}
//通知外部狀態(tài)已變更
if (expandStatusListener != null) {
expandStatusListener.statusChange(isExpand());
}
}
});
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
try {
showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
} finally {
typedArray.recycle();
}
}
public void setText(final CharSequence content) {
contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// 避免重復(fù)監(jiān)聽
contentText.getViewTreeObserver().removeOnPreDrawListener(this);
int linCount = contentText.getLineCount();
if (linCount > showLines) {
if (isExpand) {
contentText.setMaxLines(Integer.MAX_VALUE);
textPlus.setText("收起");
} else {
contentText.setMaxLines(showLines);
textPlus.setText("全文");
}
textPlus.setVisibility(View.VISIBLE);
} else {
textPlus.setVisibility(View.GONE);
}
//Log.d("onPreDraw", "onPreDraw...");
//Log.d("onPreDraw", linCount + "");
return true;
}
});
contentText.setText(content);
contentText.setMovementMethod(new CircleMovementMethod(getResources().getColor(R.color.mine_head_color)));
}
public void setExpand(boolean isExpand) {
this.isExpand = isExpand;
}
public boolean isExpand() {
return this.isExpand;
}
public void setExpandStatusListener(ExpandStatusListener listener) {
this.expandStatusListener = listener;
}
public static interface ExpandStatusListener {
void statusChange(boolean isExpand);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/contentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="phone|web"
android:gravity="center_vertical"
android:padding="2dp"
android:text=""
android:textColor="@color/text_color"
android:textSize="16dp"
/>
<TextView
android:id="@+id/textPlus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text=""
android:textColor="@color/nick"
android:textSize="14sp"
/>
</LinearLayout>
attrs
<declare-styleable name="ExpandTextView">
<attr name="showLines" format="integer"/>
</declare-styleable>