Skip to content

Commit

Permalink
增加了RichTextView,优化了文本处理链接和其他span的优先级(2017-08-28)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuoSmall committed Aug 28, 2017
1 parent 5ecab1d commit 04e6c0a
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 1 deletion.
36 changes: 35 additions & 1 deletion app/src/main/java/com/example/richtext/model/MVViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.graphics.Color;
import android.text.Spannable;
Expand All @@ -25,10 +26,24 @@ public class MVViewModel extends BaseObservable {

public final ObservableField<CharSequence> currentTextString = new ObservableField<>();

public final ObservableField<String> currentTextTitle= new ObservableField<>("未输入文本");
public final ObservableField<String> currentTextViewString = new ObservableField<>();

public final ObservableField<String> currentTextTitle = new ObservableField<>("未输入文本");

public final ObservableField<Integer> linkFlag = new ObservableField<>(0);

public final ObservableArrayList<TopicModel> topicListOb = new ObservableArrayList<>();

public final ObservableArrayList<UserModel> nameListOb = new ObservableArrayList<>();

public final ObservableField<Integer> atColor = new ObservableField<>(Color.YELLOW);

public final ObservableField<Integer> topicColor = new ObservableField<>(Color.RED);

public final ObservableField<Integer> linkColor = new ObservableField<>(Color.BLUE);

public final ObservableField<Boolean> needNumberShow = new ObservableField<>(true);

private List<TopicModel> topicModels = new ArrayList<>();

private List<UserModel> nameList = new ArrayList<>();
Expand All @@ -48,6 +63,8 @@ public MVViewModel(Context context, IMVVMView imvvmView) {
private void initData() {
nameList.clear();
topicModels.clear();
nameListOb.clear();
topicListOb.clear();

UserModel userModel = new UserModel();
userModel.setUser_name("22222");
Expand All @@ -63,6 +80,22 @@ private void initData() {
topicModel.setTopicName("话题话题");
topicModels.add(topicModel);


userModel = new UserModel();
userModel.setUser_name("22222");
userModel.setUser_id("2222");
nameListOb.add(userModel);
userModel = new UserModel();
userModel.setUser_name("kkk");
userModel.setUser_id("23333");
nameListOb.add(userModel);

topicModel = new TopicModel();
topicModel.setTopicId("333");
topicModel.setTopicName("话题话题");
topicListOb.add(topicModel);


}

private ITextViewShow iTextViewShow = new ITextViewShow() {
Expand Down Expand Up @@ -105,6 +138,7 @@ private void setCurrentText(String text) {
.setSpanTopicCallBack(imvvmView.getSpanTopicCallBack())
.buildSpan(iTextViewShow);
setLocalCurrentTextString(spannable);
currentTextViewString.set(text);
}

private void setLocalCurrentTextString(CharSequence charSequence) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_mvvm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,21 @@
android:onClick="@{() -> viewmodel.insertTextClick()}"
android:text="插入切换文本" />

<com.shuyu.textutillib.RichTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mvvm_rich_text_insert_text_btn"
android:minLines="8"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:richText="@{viewmodel.currentTextViewString}"
app:nameList="@{viewmodel.nameListOb}"
app:topicList="@{viewmodel.topicListOb}"
app:atColor="@{viewmodel.atColor}"
app:topicColor="@{viewmodel.topicColor}"
app:linkColor="@{viewmodel.linkColor}"
app:needNumberShow="@{viewmodel.needNumberShow}"
/>

</RelativeLayout>
</layout>
244 changes: 244 additions & 0 deletions textUtilsLib/src/main/java/com/shuyu/textutillib/RichTextView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package com.shuyu.textutillib;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.shuyu.textutillib.listener.SpanAtUserCallBack;
import com.shuyu.textutillib.listener.SpanTopicCallBack;
import com.shuyu.textutillib.listener.SpanUrlCallBack;
import com.shuyu.textutillib.model.TopicModel;
import com.shuyu.textutillib.model.UserModel;

import java.util.ArrayList;
import java.util.List;

/**
* Created by guoshuyu on 2017/8/28.
*/

public class RichTextView extends TextView {

private List<TopicModel> topicList = new ArrayList<>();

private List<UserModel> nameList = new ArrayList<>();

private int atColor = Color.BLUE;

private int topicColor = Color.BLUE;

private int linkColor = Color.BLUE;

private SpanUrlCallBack spanUrlCallBackListener;

private SpanAtUserCallBack spanAtUserCallBackListener;

private SpanTopicCallBack spanTopicCallBackListener;

private boolean needNumberShow = true;//是否需要数字处理

public RichTextView(Context context) {
super(context);
}

public RichTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public RichTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

private SpanUrlCallBack spanUrlCallBack = new SpanUrlCallBack() {
@Override
public void phone(View view, String phone) {
if (spanUrlCallBackListener != null) {
spanUrlCallBackListener.phone(view, phone);
}
}

@Override
public void url(View view, String url) {
if (spanUrlCallBackListener != null) {
spanUrlCallBackListener.url(view, url);
}
}
};

private SpanAtUserCallBack spanAtUserCallBack = new SpanAtUserCallBack() {
@Override
public void onClick(View view, UserModel userModel1) {
if (spanAtUserCallBackListener != null) {
spanAtUserCallBackListener.onClick(view, userModel1);
}
}
};

private SpanTopicCallBack spanTopicCallBack = new SpanTopicCallBack() {
@Override
public void onClick(View view, TopicModel topicModel) {
if (spanTopicCallBackListener != null) {
spanTopicCallBackListener.onClick(view, topicModel);
}
}
};

/**
* 显示处理文本
*
* @param content
*/
private void resolveRichShow(String content) {
RichTextBuilder richTextBuilder = new RichTextBuilder(getContext());
richTextBuilder.setContent(content)
.setAtColor(atColor)
.setLinkColor(linkColor)
.setTopicColor(topicColor)
.setListUser(nameList)
.setListTopic(topicList)
.setNeedNum(needNumberShow)
.setTextView(this)
.setSpanAtUserCallBack(spanAtUserCallBack)
.setSpanUrlCallBack(spanUrlCallBack)
.setSpanTopicCallBack(spanTopicCallBack)
.build();

}

/**
* 设置@某人文本
*
* @param text 文本
* @param nameList @人列表
*/
public void setRichTextUser(String text, List<UserModel> nameList) {
this.setRichText(text, nameList, topicList);
}

/**
* 设置话题文本
*
* @param text 文本
* @param topicList 话题列表
*/
public void setRichTextTopic(String text, List<TopicModel> topicList) {
this.setRichText(text, nameList, topicList);
}

/**
* 设置话题和@文本
*
* @param text 文本
* @param nameList @人列表
* @param topicList 话题列表
*/
public void setRichText(String text, List<UserModel> nameList, List<TopicModel> topicList) {
if (nameList != null) {
this.nameList = nameList;
}
if (topicList != null) {
this.topicList = topicList;
}
resolveRichShow(text);
}

/**
* 设置话题和@文本
*
* @param text 文本
*/
public void setRichText(String text) {
setRichText(text, nameList, topicList);
}

public boolean isNeedNumberShow() {
return needNumberShow;
}

public List<TopicModel> getTopicList() {
return topicList;
}

/**
* 设置话题列表
*/
public void setTopicList(List<TopicModel> topicList) {
this.topicList = topicList;
}

public List<UserModel> getNameList() {
return nameList;
}

/**
* 设置at列表
*/
public void setNameList(List<UserModel> nameList) {
this.nameList = nameList;
}

/**
* 是否需要处理数字
*
* @param needNumberShow 是否需要高亮数字和点击
*/
public void setNeedNumberShow(boolean needNumberShow) {
this.needNumberShow = needNumberShow;
}

/**
* url点击
*/
public void setSpanUrlCallBackListener(SpanUrlCallBack spanUrlCallBackListener) {
this.spanUrlCallBackListener = spanUrlCallBackListener;
}

/**
* at某人点击
*/
public void setSpanAtUserCallBackListener(SpanAtUserCallBack spanAtUserCallBackListener) {
this.spanAtUserCallBackListener = spanAtUserCallBackListener;
}

/**
* 话题点击
*/
public void setSpanTopicCallBackListener(SpanTopicCallBack spanTopicCallBackListener) {
this.spanTopicCallBackListener = spanTopicCallBackListener;
}

public int getAtColor() {
return atColor;
}

/**
* at某人颜色
*/
public void setAtColor(int atColor) {
this.atColor = atColor;
}

public int getTopicColor() {
return topicColor;
}
/**
* 话题颜色
*/
public void setTopicColor(int topicColor) {
this.topicColor = topicColor;
}

public int getLinkColor() {
return linkColor;
}

/**
* 链接颜色
*/
public void setLinkColor(int linkColor) {
this.linkColor = linkColor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ private static Spannable resolveUrlLogic(Context context, ITextViewShow textView

}
for (ClickAtUserSpan atUserSpan : atSpan) {
//剔除话题和at某人中的link span
LinkSpan[] removeUrls = style.getSpans(sp.getSpanStart(atUserSpan), sp.getSpanEnd(atUserSpan), LinkSpan.class);
if (removeUrls != null && removeUrls.length > 0) {
for (LinkSpan linkSpan : removeUrls) {
style.removeSpan(linkSpan);
}
}
style.setSpan(atUserSpan, sp.getSpanStart(atUserSpan), sp.getSpanEnd(atUserSpan), Spanned.SPAN_MARK_POINT);
}
SmileUtils.addSmiles(context, style);
Expand Down
10 changes: 10 additions & 0 deletions textUtilsLib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@
<attr name="richMarginTop" format="dimension" />
</declare-styleable>

<declare-styleable name="RichTextView">
<attr name="needNumberShow" format="boolean" />
<attr name="richText" format="string" />
<attr name="topicList" format="reference" />
<attr name="nameList" format="reference" />
<attr name="atColor" format="color" />
<attr name="topicColor" format="color" />
<attr name="linkColor" format="color" />
</declare-styleable>

</resources>

0 comments on commit 04e6c0a

Please sign in to comment.