Android studio -简单实现textview的折叠和展开

先上效果图:
展开前
展开后
参考:
Android实现TextView内容可展开收缩功能

网上有很多实现的办法,但是大多都比较麻烦,发现上面那篇文章的方法实现的比较简单,所以进行了尝试

  1. 首先定义变量
    private boolean isExpand;
    private Runnable resumeRunnable;
    private String describtionStr = "内容";
    private TextView content;
  1. 在onCreate函数里添加:
        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. 然后在onCreate函数外添加:
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. 关于如何让其它控件根据展开或收起而变化,即展开后其它控件也跟着下移,只需要使用relativeLayout布局,并将textview和它的父组件修改成wrapcontent就行

猜你喜欢

转载自blog.csdn.net/zzzzzwbetter/article/details/129759443
今日推荐