Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RollCretin committed Aug 30, 2018
2 parents 5602e24 + 5d36107 commit 8c13860
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 22 deletions.
78 changes: 78 additions & 0 deletions .idea/markdown-navigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/markdown-navigator/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,87 @@
# ExpandableTextView
可展开和收回的TextView
实现类似微博内容,@用户,链接高亮,@用户和链接可点击跳转,可展开和收回的TextView。觉得好用别忘了star哦,你的star是对我最大的激励。

### 实现效果:

<img src="./extra/demo.png"/>

### 使用方式:

#### Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

#### Step 2. Add the dependency

```
dependencies {
implementation 'com.github.MZCretin:ExpandableTextView:v1.0'
}
```

### 代码说明

+ 以下属性都可以在xml中设置
```xml
<!--保留的行数-->
<attr name="ep_max_line" format="integer" />
<!--是否需要展开-->
<attr name="ep_need_expand" format="boolean" />
<!--是否需要收起 这个是建立在开启展开的基础上的-->
<attr name="ep_need_contract" format="boolean" />
<!--是否需要动画-->
<attr name="ep_need_animation" format="boolean" />
<!--收起的文案-->
<attr name="ep_contract_text" format="string" />
<!--展开的文案-->
<attr name="ep_expand_text" format="string" />
<!--展开的文字的颜色-->
<attr name="ep_expand_color" format="color" />
<!--收起的文字的颜色-->
<attr name="ep_contract_color" format="color" />
<!--在收回和展开前面添加的内容的字体颜色-->
<attr name="ep_end_color" format="color" />
<!--链接的文字的颜色-->
<attr name="ep_link_color" format="color" />
<!--链接的图标-->
<attr name="ep_link_res" format="reference" />
```

+ java代码
```java
ExpandableTextView expandableTextView = findViewById(R.id.ep_01);
//需要显示的内容
String yourText = " 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
//将内容设置给控件
expandableTextView.setContent(yourText);
//xml中的属性也可以通过代码设置 比如
expandableTextView.setmNeedExpend(true);
//还有很多。。。。
//添加点击监听
expandableTextView.setLinkClickListener(new ExpandableTextView.OnLinkClickListener() {
@Override
public void onLinkClickListener(ExpandableTextView.LinkType linkType, String content) {
//根据类型去判断
if (linkType.equals(ExpandableTextView.LinkType.LINK_TYPE)) {
Toast.makeText(MainActivity.this, "你点击了链接 内容是:" + content, Toast.LENGTH_SHORT).show();
} else if (linkType.equals(ExpandableTextView.LinkType.MENTION_TYPE)) {
Toast.makeText(MainActivity.this, "你点击了@用户 内容是:" + content, Toast.LENGTH_SHORT).show();
}
}
});
```

### 实现思路讲解

**简书:** [【需求解决系列之三】Android 自定义可展开收回的ExpandableTextView](https://www.jianshu.com/p/5519fbab6907)

**掘金:** [【需求解决系列之三】Android 自定义可展开收回的ExpandableTextView](https://juejin.im/post/5b876a4de51d4571c5137660)
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile project(path: ':expandabletextviewlibrary')
//这里使用本地的 如果想设置可以打开自己测试下
// implementation 'com.github.MZCretin:ExpandableTextView:v1.0'
implementation project(':expandabletextviewlibrary')
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cretin.www.expandabletextview;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.text.util.LinkifyCompat;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -10,6 +12,7 @@
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.util.Linkify;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -40,16 +43,13 @@ public class MainActivity extends AppCompatActivity {
"3,5;6,9;10,11;14,16;21,22",
};

private String github = "https://github.com/MZCretin/ExpandableTextView";
private String blogs = "https://www.jianshu.com/p/b7a8ddc639db";
private String contact = "[email protected]";
private TextView tvTips00;

private ExpandableTextView.OnLinkClickListener linkClickListener = (type, content) -> {
if (type.equals(ExpandableTextView.LinkType.LINK_TYPE)) {
Toast.makeText(MainActivity.this, "你点击了链接 " + content, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "你点击了链接 内容是:" + content, Toast.LENGTH_SHORT).show();
} else if (type.equals(ExpandableTextView.LinkType.MENTION_TYPE)) {
Toast.makeText(MainActivity.this, "你点击了@用户 " + content, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "你点击了@用户 内容是:" + content, Toast.LENGTH_SHORT).show();
}
};

Expand All @@ -76,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {

setTips();

String yourText = " hello...world 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
String yourText = " 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
views[0].setContent(yourText);
views[0].setLinkClickListener(linkClickListener);

Expand All @@ -96,6 +96,12 @@ protected void onCreate(Bundle savedInstanceState) {
views[5].setContent(yourText);
views[5].setEndExpendContent(" 1小时前");
views[5].setLinkClickListener(linkClickListener);

findViewById(R.id.ll_ad).setOnClickListener(v -> {
Uri uri = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=com.cretin");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
});
}

/**
Expand All @@ -108,7 +114,7 @@ private void setTips() {
tvTips00.setMovementMethod(LinkMovementMethod.getInstance());
tvTips00.setText(value);

//处理圣剩下的
//处理剩下的
for (int i = 0; i < indexs.length; i++) {
String index = indexs[i];
TextView view = tips[i];
Expand Down
58 changes: 48 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="100dp">
android:paddingBottom="5dp">

<TextView
android:id="@+id/tv_tips00"
android:textSize="14sp"
android:text="ExpandTextView包含如下功能:\n1、实现类似微博@好友的功能,对@内容进行高亮显示,可以点击\n2、实现链接的高亮显示,可以点击,并且对链接进行原文隐藏\n3、实现内容超过指定行数折叠效果,点击可以展开内容\n4、在原文内容末尾添加指定内容,比如时间串,详情见效果图\n\n联系我:\[email protected]\n\n博客地址:\nhttps://www.jianshu.com/p/b7a8ddc639db\n\nGithub地址(您的star是对我最大的鼓励):\nhttps://github.com/MZCretin/ExpandableTextView\n\n"
android:textColor="#333333"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="4dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp" />
android:paddingTop="10dp"
android:text="ExpandTextView包含如下功能:\n1、实现类似微博@好友的功能,对@内容进行高亮显示,可以点击\n2、实现链接的高亮显示,可以点击,并且对链接进行原文隐藏\n3、实现内容超过指定行数折叠效果,点击可以展开内容\n4、在原文内容末尾添加指定内容,比如时间串,详情见效果图\n\n联系我:\[email protected]\n\n博客地址:\nhttps://www.jianshu.com/p/5519fbab6907\n\nGithub地址(您的star是对我最大的鼓励):\nhttps://github.com/MZCretin/ExpandableTextView\n\n"
android:textColor="#333333"
android:textSize="14sp" />

<TextView
android:id="@+id/tv_tips01"
Expand All @@ -33,8 +33,8 @@
android:lineSpacingExtra="4dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text="1、正常带链接和@用户,没有展开和收回功能"
android:textColor="#666666"
android:textSize="12sp" />
Expand All @@ -53,10 +53,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e6e6e6"
android:paddingRight="15dp"
android:lineSpacingExtra="4dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text="2、正常带链接和@用户,有展开和收回功能,有切换动画"
android:textColor="#666666"
Expand All @@ -78,10 +78,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e6e6e6"
android:paddingRight="15dp"
android:lineSpacingExtra="4dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text="3、正常带链接和@用户,有展开和收回功能,没有切换动画"
android:textColor="#666666"
Expand All @@ -102,11 +102,11 @@
android:id="@+id/tv_tips04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="15dp"
android:background="#e6e6e6"
android:lineSpacingExtra="4dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text="4、正常带链接和@用户,有展开,没有收回功能"
android:textColor="#666666"
Expand All @@ -129,9 +129,9 @@
android:layout_height="wrap_content"
android:background="#e6e6e6"
android:lineSpacingExtra="4dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text="5、正常带链接和@用户,有展开,有收回功能,带附加内容(比如时间)"
android:textColor="#666666"
Expand Down Expand Up @@ -173,5 +173,43 @@
app:ep_need_contract="false"
app:ep_need_expand="true" />

<LinearLayout
android:id="@+id/ll_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="这是一条广告"
android:textColor="#333333"
android:textSize="15sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="《段子乐》是一款集视频,趣图,段子于一身的社区APP"
android:textColor="#666666"
android:textSize="12sp" />
</LinearLayout>


</LinearLayout>

</LinearLayout>
</ScrollView>
Binary file added app/src/main/res/mipmap-xxhdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}

Expand Down
Loading

0 comments on commit 8c13860

Please sign in to comment.