增加ExpendTextView(textView增加...全文按钮)
This commit is contained in:
107
app/src/main/java/com/gh/common/view/ExpendTextView.java
Normal file
107
app/src/main/java/com/gh/common/view/ExpendTextView.java
Normal file
@ -0,0 +1,107 @@
|
||||
package com.gh.common.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.Layout;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextPaint;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import com.gh.gamecenter.R;
|
||||
|
||||
public class ExpendTextView extends android.support.v7.widget.AppCompatTextView {
|
||||
|
||||
private CharSequence mSnapshotText;
|
||||
|
||||
private String mExpendText = "...全文";
|
||||
|
||||
private int mMaxLines = 3; // 由于sdk版本限制(getMaxLines) 这里设置默认值
|
||||
|
||||
private boolean mInitLayout = false;
|
||||
private boolean mOpenLayout = false;
|
||||
|
||||
public ExpendTextView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public ExpendTextView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public ExpendTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||
mMaxLines = getMaxLines();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
if (mInitLayout && !mOpenLayout && getLineCount() > mMaxLines) {
|
||||
mSnapshotText = getText();
|
||||
mInitLayout = false;
|
||||
showExpendButton();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(CharSequence text, BufferType type) {
|
||||
mInitLayout = true;
|
||||
super.setText(text, type);
|
||||
}
|
||||
|
||||
private void showExpendButton() {
|
||||
Layout layout = getLayout();
|
||||
int start = layout.getLineStart(0);
|
||||
int lastLineEnd = layout.getLineEnd(mMaxLines - 1);
|
||||
int lastLineStart = layout.getLineStart(mMaxLines - 1);
|
||||
float lastLineRight = layout.getLineRight(mMaxLines - 1);
|
||||
|
||||
int viewWidth = getWidth() - getPaddingRight() - getPaddingLeft();
|
||||
|
||||
TextPaint paint = getPaint();
|
||||
float expendTextWidth = paint.measureText(mExpendText);
|
||||
CharSequence content = mSnapshotText.subSequence(start, lastLineEnd);
|
||||
if (viewWidth - lastLineRight > expendTextWidth) {
|
||||
content = content.toString().trim() + mExpendText;
|
||||
} else {
|
||||
CharSequence lastText = mSnapshotText.subSequence(lastLineStart, lastLineEnd);
|
||||
for (int i = 0; i < lastText.length(); i++) {
|
||||
CharSequence sequence = lastText.subSequence(0, i);
|
||||
float w = paint.measureText(sequence.toString());
|
||||
if (viewWidth - w > expendTextWidth) {
|
||||
content = mSnapshotText.subSequence(start, lastLineStart + i) + mExpendText;
|
||||
}
|
||||
}
|
||||
}
|
||||
SpannableStringBuilder msp = new SpannableStringBuilder(content);
|
||||
int length = msp.length();
|
||||
msp.replace(length - mExpendText.length(), length, mExpendText);
|
||||
msp.setSpan(new ClickableSpan() {
|
||||
@Override
|
||||
public void updateDrawState(TextPaint ds) {
|
||||
super.updateDrawState(ds);
|
||||
ds.setColor(ContextCompat.getColor(getContext(), R.color.theme));
|
||||
ds.setUnderlineText(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View widget) {
|
||||
mOpenLayout = true;
|
||||
setMaxLines(Integer.MAX_VALUE);
|
||||
setText(mSnapshotText);
|
||||
|
||||
}
|
||||
}, length - mExpendText.length(), length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
|
||||
setText(msp);
|
||||
setMovementMethod(LinkMovementMethod.getInstance());
|
||||
}
|
||||
|
||||
}
|
||||
@ -13,10 +13,10 @@ import butterknife.BindView;
|
||||
*/
|
||||
public class GameImageViewHolder extends BaseRecyclerViewHolder {
|
||||
|
||||
@BindView(R.id.home_game_iv_image)
|
||||
@BindView(R.id.game_image_icon)
|
||||
public SimpleDraweeView image;
|
||||
|
||||
@BindView(R.id.home_game_line)
|
||||
@BindView(R.id.game_image_line)
|
||||
public View line;
|
||||
|
||||
public GameImageViewHolder(View itemView) {
|
||||
|
||||
@ -180,9 +180,9 @@ public class QuestionsDetailAdapter extends ListAdapter<AnswerEntity> {
|
||||
addTag(holder.mTagRl, mQuestionsDetailEntity.getCommunityName(), true);
|
||||
String description = mQuestionsDetailEntity.getDescription();
|
||||
if (TextUtils.isEmpty(description)) {
|
||||
holder.mDesRl.setVisibility(View.GONE);
|
||||
holder.mDes.setVisibility(View.GONE);
|
||||
} else {
|
||||
holder.mDesRl.setVisibility(View.VISIBLE);
|
||||
holder.mDes.setVisibility(View.VISIBLE);
|
||||
holder.mDes.setText(description);
|
||||
}
|
||||
if (!TextUtils.isEmpty(mQuestionsDetailEntity.getMe().getMyAnswerId())) {
|
||||
@ -210,17 +210,6 @@ public class QuestionsDetailAdapter extends ListAdapter<AnswerEntity> {
|
||||
holder.mTitle.setText(mQuestionsDetailEntity.getTitle());
|
||||
holder.mAnswercount.setText(mContext.getString(R.string.ask_answer_count, mQuestionsDetailEntity.getAnswersCount()));
|
||||
holder.concernCount.setText(mQuestionsDetailEntity.getFollowCount() + "人关注");
|
||||
|
||||
holder.mDesAll.setOnClickListener((v) -> {
|
||||
mIsExpand = true;
|
||||
holder.mDesAll.setVisibility(View.GONE);
|
||||
holder.mDes.setMaxLines(Integer.MAX_VALUE);// 展开
|
||||
holder.mDes.setText(mQuestionsDetailEntity.getDescription());
|
||||
});
|
||||
holder.mDes.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
|
||||
int lineCount = holder.mDes.getLineCount();
|
||||
holder.mDesAll.setVisibility(lineCount > 3 && !mIsExpand ? View.VISIBLE : View.GONE);
|
||||
});
|
||||
}
|
||||
|
||||
private void addTag(FlexboxLayout mTagRl, String tag, boolean isLastTag) {
|
||||
|
||||
@ -24,8 +24,6 @@ public class QuestionsDetailItemViewHolder extends BaseRecyclerViewHolder {
|
||||
public TextView mTitle;
|
||||
@BindView(R.id.questionsdetail_item_des)
|
||||
public TextView mDes;
|
||||
@BindView(R.id.questionsdetail_item_des_rl)
|
||||
public View mDesRl;
|
||||
@BindView(R.id.questionsdetail_item_pic1)
|
||||
public SimpleDraweeView mPic1;
|
||||
@BindView(R.id.questionsdetail_item_pic2)
|
||||
@ -36,8 +34,6 @@ public class QuestionsDetailItemViewHolder extends BaseRecyclerViewHolder {
|
||||
public TextView mAnswercount;
|
||||
@BindView(R.id.questionsdetail_item_picll)
|
||||
public LinearLayout mImgLl;
|
||||
@BindView(R.id.questionsdetail_item_des_all)
|
||||
public View mDesAll;
|
||||
@BindView(R.id.questionsdetail_answer_tv)
|
||||
TextView answerTv;
|
||||
@BindView(R.id.questionsdetail_answer)
|
||||
|
||||
Reference in New Issue
Block a user