RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 675205
Accepted
user194625
user194625
Asked:2020-06-05 00:22:10 +0000 UTC2020-06-05 00:22:10 +0000 UTC 2020-06-05 00:22:10 +0000 UTC

如何实现标签列表?

  • 772

我正在尝试制作一个列表 - 标签网格(在我们的社区中,一个接一个的椭圆形,如果没有足够的空间,则移动到另一行),我创建了一个适配器RecyclerView,但我不知道是哪个一个LayoutManager使用,因为它应该View进一步插入,但如果没有足够的空间,则移动到另一行。GridLayoutManager不适合这个,它需要传递固定数量的列。我找到了一些GridAutofutLayoutManager,但它也不适合,因为它需要超过列的宽度。这还不是全部,因为LayoutManager我需要的那个根本不需要任何东西,除了上下文。我超过了屏幕宽度的三分之一,但事实并非如此。

在此处输入图像描述

也许,一般来说,通过Spannable它是可能的吗?标签必须是可点击的。求助,苦恼了半天

recyclerview
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    user194625
    2020-06-05T14:20:32Z2020-06-05T14:20:32Z

    找到图书馆 > https://github.com/nex3z/FlowLayout

    下面是徒劳的:

    在克服了一堆耙子之后,用SpannySpannable库武装起来。

    所以标签TextView

    <TextView
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:id="@+id/marks_tv"
        android:textColor="#000000"
        android:layout_gravity="fill"
        android:lineSpacingExtra="10dp"
        android:layout_marginTop="16dp"/>
    

    编码

    List<String> marks = i.getStringArrayListExtra("marks");
        Spanny span = new Spanny();
        span.append("Метки:", new RoundedBackgroundSpan(ctx, Color.TRANSPARENT, Color.BLACK, category.getTextSize(), 0));
        int spanCount = 0;
        for (String mark : marks){
            String marked = mark.toLowerCase().replace(" ", "-");
            if(!marked.equals(category.getText())){
            span = span.append("   ");
            span = span.append(marked, new RoundedBackgroundSpan(ctx, Color.parseColor("#000000"), Color.parseColor("#ffffff"), category.getTextSize(), 10));
            spanCount++;
        }
        }
        if(spanCount == 0){
            marks_tv.setVisibility(View.GONE);
        } else {
        marks_tv.append(span);
            marks_tv.post(new Runnable(){
                    @Override
                    public void run ()
                    {
                        marks_tv.setMinimumHeight(marks_tv.getHeight() + Utils.dpToPx(ctx, 6));
                    }
        });
    }
    

    风俗RoundedBackgroundSpan

    public class RoundedBackgroundSpan extends ReplacementSpan {
    
    private static final int CORNER_RADIUS = 20;
    
    private float PADDING_X;
    private float PADDING_Y;
    
    private float MAGIC_NUMBER;
    
    private int mBackgroundColor;
    private int mTextColor;
    private float mTextSize;
    
    private Context ctx;
    
    public RoundedBackgroundSpan(Context ctx, int backgroundColor, int textColor, float textSize, int paddingX) {
        this.ctx = ctx;
        mBackgroundColor = backgroundColor;
        mTextColor = textColor;
        mTextSize = textSize;
        PADDING_X = Utils.dpToPx(ctx, paddingX);
        PADDING_Y = Utils.dpToPx(ctx, 2);
        MAGIC_NUMBER = Utils.dpToPx(ctx, 2);
    }
    
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        paint = new Paint(paint);
        paint.setTextSize(mTextSize);
        paint.setColor(mBackgroundColor);
        float textHeightWrapping = Utils.dpToPx(ctx, 2);
        float tagBottom = top + textHeightWrapping + PADDING_Y + mTextSize + PADDING_Y + textHeightWrapping;
        float tagRight = x + getTagWidth(text, start, end, paint);
        RectF rect = new RectF(x, top, tagRight, tagBottom);
        canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
        paint.setColor(mTextColor);
        canvas.drawText(text, start, end, x + PADDING_X, tagBottom - PADDING_Y - textHeightWrapping - MAGIC_NUMBER, paint);
    }
    
    private int getTagWidth(CharSequence text, int start, int end, Paint paint) {
        return Math.round(PADDING_X + paint.measureText(text.subSequence(start, end).toString()) + PADDING_X);
    }
    
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        paint = new Paint(paint); 
        paint.setTextSize(mTextSize);
        return getTagWidth(text, start, end, paint);
    }
    }
    

    如果有什么不清楚的,写 - 我会回答。

    截图

    在此处输入图像描述

    在此处输入图像描述

    • 0

相关问题

  • 如何从 RecyclerView 中的按钮导航到新 Activity,并传递项目的位置

  • 键盘崩溃进入 RecyclerView

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5