最近开发需求中要模仿微信朋友圈文章的展开收起功能,网上找了找,发现都有问题,于是乎自己在前辈的基础上进行了一定量的修改,下边将源码贴出来供大家参考:

1.主Activity布局文件就不粘贴了,很简单,就一个ListView.

2.主Activity功能实现:

package com.example.textviewdemo;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.BufferType;public class MainActivity extends Activity {
String mStr;
int type;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Globl.map = new HashMap<Integer, Boolean>();
ListView listview = (ListView) findViewById(R.id.listview);
mStr = "手指在ListView上下滚动时,ListViewItem背景变黑,因为在滚动的时候为了提升性能做了优化,为提高滚动的性能,Android 框架在ListView中引入CacheColorHint属性。如果该值为非0,则说明该ListView绘制在单色不透明的背景上,在默认情况下该值 为#191919,也就是黑色主题中的黑色背景颜色值,这样当ListView滚动的时候";
listview.setAdapter(new MyListAdpter(this));
}
class MyListAdpter extends BaseAdapter {
Context con;
CollapsibleTextView tv;public MyListAdpter(Context con) {
this.con = con;}@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}HashMap<Integer, View> hashM = new HashMap<Integer, View>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
View view;
if (hashM.get(position) == null) {
holder = new Holder();
view = LayoutInflater.from(con).inflate(R.layout.item_list,
null);
holder.tv = (CollapsibleTextView) view
.findViewById(R.id.tv_text);
holder.tvcount = (TextView) view.findViewById(R.id.tvcount);
view.setTag(holder);
hashM.put(position, view);
} else {
view = hashM.get(position);
holder = (Holder) view.getTag();
}// if (Globl.map.get(position) == false) {
// Globl.map.put(position, false);
// type = 2;
// } else {
// type = 1;
// }
// tv.setNowType(type);
// int typeNow = tv.getNowType();
holder.tvcount.setText(position + "");
holder.tv.setDesc(mStr, holder.tv, BufferType.NORMAL);
return view;
}class Holder {
CollapsibleTextView tv;
TextView tvcount;
}
}}

3.自定义控件CollapsibleTextView 源码:

/*** @Explain: Text过长收起 带有查看全文/收起功能控件;* @Author:LYl* @Time:2014-11-27 下午4:33:05* @Version V2.1.54*/
public class CollapsibleTextView extends LinearLayout implements
OnClickListener {
/** 最大显示的行数 */
private static final int DEFAULT_MAX_LINE_COUNT = 8;
/** 实际展示的行数 */
private static final int DEFAULT_SHOW_LINE_COUNT = 6;private static final int COLLAPSIBLE_STATE_NONE = 0;
/** View处于展开状态 **/
private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
/** view收缩时状态 **/
private static final int COLLAPSIBLE_STATE_SPREAD = 2;
/** 显示内容的View */
private TextView tv_context;
/** 展开/收起按钮 */
private TextView bt_spread;
private String shrinkup;
private String spread;
/** 当前正处于的状态 */
// private int mState;
private boolean flag;
private int nowType;
private CollapsibleTextView coTextView;
/** 判断是不是点击了查看更多、收起 */
private boolean isClicke = false;
private int lookCount = 0;public CollapsibleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
shrinkup = "收起";
spread = "查看全文";
View view = inflate(context, R.layout.collapsible_textview, this);
view.setPadding(0, -1, 0, 0);
tv_context = (TextView) view.findViewById(R.id.tv_context);
bt_spread = (TextView) view.findViewById(R.id.bt_spread);
bt_spread.setOnClickListener(this);
}public CollapsibleTextView(Context context) {
this(context, null);
}/**
* 赋值
*/
public final void setDesc(CharSequence charSequence,
CollapsibleTextView tv, BufferType bufferType) {
this.coTextView = tv;
// 对内容中的网址进行处理;
tv_context.setAutoLinkMask(Linkify.WEB_URLS);
tv_context.setMovementMethod(LinkMovementMethod.getInstance());
tv_context.setText(charSequence, bufferType);
// 初始类型
if (lookCount == 0) {
coTextView.setNowType(COLLAPSIBLE_STATE_SPREAD);
}
lookCount += 1;
// TODO LYL 放到ListView中需要加下句:falg=false;一般情况去掉就可
flag = false;
requestLayout();
}@Override
public void onClick(View v) {
flag = false;
isClicke = true;
requestLayout();
}@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!flag) {
flag = true;
if (tv_context.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
bt_spread.setVisibility(View.GONE);
tv_context.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
coTextView.setNowType(COLLAPSIBLE_STATE_NONE);
} else {
post(new InnerRunnable());}
}
}class InnerRunnable implements Runnable {
@Override
public void run() {
int zType = 0;
// 第一次进入操作(没有点击并且是第一次进入);
System.out.println("lookCount:" + lookCount);
if (!isClicke && lookCount == 1) {
if (coTextView.getNowType() == COLLAPSIBLE_STATE_SPREAD) {
tv_context.setMaxLines(DEFAULT_SHOW_LINE_COUNT);
bt_spread.setVisibility(View.VISIBLE);
bt_spread.setText(spread);
zType = COLLAPSIBLE_STATE_SHRINKUP;
} else if (coTextView.getNowType() == COLLAPSIBLE_STATE_SHRINKUP) {
tv_context.setMaxLines(Integer.MAX_VALUE);
bt_spread.setVisibility(View.VISIBLE);
bt_spread.setText(shrinkup);
zType = COLLAPSIBLE_STATE_SPREAD;
}
coTextView.setNowType(zType);
// 点击了查看更多、收起转换状态;
} else if (isClicke) {
isClicke = false;
if (coTextView.getNowType() == COLLAPSIBLE_STATE_SPREAD) {
tv_context.setMaxLines(DEFAULT_SHOW_LINE_COUNT);
bt_spread.setVisibility(View.VISIBLE);
bt_spread.setText(spread);
coTextView.setNowType(COLLAPSIBLE_STATE_SHRINKUP);
} else if (coTextView.getNowType() == COLLAPSIBLE_STATE_SHRINKUP) {
tv_context.setMaxLines(Integer.MAX_VALUE);
bt_spread.setVisibility(View.VISIBLE);
bt_spread.setText(shrinkup);
coTextView.setNowType(COLLAPSIBLE_STATE_SPREAD);
}
// 滑动listView 从新载入到可见界面 不做操作,保持原有状态;(为了后面看得人能够更理解写上)
} else if (!isClicke && lookCount != 1) {}}
}public int getNowType() {
return nowType;
}public void setNowType(int nowType) {
this.nowType = nowType;
}}

4.自定义控件加载的Layout:

<?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="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_context"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="4.0dip"android:gravity="center_vertical"android:textColor="#ff000000"android:textSize="14.0dip" /><TextViewandroid:id="@+id/bt_spread"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="4.0dip"android:gravity="center"android:singleLine="true"android:textColor="#ff576b95"android:textSize="18.0dip"android:visibility="gone" />
</LinearLayout>

现在经过修改后其实还是有点小问题的,如果按照上边的写的话,偶尔点击收起/展开按钮会失效,但是一滑动整体ListView就会执行操作;如果,将getView()方法中的HashMap判断换成ConvertView判断,点击就没问题了,但是展开一个item后慢慢的滑出屏幕,您会发现,下边刚刚出现的Item会处于打开状态,本人才疏学浅,猜测可能是复用了,目前没想到什么好的方法解决,希望能有大神指点一下,这是目的之一,同时也希望能帮助初学者!

下边是运行后的结果:

Android仿微信朋友圈查看全文/收起功能(雷惊风)相关推荐

  1. Android仿微信朋友圈,全文收起功能,附源码

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

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

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

  3. android 微信评论功能,Android仿微信朋友圈点赞和评论功能

    最近在做朋友圈的项目,所以写一个Android仿朋友圈点赞和评论功能Demo,代码就是简单实现了一下功能,没有做优化,凑合看. 图文排列是用的RecyclerView实现的,弹窗效果是用的自定义的Po ...

  4. Android 仿微信朋友圈查看

    项目要做一个类似于这样的功能,就做了. 项目下载地址:http://download.csdn.net/detail/u014608640/9917626 一,看下效果: 二.activity类 pu ...

  5. android com.mylhyl,Android 高仿微信朋友圈拍照上传功能

    模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. photopicker的使用 这是一个支持选择多张图片,点击图片放大,图片之间 ...

  6. android 微信高仿,Android 高仿微信朋友圈拍照上传功能

    模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. PhotoPicker的使用 这是一个支持选择多张图片,点击图片放大,图片之间 ...

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

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

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

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

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

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

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

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

最新文章

  1. rsync使用sudo权限
  2. 上接稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据
  3. metapath2vec: Scalable Representation Learning for Heterogeneous Networks
  4. 关于apache kylin 安装32位linux办法
  5. yum源yum-fastestmirror
  6. win10资源管理器怎么打开_让你效率倍增的电脑神器,最强资源管理器增强工具「QTTabBar」...
  7. JFinal 调用 oracle 存储过程的 步骤
  8. docker制作容器(待更新)
  9. wxpython学习笔记
  10. php生成水印函数,PHP缩略图生成和图片水印制作
  11. SVN(一)------windows下搭建SVN服务器及SVN客户端安装教程
  12. 数据分析软件SPSS22的授权及汉化
  13. 温州大学《机器学习》课件!
  14. 支持APP的打印服务器,TP-LINK双频无线路由器打印服务器客户端软件
  15. 电脑端微信文件的存储位置在哪?
  16. QGIS:创建矢量图层
  17. 文本编辑器的制作(C#)
  18. 考研线性代数(矩阵)
  19. 腾讯云自定义配置购买云服务器图文操作教程 新手必看!
  20. matlab 绘制三维空间隐函数自由曲面

热门文章

  1. 资产配置神器--美林时钟帮你获取超额收益
  2. linux vim m,Linux vi(m)用法 (转)
  3. BH1750FVI光强度传感器及其STM32驱动程序
  4. Movavi Screen Recorder 10 Mac(全能录屏软件) 中文激活版
  5. Win7——无Internet访问权限
  6. 终于明白#!bin/sh是什么意思了
  7. c++除法保留小数_小学数学整数和小数的应用题解答方法公式汇总,新学期必备...
  8. 苹果5完美越狱_iOS 9.3.5如何越狱 iOS 9.3.5免费越狱教程「步骤详解」
  9. nvme协议 sata接口_NVMe/SATA SSD有啥不一样?萌新怎么选
  10. phalcon 自动加载_Phalcon自动加载(PHP自动加载),phalcon加载php_PHP教程