Skip to content

Commit

Permalink
修复一些bug 新增一些特性
Browse files Browse the repository at this point in the history
  • Loading branch information
MZCretin committed Sep 20, 2018
1 parent 5c7b78c commit 2ad6be3
Show file tree
Hide file tree
Showing 24 changed files with 676 additions and 130 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
实现类似微博内容,@用户,链接高亮,@用户和链接可点击跳转,可展开和收回的TextView。觉得好用别忘了star哦,你的star是对我最大的激励。

### 更新日志
+ 2018-09-20 16:31:13 更新
+ 一、提供了在RecyclerView中使用的时候,对之前状态的保存的功能支持;
+ 二、新增对@用户和链接的处理,用户可以设置不对这些内容进行识别,仅仅使用展开和收回功能;
+ 三、优化的demo的效果,请大家重新下载apk进行体验。
+ 四、如果你没有设置对链接的监听,会默认调用系统浏览器打开链接
+ 五、最新版请使用v1.3


+ 2018-09-03 17:39:56 修复一些bug,链接sheSpan位置错误,未生成release,等待下次修复其他bug一起打tag依赖包,使用请本地依赖使用

+ 2018-08-31 17:31:56 优化设置padding对宽度造成的影响,依赖请使用tag版本v1.2
Expand All @@ -12,6 +20,10 @@

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

#### 下面是RecyclerView中的样式,可以保留之前展开和收回的状态

<img src="./extra/demo_gif.gif"/>

### 使用方式:

#### Step 1. Add the JitPack repository to your build file
Expand All @@ -31,14 +43,19 @@

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

### demo下载

[Demo下载](https://raw.githubusercontent.com/MZCretin/ExpandableTextView/master/extra/demo.apk)

扫描二维码下载:
<img src="./extra/erweima.png"/>



### 代码说明

+ 以下属性都可以在xml中设置
Expand All @@ -65,10 +82,48 @@
<attr name="ep_link_color" format="color" />
<!--链接的图标-->
<attr name="ep_link_res" format="reference" />

//布局文件中使用 可选 也可以在代码中设置
<com.ctetin.expandabletextviewlibrary.ExpandableTextView
android:id="@+id/ep_01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:lineSpacingExtra="4dp"
android:textSize="14sp"
<!--开始展开的行数 -->
app:ep_max_line="4"
<!--是否需要对链接进行识别-->
app:ep_need_link="true"
<!--是否需要对@用户进行识别-->
app:ep_need_mention="true"
<!--是否需要收回功能-->
app:ep_need_contract="true"
<!--是否需要展开和收回的动画-->
app:ep_need_animation="true"
<!--展开文字的颜色-->
app:ep_expand_color="@color/colorAccent"
<!--收回的文字描述-->
app:ep_contract_text="收回"
<!--在展开前可添加tips tips的文字颜色-->
app:ep_end_color="@color/colorAccent"
<!--展开的文字描述-->
app:ep_expand_text="展开"
<!--被识别出来的链接的颜色-->
app:ep_link_color="@color/colorAccent"
<!--被识别出来的链接的前面的图标资源-->
app:ep_link_res="@color/colorAccent"
<!--展开的文字的颜色-->
app:ep_contract_color="@color/colorAccent"
<!--是否需要展开功能-->
app:ep_need_expand="false" />
```

+ java代码
```java
/**
* 正常的使用
*/
ExpandableTextView expandableTextView = findViewById(R.id.ep_01);
//需要显示的内容
String yourText = " 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
Expand All @@ -91,6 +146,16 @@
});
```

### 新特性额外说明
如果你希望在RecyclerView(或者ListView)中使用,请认真阅读demo中在RecyclerView中的使用,细节都在注释中。

如果你需要在列表中保留之前的展开或收回状态,特殊说明的有以下几点:
+ 一、实现 ExpandableStatusFix
* 二、在你的model中定义一个 private StatusType status;
* 三、实现对应的方法,将你刚刚定义的status返回,
* 四、并在给ExpandableTextView设置内容之前,调用bind方法


### 实现思路讲解

**简书:** [【需求解决系列之三】Android 自定义可展开收回的ExpandableTextView](https://www.jianshu.com/p/5519fbab6907)
Expand Down
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//这里使用本地的 如果想设置ke
// implementation 'com.github.MZCretin:ExpandableTextView:v1.0'
// implementation 'com.github.MZCretin:ExpandableTextView:v1.0'
implementation project(':expandabletextviewlibrary')
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.cretin.www.expandabletextview.MainActivity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowInRecyclerViewActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class MainActivity extends AppCompatActivity {
private TextView[] tips;
private String[] indexs = new String[]{
"3,5;6,9;10,12",
"3,5;6,11;12,13;21,22",
"2,6;7,12;13,14;22,23",
"3,5;6,9;10,11;19,20",
"3,5;6,9;10,11;19,21",
"3,5;6,9;10,11;14,16",
Expand All @@ -58,26 +60,29 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

views = new ExpandableTextView[6];
tips = new TextView[6];
views = new ExpandableTextView[8];
tips = new TextView[8];
views[0] = findViewById(R.id.ep_01);
views[1] = findViewById(R.id.ep_02);
views[2] = findViewById(R.id.ep_03);
views[3] = findViewById(R.id.ep_04);
views[4] = findViewById(R.id.ep_05);
views[5] = findViewById(R.id.ep_06);
views[6] = findViewById(R.id.ep_07);
views[7] = findViewById(R.id.ep_08);
tips[0] = findViewById(R.id.tv_tips01);
tips[1] = findViewById(R.id.tv_tips02);
tips[2] = findViewById(R.id.tv_tips03);
tips[3] = findViewById(R.id.tv_tips04);
tips[4] = findViewById(R.id.tv_tips05);
tips[5] = findViewById(R.id.tv_tips06);
tips[6] = findViewById(R.id.tv_tips07);
tips[7] = findViewById(R.id.tv_tips08);
tvTips00 = findViewById(R.id.tv_tips00);

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

views[0].setContent(yourText);
views[0].setLinkClickListener(linkClickListener);

Expand All @@ -91,18 +96,30 @@ protected void onCreate(Bundle savedInstanceState) {
views[3].setLinkClickListener(linkClickListener);

views[4].setContent(yourText);
views[4].setEndExpendContent(" 1小时前");
views[4].setLinkClickListener(linkClickListener);

views[5].setContent(yourText);
views[5].setEndExpendContent(" 1小时前");
views[5].setLinkClickListener(linkClickListener);

views[6].setContent(yourText);
views[6].setEndExpendContent(" 1小时前");
views[6].setLinkClickListener(linkClickListener);

views[7].setContent(yourText);
views[7].setEndExpendContent(" 1小时前");
views[7].setLinkClickListener(linkClickListener);

//在RecyclerView中查看效果
findViewById(R.id.ll_recyclerview).setOnClickListener(v -> {
startActivity(new Intent(this, ShowInRecyclerViewActivity.class));
});

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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.cretin.www.expandabletextview;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cretin.www.expandabletextview.model.ViewModel;
import com.cretin.www.expandabletextview.model.ViewModelWithFlag;
import com.ctetin.expandabletextviewlibrary.ExpandableTextView;

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

public class ShowInRecyclerViewActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyRecyclerViewAdapter adapter;
private List list;

private FloatingActionButton floatingActionButton;

//是需要保存展开或收回状态
private boolean flag;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_in_recycler_view);

recyclerView = findViewById(R.id.recyclerview);
floatingActionButton = findViewById(R.id.float_btn);

list = new ArrayList<>();

for (int i = 0; i < 50; i++) {
list.add(new ViewModel(yourText));
}

adapter = new MyRecyclerViewAdapter(this, list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(adapter);

floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeStateAndSetData();
}
});
}

/**
* 切换状态
*/
private void changeStateAndSetData() {
flag = !flag;
list.clear();
if (flag) {
Toast.makeText(this, "保留之前的展开或收回状态", Toast.LENGTH_SHORT).show();
floatingActionButton.setImageResource(R.mipmap.green);
for (int i = 0; i < 50; i++) {
list.add(new ViewModelWithFlag(yourText));
}
} else {
Toast.makeText(this, "不保留之前的展开或收回状态", Toast.LENGTH_SHORT).show();
floatingActionButton.setImageResource(R.mipmap.gray);
for (int i = 0; i < 50; i++) {
list.add(new ViewModel(yourText));
}
}
adapter.notifyDataSetChanged();
}


public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.RecyclerHolder> {
private Context mContext;
private List dataList;

public MyRecyclerViewAdapter(Context context, List list) {
this.mContext = context;
this.dataList = list;
}

@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_text, parent, false);
return new RecyclerHolder(view);
}

@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
if (flag) {
//注意:保留状态需要在设置内容之前调用bind方法
holder.textView.bind((ViewModelWithFlag) list.get(position));
holder.textView.setContent(((ViewModelWithFlag) list.get(position)).getTitle());
} else {
holder.textView.setContent(((ViewModel) list.get(position)).getTitle());
}
holder.textView.setLinkClickListener((type, content1) -> {
switch (type) {
case LINK_TYPE:
Toast.makeText(mContext, "点击链接:" + content1, Toast.LENGTH_SHORT).show();
break;
case MENTION_TYPE:
Toast.makeText(mContext, "点击用户:" + content1, Toast.LENGTH_SHORT).show();
break;
}
});
}

@Override
public int getItemCount() {
return dataList.size();
}

class RecyclerHolder extends RecyclerView.ViewHolder {
private ExpandableTextView textView;

private RecyclerHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_item);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cretin.www.expandabletextview.model;

/**
* @date: on 2018/9/20
* @author: cretin
* @email: [email protected]
* @desc: ViewModel 不需要记住之前的状态
*/
public class ViewModel {
private String title;

public ViewModel(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Loading

0 comments on commit 2ad6be3

Please sign in to comment.