Text过长收起

来源:互联网 发布:apache flink 编辑:程序博客网 时间:2024/04/29 17:06

TextView过长,超过6行,便收起


代码:

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;

    /** View 默认状态 ,即不是展开状态也不是收缩状态 **/
    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 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());

     //小表情判断

        SpannableString spannableString =FaceConversionUtil.getInstace().getExpressionString(getContext(), charSequence.toString());


        tv_context.setText(spannableString, bufferType);
        lookCount=0;
        coTextView.setNowType(COLLAPSIBLE_STATE_SPREAD);
        lookCount += 1;
        flag = false;
        isClicke=false;
        bt_spread.setVisibility(View.GONE);
        requestLayout();
    }

    @Override
    public void onClick(View v) {
        flag = false;
        isClicke = true;
        requestLayout();
    }

    @SuppressLint("DrawAllocation")
    @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;
            // 第一次进入操作(没有点击并且是第一次进入);
            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);
                }
            }
        }
    }

    public int getNowType() {
        return nowType;
    }

    public void setNowType(int nowType) {
        this.nowType = nowType;
    }
}



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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_context"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#7B7A78"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/bt_spread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left|center_vertical"
        android:paddingBottom="2dp"
        android:paddingRight="50dp"
        android:singleLine="true"
        android:textColor="#60C4B8"
        android:textSize="15sp"
        android:visibility="gone" />

</LinearLayout>


如果这个textView要使用这个收起,查看:

xml:

<com.xxx.xxxxxx.CollapsibleTextView
                android:id="@+id/theme"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp" />

代码自定义:

viewHolder.theme = (com.xxxx.xxxxxCollapsibleTextView) convertView.findViewById(R.id.theme);


viewHolder.theme.setDesc(entity.getTheme(), viewHolder.theme, BufferType.NORMAL);



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 当学生犯了错老师应该怎么办? 用木棒按摩脚底感觉有疙瘩怎么办 人流后按摩造成子宫复位不好怎么办 留守儿童不去学校你该怎么办 在校读书的留守儿童自身应该怎么办 进行定向越野活动时迷路了怎么办 温十系统更新后卡死无法开机怎么办 在武术比赛中被打昏了了怎么办 孩子被欺负了怎么办怎么告诉孩子 网贷雇佣黑社会到家要钱要怎么办 穿越火线枪声和脚步声没有了怎么办 魅蓝5s快充不了怎么办 门的密码输错3次怎么办 孩子剧烈运动后咳嗽到吐怎么办 前列腺穿刺后血尿一个月了怎么办 两年体检尿潜血2+怎么办严重吗? 尿结石尿不出来堵住了怎么办 四岁宝宝一直咳嗽很厉害怎么办 老公家的事业不想做了怎么办 苹果6sp手机卡顿反应慢怎么办 工程之星x3手薄密码忘了怎么办 煮绿豆汤把绿豆炒焦了怎么办 想问别人问题又不好开口怎么办 宝宝认人晚上哭着找阿姨怎么办呀 在拼多多被商家威胁了怎么办 格来云游戏禁止改画质怎么办 京东换了手机登陆要安全验证怎么办 百度网盘登录总是需要验证码怎么办 在微信群发出视频无法撤回怎么办 百度网盘客户端说网络有风险怎么办 百度网盘绑定的手机号注销了怎么办 百度网盘绑定的手机号不用了怎么办 百度网盘内存怎么清空不了怎么办 百度网盘下载时本地空间不足怎么办 百度网盘文件放到桌面太大怎么办 百度有钱花借款没有钱还怎么办 安心流量包扣了两百多钱怎么办 邮箱里超大附件的文件过期了怎么办 ps时间轴储存出现参数错误怎么办 苹果手机网易云会员支付失败怎么办 鲸钱包网络异常需重新加载怎么办