Android 根据String字符串长度判断展开收起并解决RecyclerView中 错位的问题

功能

公司需求有圈子功能,超过三行要显示一个全文按钮 点击展示所有内容,并变为收起,点收起显示三行。

判断是否超过最大行代码

/**
     * 计算文本的长度是否超过最大行
     *
     * @param text
     * @return
     */
    public static boolean isGTmaxLines( String text,Context context) {
        int MAX_LINES=3;
        int lines=0;
        // 获得字体的宽度,sp转px
        int txtWidth = DisplayUtils.sp2px(context, 14);
        // 获得屏幕的宽度 30为margin+padding总和
        int viewWidth = ConfigYibaisong.window_x-DisplayUtils.dip2px(context,30);
        // 获得单行最多显示字数
        int maxWords = viewWidth / txtWidth;
        // 计算字符串长度,
        int stringLen = text.length();
        if(text.contains("\n")){
            String strs[] = text.split("\n");
            int str_lines;
            for (int i=0;i<strs.length;i++){
                str_lines =strs[i].length() / maxWords;
                if(strs[i].length() % maxWords > 0){
                    str_lines++;
                }
                lines += str_lines;
            }
        }else {
            lines = stringLen / maxWords;// 字符串长度除以单行最多显示字数为行数
        }
        if (lines > MAX_LINES) {
            // 如果大于指定行数,则直接返回
            return true;
        } else if (lines == MAX_LINES) {
            // 否则需要判断下是否等于最大行,但是有余数
            if (stringLen % maxWords > 0) {
                return true;
            }
        }
        return false;
    }

3是行数。自己修改下就行。
注意是判断了/n 要不你懂得 。

解决列表滑动错位问题

就是解决展开跟隐藏错位的问题

初始化数组在构造方法中

 private SparseArray<Boolean> content_array;//记录展开,收缩  true 为展开状态  false收缩状态
     ...
      content_array = new SparseArray<>();

主要逻辑

 final int id = Integer.parseInt(data.getId());//圈子id
            //内容逻辑
            final String content = StringUtils.decodeUtf8Str(data.getContent());
            if(BaisongUtils.isGTmaxLines(content,mContext)){
                holder.circle_content_more.setVisibility(View.VISIBLE);
                if(content_array.get(id)==null){
                    content_array.put(id,false);
                }else if(content_array.get(id)){
                    AppLogMessageMgr.e("Circle","Full-----------"+id+"----"+holder.circle_content_more.toString());
                    holder.circle_content.setMaxLines(10);
                    holder.circle_content_more.setText(AppResourceMgr.getString(mContext,R.string.fold));
                }else {
                    AppLogMessageMgr.e("Circle","Fold-------"+id+"----"+holder.circle_content_more.toString());
                    holder.circle_content.setMaxLines(3);
                    holder.circle_content_more.setText(AppResourceMgr.getString(mContext,R.string.fullText));
                }
            }else {
                holder.circle_content_more.setVisibility(View.GONE);
            }

id是每条博文的唯一标示, 如果没有的话就用position 如果用position的话就需要一个remove数组功能。在下拉刷新前调用。
remove功能

/***
     * 清楚所有记录
     */
    public void  clearContentLog(){
        content_array.clear();
    }

最后是点击展开跟收缩里的方法

@Override
                public void onClick(View v) {
                    if(content_array.get(id)){
                        content_array.put(id,false);
                        holder.circle_content.setMaxLines(3);
                        holder.circle_content_more.setText(AppResourceMgr.getString(mContext,R.string.fullText));
                    }else {
                        holder.circle_content.setMaxLines(10);
                        content_array.put(id,true);
                        holder.circle_content_more.setText(AppResourceMgr.getString(mContext,R.string.fold));
                    }
                }

此致,敬礼,问题解决。

猜你喜欢

转载自blog.csdn.net/a940659387/article/details/79789229