Android微信朋友圈全文、收起功能
在众多的社交类软件中,朋友圈是必不可少的,可以与好友、同学等分享自己的日常和有意思的事情,在开发社交类App时,朋友圈发表的内容你不可能让他全部显示,全部显示的话用户体验度会非常不好,这时就要用到全文、收缩的功能,朋友如果想要看你发的动态,只要点一下全文就可以查看所有的全部的内容了,如果不想看,也没有必要把这一篇文章全部都滑到底部,才能看下一条内容。
源码地址:链接:http://pan.baidu.com/s/1boQC7vT 密码:qfnk
如果链接失效,请发我邮箱:dingchao7323@qq.com看一下具体的效果图:


下边将源码贴出来供大家参考:(代码不是最简便的,但是功能是可以的)
首先写一个布局,这个布局是每个子项的布局 item_text_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="56dp"
android:orientation="vertical"
android:paddingBottom="8dp">
<TextView
android: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" />
<TextView
android: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>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="56dp"
android:alpha="0.12"
android:background="@android:color/black" />
</LinearLayout>
写一个Util类,其实是存放的数据,也可以读取数据库,获取JSON字符串,这里为了实现功能就是用固定的数据
Util.class
package com.example.friendzhanshou;
/**
* @author DingChao
* 2017/2/10
*/
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];
}
}
设置适配器
TextListAdapter.class
package com.example.friendzhanshou;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Adapter;
import android.widget.TextView;
/**
* @author DingChao
* 2017/2/10
*/
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<>();
}
@Override
public TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new TextHolder(mContent.getLayoutInflater().inflate(R.layout.item_test_list, parent, false));
}
@Override
public 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() {
@Override
public 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() {
@Override
public 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);
}
}
});
}
@Override
public 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);
}
}
}
主布局的内容:
<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.friendzhanshou.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_text_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
MainActivity中的代码也很简单,获取空间,绑定数据源:
package com.example.friendzhanshou;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRvTextList;
@Override
protected 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));
}
}
这些就是主要的实现代码,如果想要项目,上边有源码的地址。
Android微信朋友圈全文、收起功能的更多相关文章
- Android 仿微信朋友圈发动态功能(相册图片多选)
代码分享 代码名称: 仿微信朋友圈发动态功能(相册图片多选) 代码描述: 仿微信朋友圈发动态功能(相册图片多选) 代码托管地址: http://www.apkbus.com/android-15276 ...
- React Native(九)——实现仿微信朋友圈发表动态功能
好像很久都没写博客了,罪过罪过~ 许是因为刚接触App有点生疏不得窍门吧,这个月还没有更新过文章.小个把月下来,还是觉得应该边学边总结,这样才能像大神靠近(最近刚接触同业的大牛级人物,还是从中学到了很 ...
- 仿:Android - 微信 - 朋友圈 - 小视频播放,多 4 句废话算我输
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- iOS - 仿微信朋友圈视频剪切功能
分析需求 我们先看一看微信的界面 微信效果图 1.页面下部拖动左边和右边的白色竖条控制剪切视频的开始和结束时间,预览界面跟随拖动位置跳到视频相应帧画面,控制视频长度最长15秒,最短5秒 2.拖动下部图 ...
- js实现微信朋友圈模糊图片功能
本人第一次写文章 ,写的不好大家就凑合着看吧. 界面的简单效果. 主要html如下 <div id="content"> <!--模糊图片--> <i ...
- Android 高仿微信朋友圈动态, 支持双击手势放大并滑动查看图片。
转载请注明出处:http://blog.csdn.net/sk719887916/article/details/40348873 作者skay: 最近参与了开发一款旅行APP,其中包含实时聊天和动态 ...
- ionic 实现微信朋友圈分享的完整开发流程
最近开始要着手负责开发ionic的项目了,一直很好奇想实现一个微信朋友圈分享的功能,所以我就网上找了找文章来练手实现,果不其然,找到了几篇,但是发现它们的流程都不太详细,清楚,直接,还有不少坑. 今天 ...
- 微信朋友圈评论/回复/cell/键盘谈起
微信朋友圈评论功能的细节考虑及实现 微信朋友圈回复tableview iOS 实现微信朋友圈评论回复功能(一)
- 试用友盟SDK实现Android分享微信朋友圈
社会化分享是眼下必学且火热的功能.之前有写第三方登录,那仅仅是社会化分享的一部分.今天来玩玩分享微信朋友圈. 为了方便操作,还是依照步骤写. 一,注冊 注冊应用已经在这里具体说明过了,这里就不多提了. ...
随机推荐
- 模块的_name_
模块的__name__每个模块都有一个名称,在模块中可以通过语句来找出模块的名称.这在一个场合特别有用——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行.假如我们只想在程序本身 ...
- iOS开源库--最全的整理
youtube下载神器:https://github.com/rg3/youtube-dl我擦咧 vim插件:https://github.com/Valloric/YouCompleteMevim插 ...
- hadoop+海量数据面试题汇总(一)
hadoop面试题 Q1. Name the most common InputFormats defined in Hadoop? Which one is default ? Following ...
- 3D VR卡镜的使用方法
先把它展开 然后把它卡在手机中间 介绍一个VR游戏资源 Chair In a Room,这是一个立体沉浸式的3D游戏,原理是陀螺仪传感器随着手机转动可以观察整个三维房间 如图所示,点击进入,将两眼放到 ...
- jquery中,使用append增加元素时,该元素的绑定监听事件失效
举例:如果在一个<div id="resultArea"></div>中,通过append添加一个id="checkOutTip"的文本 ...
- jquery判断对象的type
利用Object.toString.call()方法 看代码 先初始化class2type,将每种对象的toString后的值和type建立对应关系 core_toString.call([])输出& ...
- kvm下Windows激活方式小计
使用kvm创建widnwos镜像模板,镜像模板默认是已经激活的正版系统,但是使用程序拷贝部署到不同的机器后发现已经激活的系统变成未激活状态,我们需求就是需要拷贝到不同的机器也能显示是正版系统 网上找了 ...
- displayport
去年买了一张ATI的显卡,但是很坑爹的是显卡后面没有VGA接口,除了一个HDMI,另外两个接口不认识,后来认识了DVI,用DVI连接显示器,剩下一个接口是什么一直没管,前两天电脑清灰,感兴趣了,去查了 ...
- 路由器、交换机学习之IP地址、使用网络掩码划分子网
局域网子网划分 对于C类IP地址来说(192.168.1.X,其中前面的192.168.1为网络号,后面的X为主机号,这样的网络中可以有254台主机,其中.0为局域网地址,.255为广播地址)进行子网 ...
- HDU1429 bfs
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...