RecyclerView包装内容项

时间:2015-12-12 21:24:52

标签: android android-layout android-recyclerview staggeredgridlayout android-wrap-content

我需要实现下一个UI元素:

enter image description here

  • 未知大小的字符串列表(来自服务器调用)
  • 任何项目都应该是包装内容。
  • 如果某个项目不适合行,他将在下一行。
  • 所有列表/网格都居中

我想过将MyFragment fragment = (MyFragment) manager.findFragmentByTag(FRAGMENT_TAG); if (fragment != null) { fragment.passData(data); } RecyclerView

一起使用

但我不知道它是否是正确的方法,任何想法?

2 个答案:

答案 0 :(得分:3)

每行中有不同数量的单元格意味着您必须努力工作才能从回收方法中获得任何价值。因为为了知道第17行中哪些数据,你必须(预)测量0到16行中的所有数据。

取决于您的使用案例。如果列表以一些合理数量的项目为界。使用单个TextView和一些clever use of spans可能是更好的解决方案。只需将所有主题标签收集到一个字符串中,然后使用RoundedBackgroundSpan(请参阅链接)添加彩色背景。然后将整个事物包裹在ScrollView

编辑1:添加了可能的解决方案代码。

public class RoundedBackgroundSpan extends ReplacementSpan {

    int mBackgroundColor;
    int mTextColor;
    float mRoundedCornerRadius;
    float mSidePadding = 10; // play around with these as needed
    float mVerticalPadding = 30; // play around with these as needed

    public RoundedBackgroundSpan(final int backgroundColor, final int textColor, final float roundedCornerRadius)
    {
        mBackgroundColor = backgroundColor;
        mTextColor = textColor;
        mRoundedCornerRadius = roundedCornerRadius;
    }

    @Override
    public int getSize(final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm)
    {
        return Math.round(MeasureText(paint, text, start, end) + (2 * mSidePadding));
    }

    @Override
    public void draw(final Canvas canvas, final CharSequence text, final int start, final int end, final float x, final int top, final int y, final int bottom, final Paint paint)
    {
        // draw the rounded rectangle background
        RectF rect = new RectF(x, -mVerticalPadding + ((bottom + top) / 2) + paint.getFontMetrics().top, x + MeasureText(paint, text, start, end) + (2 * mSidePadding), mVerticalPadding + ((bottom + top) / 2) + paint.getFontMetrics().bottom);
        paint.setColor(mBackgroundColor);
        canvas.drawRoundRect(rect, mRoundedCornerRadius, mRoundedCornerRadius, paint);
        // draw the actual text
        paint.setColor(mTextColor);
        canvas.drawText(text, start, end, x + mSidePadding, ((bottom + top) / 2), paint);
    }

    private float MeasureText(Paint paint, CharSequence text, int start, int end)
    {
        return paint.measureText(text, start, end);
    }

}

以及其他地方(活动/片段最有可能)

    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    for (String hashTag : hashTags)
    {
        stringBuilder.append(hashTag);
        stringBuilder.setSpan(new RoundedBackgroundSpan(getRandomColor(), getResources().getColor(android.R.color.darker_gray), 10), stringBuilder.length() - hashTag.length(), stringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.append(" ");
    }

    textView.setText(stringBuilder);

在xml的某个地方(注意android:lineSpacingMultiplier =“3”和android:gravity =“center”)

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:lineSpacingMultiplier="3"
        android:gravity="center"
        android:padding="10dp"
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</ScrollView>

答案 1 :(得分:3)

我不确定这种方法对您有帮助,而不是

  

将RecyclerView与StaggeredGridLayoutManager一起使用

您可以使用第三方FlowLayout

  1. First implementation (Android flow layout)

    enter image description here enter image description here

  2. Second implementation (Flow layout)

    enter image description here

  3. 检查此要点以获取完整示例:

    https://github.com/davidbeloo/Hashtags

    enter image description here

相关问题