Android显示全文折叠控件使用方法详解

一般列表里文字太多的一个折叠效果的空间,效果图如下。

当文字超过设定的行数后就折叠,小于设定行数不显示展开按钮。下面上代码。

先看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@color/color_white" >
 <TextView
 android:id="@+id/desc_tv"
 style="@style/font2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center_vertical" />
 <TextView
 android:id="@+id/desc_op_tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/desc_tv"
 android:gravity="center_vertical"
 android:singleLine="true"
 android:text="@string/quan_wen"
 android:textColor="#5f897b"
 android:textSize="16sp"
 android:visibility="gone" />
</RelativeLayout>

很简单,上面的TextView显示主要的文本内容,下面的就是折叠的时候点击的。

下面是自定义。

package xxx;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import xxx.R;
/**
 * 查看全文控件
 */
public class CollapsibleTextView extends LinearLayout implements View.OnClickListener {
 private static final int COLLAPSIBLE_STATE_NONE = 0;// 不显示
 private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;// 显示收起
 private static final int COLLAPSIBLE_STATE_SPREAD = 2;// 显示全文
 private int mState = COLLAPSIBLE_STATE_SPREAD;
 private static final String COLLAPSIBLE_STATE_SHRINKUP_TEXT = "收起";
 private static final String COLLAPSIBLE_STATE_SPREAD_TEXT = "全文";
 private TextView mText;
 /**
 * @return Returns the mText.
 */
 public TextView getmText() {
 return mText;
 }
 public int getmState() {
 return mState;
 }
 public void setmState(int mState) {
 this.mState = mState;
 }
 private TextView mTextTip;
 private changeState changeStateCallBack;
 private boolean isNeedLayout;
 private int maxLineCount = 8;
 private final Handler handler = new Handler() {
 @Override
 public void dispatchMessage(Message msg) {
  if (mText.getLineCount() <= maxLineCount) {
  // 行数不足不做处理
  mState = COLLAPSIBLE_STATE_NONE;
  mText.setMaxLines(Integer.MAX_VALUE);
  mTextTip.setVisibility(View.GONE);
  }
  else {
  switch (mState) {
  case COLLAPSIBLE_STATE_SPREAD:
   // 全文状态
   mText.setMaxLines(maxLineCount);
   mTextTip.setVisibility(View.VISIBLE);
   mTextTip.setText(COLLAPSIBLE_STATE_SPREAD_TEXT);
   break;
  case COLLAPSIBLE_STATE_SHRINKUP:
   // 收起状态
   mText.setMaxLines(Integer.MAX_VALUE);
   mTextTip.setVisibility(View.VISIBLE);
   mTextTip.setText(COLLAPSIBLE_STATE_SHRINKUP_TEXT);
   break;
  default:
   // 除非发生不可知状态,一般不会执行到这个
   mState = COLLAPSIBLE_STATE_NONE;
   mText.setMaxLines(Integer.MAX_VALUE);
   mTextTip.setVisibility(View.GONE);
   break;
  }
  }
 }
 };
 public CollapsibleTextView(Context context) {
 this(context, null);
 initView();
 }
 public CollapsibleTextView(Context context, AttributeSet attrs) {
 super(context, attrs);
 initView();
 }
 @SuppressLint("NewApi")
 public CollapsibleTextView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 initView();
 }
 private void initView() {
 View view = inflate(getContext(), R.layout.collapsible_textview, this);
 view.setPadding(0, -1, 0, 0);
 mText = (TextView) view.findViewById(R.id.desc_tv);
 mTextTip = (TextView) view.findViewById(R.id.desc_op_tv);
 mTextTip.setOnClickListener(this);
 }
 /**
 * 设置文本
 *
 * @param charSequence
 * @param bufferType
 */
 public final void setText(CharSequence charSequence, BufferType bufferType) {
 isNeedLayout = true;
 mState = COLLAPSIBLE_STATE_SPREAD;
 mText.setText(charSequence, bufferType);
 }
 /**
 * 设置文本
 *
 * @param charSequence
 */
 public final void setText(CharSequence charSequence) {
 isNeedLayout = true;
 mText.setText(charSequence);
 }
 @Override
 public void onClick(View v) {
 isNeedLayout = true;
 if (mState == COLLAPSIBLE_STATE_SPREAD) {
  // 如果是全文状态,就改成收起状态
  mState = COLLAPSIBLE_STATE_SHRINKUP;
  requestLayout();
 }
 else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
  // 如果是收起状态,就改成全文状态
  mState = COLLAPSIBLE_STATE_SPREAD;
  requestLayout();
 }
 if (null != changeStateCallBack) {
  changeStateCallBack.changeFlag(v);
 }
 }
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 super.onLayout(changed, l, t, r, b);
 if (isNeedLayout) {
  isNeedLayout = false;
  handler.sendMessage(Message.obtain());
 }
 }
 public int getMaxLineCount() {
 return maxLineCount;
 }
 public void setMaxLineCount(int maxLineCount) {
 this.maxLineCount = maxLineCount;
 }
 public changeState getChangeStateCallBack() {
 return changeStateCallBack;
 }
 public void setChangeStateCallBack(changeState changeStateCallBack) {
 this.changeStateCallBack = changeStateCallBack;
 }
 public interface changeState {
 public void changeFlag(View v);
 }
}

点击展开后重新绘制根据状态值触发。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android UI实现多行文本折叠展开效果

    上文介绍了单行文本水平触摸滑动效果,通过EditText实现TextView单行长文本水平滑动效果. 本文继续介绍了多行文本折叠展开,自定义布局View实现多行文本折叠和展开 1.概述 经常在APP中能看到有引用文章或大段博文的内容,他们的展示样式也有点儿意思,默认是折叠的,当你点击文章之后它会自动展开.再次点击他又会缩回去. 网上有找到部分效果,感觉不是很满意.最后自己尝试用 自定义布局layout 写了个demo.比较简陋,不过可以用了.有这方面需求的朋友可以稍加改造下.如有更好的创意,也不

  • Android折叠式Toolbar使用完全解析(CollapsingToolbarLayout)

    简介 在各种不同的应用中,大家可能会经常见到这样一个效果:Toolbar是透明的,有着一个背景图片以及大标题,随着页面向上滑动,其标题逐渐缩放到Toolbar上,而背景图片则在滑动到一定程度后变成了Toolbar的颜色,这种效果也即是折叠式效果.其实这种效果在GitHub上面已经有很多开源库实现了,但是Google在其推出的Design Library库中也给出了一个这种控件,让我们很方便地实现了这种效果.这个控件是CollapsingToolbarLayout,它是一个增强型的FrameLay

  • Android TextView实现多文本折叠、展开效果

    背景 在开发过程中,当我们的需求中包含说说或者评论等内容的展示时,我们都会考虑当内容太多时该如何显示.当内容的字数太多,如果全部展示出来可能会影响体验效果,但是又不能只截取一部分内容进行展示,此时就需要考虑使用多行显示折叠的效果来实现. 效果图: 使用 1.布局文件调用 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:o

  • Android TextView多文本折叠展开效果

    最近做项目,效果图要用到TextView的折叠,超过一定行数的时候,就会折叠起来,点击可以展开.网上找了一些效果,自己也稍作了修改.便拿来与网友分享分享. 参考文献:Android UI实现多行文本折叠展开效果 第一种:通过多个布局组合实现 大概步骤: - 定义布局,垂直的线性LinearLayout布局.TextView和ImageView. 在layout中定义基本组件. - 设置TextView的高度为指定行数*行高. 不使用maxLine的原因是maxLine会控制显示文本的行数,不方便

  • Android开发实现的文本折叠点击展开功能示例

    本文实例讲述了Android开发实现的文本折叠点击展开功能.分享给大家供大家参考,具体如下: 信息栏,景点介绍,购物信息,进场会使用到文本折叠的方法 实现非常简单,这里就不哆嗦了 效果如下: Demo:https://github.com/LonglyWolf/NavigationSystemHLJU 这里用到了三方类库,在app/gradle添加依赖如下: //文本过长 点击展开全部 implementation 'com.ms-square:expandableTextView:0.1.4'

  • Android中RecyclerView实现多级折叠列表效果(二)

    前言 在本文开始之前请大家先看一下这篇文章:http://www.jb51.net/article/113510.htm 上面的这篇文章是之前写的,里面发现有很多不好用地方,也学到些新姿势,改动了许多地方.下面来看看详细的介绍: 要点: 1.可以通过后台控制Item的展示. 2.TreeRecyclerAdapter,可以展开,折叠.多级展示 3.adapter可以使用装饰者模式进行扩展.支持EmptyAdapter.可以添加headview和footview 4.item的样式可以编写文档,t

  • Android中RecyclerView实现多级折叠列表效果(TreeRecyclerView)

    前言 首先不得不吐槽一下产品,尼玛为啥要搞这样的功能....搞个两级的不就好了嘛...自带控件,多好.三级,四级,听说还有六级的....这样丧心病狂的设计,后台也不好给数据吧. 先看看效果: 两级的效果: 三级的效果: 全部展开的效果(我只写了五级) 说说为什么写这货吧: 公司产品提出三级这个需求后,我就在网上找啊找. 找的第一个,发现实现其实是ExpandListview嵌套. 找的第二个,ExpandRecyclview,然后就用呗,发现三级展开很卡,看源码, 发现是RecyclerView

  • Android中FoldingLayout折叠布局的用法及实战全攻略

    一.概述 无意中翻到的FoldingLayout的介绍的博客,以及github地址.感觉很nice呀,于是花了点时间研究以及编写,本篇博客将带大家从最基本的原理分析,一步一步的实现我们的FoldingLayout,当然了,如果你能力过硬,可以直接下载github上的代码进行学习. 博客基本分为以下几个部分: 1.Matrix的setPolyToPoly使用 2.在图片上使用渐变和阴影 3.初步的FoldingLayout的实现,完成图片的折叠显示(可控制折叠次数.包含阴影的绘制) 4.引入手势,

  • Android TextView仿微信可折叠效果

    在微信朋友圈中,发送大量的文本信息时,在展示的时候微信会将该文本信息进行折叠处理,出现"全文","收起"的操作提示.当点击全文时,才能看到全部的文本信息,正好最近的项目中也提出了类似的需求,这里就对该自定义View的实现的方法进行了整理. 代码如下: 1.自定义的View: import android.content.Context; import android.content.res.TypedArray; import android.util.Attrib

  • android给RecyclerView加上折叠的效果示例

    RecyclerView有很高的自由度,可以说只有想不到没有做不到,真是越用越喜欢.这次用超简单的方法,让RecyclerView带上折叠的效果. 效果是这样的. 总结一下这个列表的特点,就是以下三点: 1. 重叠效果: 2. 层次感: 3. 首项的差动效果. 下面我们来一个个解决. 我们新建一个ParallaxRecyclerView,让它继承RecyclerView,并使用LinearLayoutManager作为布局管理器. 重叠效果 其实就是每一项都搭一部分在它前面那项而已.我们知道,R

随机推荐