Android - Expandable TextView with Animation

后端 未结 15 1101
小蘑菇
小蘑菇 2020-11-28 18:24

I have a TextView which firstly shows a small portion of a long text.

The user can press a \"see more\" button to expand the TextView and s

15条回答
  •  心在旅途
    2020-11-28 18:48

    You can check my blog post on ExpandableTexTView:

    The idea is, initially the TextView will show a small portion of a long text and when it is clicked, it will show the rest of the text.

    So here is the code that how I solved it.

    package com.rokonoid.widget;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.text.SpannableStringBuilder;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.TextView;
    /**
     * User: Bazlur Rahman Rokon
     * Date: 9/7/13 - 3:33 AM
     */
    public class ExpandableTextView extends TextView {
        private static final int DEFAULT_TRIM_LENGTH = 200;
        private static final String ELLIPSIS = ".....";
    
        private CharSequence originalText;
        private CharSequence trimmedText;
        private BufferType bufferType;
        private boolean trim = true;
        private int trimLength;
    
        public ExpandableTextView(Context context) {
            this(context, null);
        }
    
        public ExpandableTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
            this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
            typedArray.recycle();
    
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    trim = !trim;
                    setText();
                    requestFocusFromTouch();
                }
            });
        }
    
        private void setText() {
            super.setText(getDisplayableText(), bufferType);
        }
    
        private CharSequence getDisplayableText() {
            return trim ? trimmedText : originalText;
        }
    
        @Override
        public void setText(CharSequence text, BufferType type) {
            originalText = text;
            trimmedText = getTrimmedText(text);
            bufferType = type;
            setText();
        }
    
        private CharSequence getTrimmedText(CharSequence text) {
            if (originalText != null && originalText.length() > trimLength) {
                return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
            } else {
                return originalText;
            }
        }
    
        public CharSequence getOriginalText() {
            return originalText;
        }
    
        public void setTrimLength(int trimLength) {
            this.trimLength = trimLength;
            trimmedText = getTrimmedText(originalText);
            setText();
        }
    
        public int getTrimLength() {
            return trimLength;
        }
    }
    

    And add the following line in your attr.xml

    
    
        
            
        
    
    

    Put the following in your main.xml

    
     
    
         
    
     
    

    And test your activity

    package com.rokonoid.widget;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MyActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                    "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae " +
                    "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec " +
                    "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam " +
                    "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel " +
                    "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla " +
                    "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam" +
                    " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante" +
                    " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem " +
                    "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum" +
                    " sed laoreet suscipit. ";
    
            ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum);
            expandableTextView.setText(yourText);
        }
    }
    

    Reference: Android – Expandable TextView

提交回复
热议问题