美文网首页Android 开发技术分享高级UIandroid开发技巧
Android超简单实现文字展开收起效果

Android超简单实现文字展开收起效果

作者: i小灰 | 来源:发表于2020-04-18 10:06 被阅读0次

    目录

    image

    前言

    公司项目需要一个类似微信朋友圈文字展开收起的效果,为了方便一开始我在网上找类似的效果实现,结果发现代码量都很多计算量很大,而我头脑这么简单所以看起来比较费劲因此我就用非常简单的方法写了一个自定义的展开收起控件。

    效果展示

    image

    实现原理

    原理很简单,需要用3个TextView,其中一个是展示文字的,一个是用来计算文字的行数,一个是用来展开和收起的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!--    展示文字-->
        <TextView
            android:id="@+id/view_seemore_tvcontent"
            android:textColor="#000"
            android:maxLines="2"
            android:ellipsize="end"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <!--    占位置的隐藏,并且高度设置的很小,用来获取行数-->
        <TextView
            android:id="@+id/view_seemore_tvlinecount"
            android:textColor="#000"
            android:visibility="invisible"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="1dp"/>
    <!--    显示更多和收起-->
        <TextView
            android:id="@+id/view_seemore_tv_seemore"
            android:layout_marginTop="5dp"
            android:text="查看更多"
            android:textColor="#00f"
            android:visibility="gone"
            android:maxLines="2"
            android:ellipsize="end"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    
    

    然后需要在自定义控件的类中做相应的处理:

    class SeeMoreView:FrameLayout {
        //是否展开和收起的标记
        private var mIsShowAll:Boolean = false
        constructor(context: Context) : this(context,null)
        constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)
        constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
        ){
            View.inflate(context, R.layout.view_seemore,this)
            initListener()
        }
        private fun initListener() {
            //查看更多的点击事件
            view_seemore_tv_seemore.setOnClickListener {
                if(mIsShowAll){
                    //这是收起的关键代码,将最大行数设置为你想展示的最小行数即可
                    view_seemore_tvcontent.maxLines = 2
                    view_seemore_tv_seemore.text = "查看更多"
                }else{
                    //这是查看更多的关键代码,将最大行数设置一个大数即可
                    view_seemore_tvcontent.maxLines = 20
                    view_seemore_tv_seemore.text = "收起"
                }
                mIsShowAll = !mIsShowAll
            }
            //attachedToWindow之后执行操作
            post {
                //这里必须这样写,这是在attachedToWindow之后执行操作,否则获取行数会出问题
                Log.e("测试","OnLayout${view_seemore_tvlinecount.lineCount}")
                if(view_seemore_tvlinecount.lineCount>2){
                    view_seemore_tv_seemore.visibility = View.VISIBLE
                }else{
                    view_seemore_tv_seemore.visibility = View.GONE
                }
            }
    
        }
        /**
         * 设置文字
         */
        fun setText(text:String){
            //每次设置文字后都要进行重置
            view_seemore_tvcontent.text = text
            view_seemore_tvlinecount.text = text
            view_seemore_tv_seemore.text = "查看更多"
            view_seemore_tvcontent.maxLines = 2
            mIsShowAll = false
            if(view_seemore_tvlinecount.lineCount>2){
                view_seemore_tv_seemore.visibility = View.VISIBLE
            }else{
                view_seemore_tv_seemore.visibility = View.GONE
            }
        }
    }
    
    

    项目源码

    https://github.com/myml666/SeeMore

    相关文章

      网友评论

        本文标题:Android超简单实现文字展开收起效果

        本文链接:https://www.haomeiwen.com/subject/zqomvhtx.html