Android studio - Simple implementation of folding and unfolding of textview

First on the renderings:
before unfolding
after unfolding
Reference:
Android realizes the expandable and shrinkable function of TextView content

There are many ways to implement it on the Internet, but most of them are troublesome. I found that the method in the above article is relatively simple to implement, so I tried it

  1. First define the variable
    private boolean isExpand;
    private Runnable resumeRunnable;
    private String describtionStr = "内容";
    private TextView content;
  1. Add in the onCreate function:
        content = findViewById(R.id.tv_content);
        content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                //让简介折叠
                content.setText(describtionStr);
                resumeRunnable = new LineContent(content, describtionStr);
                content.post(resumeRunnable);
                content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
  1. Then add outside the onCreate function:
private class LineContent implements Runnable {
        private TextView mTarget;  
        private String mContent;

        public LineContent(TextView mTarget, String mContent) {
            this.mTarget = mTarget;
            this.mContent = mContent;
        }

        public void run() {
            if (null != mTarget && !TextUtils.isEmpty(mContent)) {
                GetLineContent();
            }
        }

        private void GetLineContent() {
            // 得到TextView的布局
            Layout layout = mTarget.getLayout();
            // 得到TextView显示有多少行
            int lines = mTarget.getLineCount();
            try {
                if (isExpand) {
                    setGoodAtText(mContent + "\u3000 ", isExpand);
                } else {
                    if (lines > 3) {
              //          String threeLinesContent = new String();
                        StringBuffer threeLinesContent = new StringBuffer();
                        for (int i = 0; i < 3; i++) {
                            threeLinesContent.append(mContent.substring(layout.getLineStart(i),layout.getLineEnd(i)));
                            String sContent= threeLinesContent.substring(0,threeLinesContent.length()-3)+"...\u3000 ";
                            setGoodAtText(sContent, false);
                            mTarget.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (isExpand) {
                                        isExpand = false;
                                    } else {
                                        isExpand = true;
                                    }
                                    mTarget.post(resumeRunnable);
                                }
                            });
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        private void setGoodAtText(String textContent, boolean expand) {
            SpannableString ss = new SpannableString(textContent);
            Drawable drawable;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawable = getResources().getDrawable(expand ? R.drawable.back : R.drawable.delete_pic, null);  //可以通过修改drawable来修改尾部接的图片
            } else {
                drawable = getResources().getDrawable(expand ? R.drawable.back : R.drawable.delete_pic);
            }
            drawable.setBounds(0, 0, 2,2);  
            ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
            ss.setSpan(imageSpan, textContent.length() - 1, textContent.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            mTarget.setText(ss);
        }
    }

    @Override
    protected void onDestroy() {
        if (null != resumeRunnable) {
            resumeRunnable = null;
        }
        super.onDestroy();
    }
  1. About how to make other controls change according to expansion or collapse, that is, other controls will also move down after expansion, just use relativeLayout layout, and modify textview and its parent component to wrapcontent

Guess you like

Origin blog.csdn.net/zzzzzwbetter/article/details/129759443