自定义ExpandTextView

2017-07-24  本文已影响0人  我弟是个程序员

严格来说,这是一个组合方式的自定义。

先看效果:

收起 展开

效果很简单。下面的文字还是图片,以及他们的的位置,都可以自己修改。
看原码:

package com.dingfang.org.customdemo.wedget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingfang.org.customdemo.R;

public class ExpandTextView extends LinearLayout {
    public static final int DEFAULT_MAX_LINES = 3;
    //展示文字的控件
    private TextView contentText;
    //按钮控件
    private TextView textPlus;

    //要展示的文字
    private String showText;
    //展示几行
    private int showLines;

    //可要可不要,把开关状态暴露出去
    private ExpandStatusListener expandStatusListener;
    //开关状态
    private boolean isExpand;

    public ExpandTextView(Context context) {
        super(context);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView();
    }

    /**
     * 初始化视图文件以及点击事件
     */
    private void initView() {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_magic_text, this);
        contentText = (TextView) findViewById(R.id.contentText);
        if(showLines > 0){
            contentText.setMaxLines(showLines);
        }
        setText(showText);

        textPlus = (TextView) findViewById(R.id.textPlus);
        textPlus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String textStr = textPlus.getText().toString().trim();
                if("全文".equals(textStr)){
                    contentText.setMaxLines(Integer.MAX_VALUE);
                    textPlus.setText("收起");
                    setExpand(true);
                }else{
                    contentText.setMaxLines(showLines);
                    textPlus.setText("全文");
                    setExpand(false);
                }
                //通知外部状态已变更
                if(expandStatusListener != null){
                    expandStatusListener.statusChange(isExpand());
                }
            }
        });
    }

    /**
     * 初始化自定义属性
     * @param attrs
     */
    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
        try {
            showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
            showText = typedArray.getString(R.styleable.ExpandTextView_text);
        }finally {
            //这个使用自定义属性后的 必做操作,
            typedArray.recycle();
        }
    }

    /**
     * 设置文字
     * @param content
     */
    public void setText(final CharSequence content){
        showText = content.toString();
        //这里在完成绘制之前监听,并设置相关属性
        contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                // 避免重复监听
                contentText.getViewTreeObserver().removeOnPreDrawListener(this);

                int linCount = contentText.getLineCount();

                if(linCount > showLines){

                    if(isExpand){
                        contentText.setMaxLines(Integer.MAX_VALUE);
                        textPlus.setText("收起");
                    }else{
                        contentText.setMaxLines(showLines);
                        textPlus.setText("全文");
                    }
                    textPlus.setVisibility(View.VISIBLE);
                }else{
                    textPlus.setVisibility(View.GONE);
                }
                return true;
            }


        });
        contentText.setText(showText);
    }

    public void setExpand(boolean isExpand){
        this.isExpand = isExpand;
    }

    public boolean isExpand(){
        return this.isExpand;
    }

    public void setExpandStatusListener(ExpandStatusListener listener){
        this.expandStatusListener = listener;
    }

    public static interface ExpandStatusListener{
        void statusChange(boolean isExpand);
    }

}

因为时组合空间,少不了视图文件layout_magic_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/contentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#232323"
        android:textSize="14sp"
        android:text=""
        />
    <TextView
        android:id="@+id/textPlus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#8290AF"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text=""
        />

</LinearLayout>

上面还有自定义属性,那么还有需要再values文件夹下的attrs文件中声明属性名称:

 <declare-styleable name="ExpandTextView">
        <attr name="showLines" format="integer"/>
        <attr name="text" format="string"/>
    </declare-styleable>

还有一些颜色或是字符串定义的常量,这里就不展示了。

上一篇下一篇

猜你喜欢

热点阅读