一、添加依赖

implementation 'com.android.support:recyclerview-v7:28.0.0'

二、资源配置

1.drawable文件夹下的shape文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="@color/colorAccent"/>
</shape>

2.layout布局文件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"tools:context=".MainActivity"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rv_text_list"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

item_test_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_hend"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginRight="16dp"android:background="@drawable/circle"android:gravity="center"android:text="1"android:textColor="@android:color/white"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:alpha="0.87"android:text="丁先森"android:textColor="@android:color/black"android:textSize="14sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="56dp"android:orientation="vertical"android:paddingBottom="8dp"><TextViewandroid:id="@+id/tv_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:alpha="0.85"android:ellipsize="end"android:text=""android:textColor="@android:color/black"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_expand_or_collapse"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="全文"android:textColor="@color/colorPrimaryDark"android:textSize="14sp" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginLeft="56dp"android:alpha="0.12"android:background="@android:color/black" />
</LinearLayout>

逻辑代码

MainActivity

public class MainActivity extends AppCompatActivity {private RecyclerView mRvTextList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mRvTextList= (RecyclerView) findViewById(R.id.rv_text_list);mRvTextList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));mRvTextList.setAdapter(new TextListAdapter(this));}
}

适配器TextListAdapter

public class TextListAdapter extends RecyclerView.Adapter<TextListAdapter.TextHolder> {private Activity mContent;private final int MAX_LINE_COUNT = 3;private final int STATE_UNKNOW = -1;private final int STATE_NOT_OVERFLOW = 1;//文本行数不能超过限定行数private final int STATE_COLLAPSED = 2;//文本行数超过限定行数,进行折叠private final int STATE_EXPANDED = 3;//文本超过限定行数,被点击全文展开private SparseArray<Integer> mTextStateList;public TextListAdapter(Activity context) {mContent = context;mTextStateList = new SparseArray<>();}@Overridepublic TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {return new TextHolder(mContent.getLayoutInflater().inflate(R.layout.item_test_list, parent, false));}@Overridepublic void onBindViewHolder(final TextHolder holder, final int position) {holder.hend.setText(position + 1 + "");//设置头部的文字holder.name.setText(Util.getName(position));//设置名称int state = mTextStateList.get(position, STATE_UNKNOW);
//        如果该itme是第一次初始化,则取获取文本的行数if (state == STATE_UNKNOW) {holder.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {@Overridepublic boolean onPreDraw() {
//                    这个回掉会调用多次,获取玩行数后记得注销监听holder.content.getViewTreeObserver().removeOnPreDrawListener(this);
//                    holder.content.getViewTreeObserver().addOnPreDrawListener(null);
//                    如果内容显示的行数大于限定显示行数if (holder.content.getLineCount() > MAX_LINE_COUNT) {holder.content.setMaxLines(MAX_LINE_COUNT);//设置最大显示行数holder.expandOrCollapse.setVisibility(View.VISIBLE);//让其显示全文的文本框状态为显示holder.expandOrCollapse.setText("全文");//设置其文字为全文mTextStateList.put(position, STATE_COLLAPSED);} else {holder.expandOrCollapse.setVisibility(View.GONE);//显示全文隐藏mTextStateList.put(position, STATE_NOT_OVERFLOW);//让其不能超过限定的行数}return true;}});holder.content.setMaxLines(Integer.MAX_VALUE);//设置文本的最大行数,为整数的最大数值holder.content.setText(Util.getContent(position));//用Util中的getContent方法获取内容} else {
//            如果之前已经初始化过了,则使用保存的状态,无需在获取一次switch (state) {case STATE_NOT_OVERFLOW:holder.expandOrCollapse.setVisibility(View.GONE);break;case STATE_COLLAPSED:holder.content.setMaxLines(MAX_LINE_COUNT);holder.expandOrCollapse.setVisibility(View.VISIBLE);holder.expandOrCollapse.setText("全文");break;case STATE_EXPANDED:holder.content.setMaxLines(Integer.MAX_VALUE);holder.expandOrCollapse.setVisibility(View.VISIBLE);holder.expandOrCollapse.setText("收起");break;}holder.content.setText(Util.getContent(position));}//        设置显示和收起的点击事件holder.expandOrCollapse.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int state = mTextStateList.get(position, STATE_UNKNOW);if (state == STATE_COLLAPSED) {holder.content.setMaxLines(Integer.MAX_VALUE);holder.expandOrCollapse.setText("收起");mTextStateList.put(position, STATE_EXPANDED);} else if (state == STATE_EXPANDED) {holder.content.setMaxLines(MAX_LINE_COUNT);holder.expandOrCollapse.setText("全文");mTextStateList.put(position, STATE_COLLAPSED);}}});}@Overridepublic int getItemCount() {return 15;}public class TextHolder extends RecyclerView.ViewHolder {public TextView hend;public TextView name;public TextView content;public TextView expandOrCollapse;public TextHolder(View itemView) {super(itemView);
//            绑定xml布局中的控件hend = (TextView) itemView.findViewById(R.id.tv_hend);name = (TextView) itemView.findViewById(R.id.tv_name);content = (TextView) itemView.findViewById(R.id.tv_content);expandOrCollapse = (TextView) itemView.findViewById(R.id.tv_expand_or_collapse);}}
}

Util工具类

public class Util {private static String[] nameArray = new String[]{"Windows", "Mac", "Linux"};private static String[] contentArray = new String[]{"在动作类影片中,只要发生混乱,那么绝对就有木仓战。现在的技术越来越发达,电影或电视中的特效也做的越来越逼真,演员们被木仓打中的效果也很形象,我们经常能看到被木仓打中的伤口血淋林的暴露在大屏幕中,从演员的表演中我们能看到木仓击是很痛的,那么你们有想过被木仓打中到底会有多痛?什么感觉吗?网站有网友为我们分享被子弹打中的感觉\n" +"1、“老实说,比我想象中的感觉要轻很多。本来我以为很痛,可是被打中后就像是被棒球击中的感觉一样,刚开始的几秒钟没什么知觉,过会才感到痛\n" +"2、“被子弹打到的感觉就像是一直有人拿针扎你一样,刺痛刺痛的。”\n" +"3、“我当初大腿被木仓击中,子弹直接从我的大腿中传过去,连带着我的肌腱也被击中,那种感觉我觉得用疼痛两个字已经不足以形容了\n" +"4、“在我十七岁的时候,脚被木仓击中,当时我以为是被蜜蜂蛰了,因为仿佛听到了蜜蜂的声音,没过几秒钟,脚上就传来灼热感,这才知道原来是被木仓击中了。\n" +"5、“我只是听到的木仓声,却没有意识到自己中木仓了。直到血流出来才意识到。所以,对我来讲,被子弹击中没什么感觉。","GNOME or KDE desktop\n" +" processor with support for AMD Virtualization™ (AMD-V™)"};/*** 获取文本内容根据下标** @param position* @return*/public static String getContent(int position) {return contentArray[position % contentArray.length];}/*** 获取名称根据下标** @param position* @return*/public static String getName(int position) {return nameArray[position % contentArray.length];}
}

Android 仿微信朋友圈 全文,收起功能相关推荐

  1. android 朋友圈功能,Android仿微信朋友圈全文收起功能示例(附源码)

    在众多的社交类软件中,朋友圈是必不可少的,可以与好友.同学等分享自己的日常和有意思的事情,在开发社交类App时,朋友圈发表的内容你不可能让他全部显示,全部显示的话用户体验度会非常不好,这时就要用到全文 ...

  2. android仿微信发布动态功能,Android仿微信朋友圈发布动态功能

    一.前言 应工作上的要求,需要有一个类似于微信朋友圈发动态上传图片的功能,想起曾经已经做过了,但奈何不忍看自己以前写的代码的惨状,觉得重新封装一个使用方便,易于维护的类似功能的类,自己之后用起来也顺手 ...

  3. android 仿微信朋友圈发布动态功能

    https://blog.csdn.net/qq_34501274/article/details/72911343

  4. Android自定义弹窗模仿微信,Android 仿微信朋友圈点赞和评论弹出框功能

    本文简单模仿微信朋友圈的点赞和评论弹出框,布局等细节请忽略,着重实现弹出框.发评论,及弹出位置的控制. 1. 微信弹出框 微信朋友圈的点赞和评论功能,有2个组成部分: 点击左下角的"更多&q ...

  5. android 仿微信朋友圈 评论,2020年android 仿微信朋友圈 评论

    2020年android 仿微信朋友圈 评论 1.如果有人问我:那些艰难的岁月你是怎么熬过来的?我想我只有一句话回答:我有一种强大的精神力量支撑着我,这种力量名字叫"想死又不敢" ...

  6. Android仿微信朋友圈发图片和文字

    Android仿微信朋友圈发图片和文字的一个开源项目,其在github上的项目主页是:https://github.com/zhangphil/FangWeiXinPengYouQuanFaTuPia ...

  7. android 微信朋友圈 全功能,Android仿微信朋友圈文字展开全文功能 Android自定义TextView仿微信朋友圈文字展开全文功能...

    Android自定义TextView仿微信朋友圈文字信息,展开全文功能 代码及注释如下: 首先写一个xml文件 showmore.xml: android:orientation="vert ...

  8. Android仿微信朋友圈7实现点赞功能

    前言: 之前一直有朋友问我点赞怎么实现?今天趁着休息时间整理出来,其实点赞的功能和用户评论差不多,都是显示一个用户列表,只不过评论有评论内容和回复评论功能.实现点赞的思路如下: 1.当用户点击点赞按钮 ...

  9. Android仿微信朋友圈6之实现消息提醒功能

    之前有朋友问我消息提醒咋实现,我一直没有整理出来,今天就放出来.微信朋友圈的消息提醒就是收到朋友的评论后背景底部显示消息条数和评论用户,顶部是一张相册背景和当前用户昵称头像. 1.消息提醒的布局如下: ...

最新文章

  1. 树莓派练习程序(蜂鸣器)
  2. 民营企业的ERP之路
  3. HTTP Error 503. The service is unavailable.
  4. 学术之问2018-04-05
  5. 【10.20校内测试】【小模拟】【无向图建树判奇偶环】【树上差分】
  6. 形式化验证工具TLA+:程序员视角的入门之道
  7. 收藏 | 一文读懂深度学习中的各种卷积
  8. scala 高级类型
  9. HAOI2011 Problem b 洛谷P2522
  10. 菜鸟涂鸦作品展_No.24
  11. Julia:提高性能的N个点
  12. java真题_2017年JAVA考试试题及答案
  13. 实验报告 三密码破解技术
  14. 基于51单片机的指纹考勤系统密码锁门禁原理图PCB程序设计
  15. PCB设计中基板颜色含义
  16. 故障:笔记本开机时自动打开 NumLock 键
  17. ftl不存在为真_判断一件书法作品是否具备收藏价值可以归纳为四个字:真、优、高、古。...
  18. Excel表格批量更改出生年月格式
  19. 全方位解读小红书笔记数据、千瓜功能指南更新!
  20. [CTS2019]氪金手游 概率Dp,树形Dp,容斥原理

热门文章

  1. cluster-trace-v2018 阿里集群数据集中文简介
  2. DHCP Snooping简述
  3. Metasploit基本用法
  4. 数据单位:概况容量 Byte、KB、MB、GB、TB、PB、EB、ZB、YB、NB、DB、CB、XB
  5. OpenCV cvtColor BGR2YUV420
  6. 【前端】jQuery常用基础知识点总结
  7. npm报错:A complete log of this run can be fund in: C\Users\用户\AppData\Roaming\npm-cache_logs\解决方案(清理缓)
  8. 2022-2027年中国电气化铁路牵引供电系统变压器行业发展前景及投资战略咨询报告
  9. 【Linux】用户组与文件目录权限
  10. 【学习笔记】Java基础知识点——第7章·集合