【问题标题】:Does anyone know how to do flow layout using RecyclerView?有谁知道如何使用 RecyclerView 进行流布局?
【发布时间】:2015-12-01 04:13:14
【问题描述】:

有谁知道如何使用 RecyclerView 进行流布局?

如何动态改变跨度计数?

Answer :

【问题讨论】:

  • 你能详细解释一下吗?
  • 我想要像上图这样的设计,我认为这可以通过 recycler-view 来实现,但不知道该怎么做
  • github.com/hongyangAndroid/FlowLayout 这可能对你有帮助。
  • 谢谢@DhinakaranThennarasu
  • 您找到解决方案了吗?那里的所有库都只是向 ViewGroup 添加视图,不涉及回收。

标签: android textview android-recyclerview flowlayout


【解决方案1】:

最好的解决方案是使用带有 google FlexLayoutManager 的 RecyclerView

// Set layout manager
    val layoutManager = FlexboxLayoutManager(context)
    recyclerview.layoutManager = layoutManager

// Now you can add your normal recyclerview adapter

recyclerview.adapter = MyListAdapter(list)

在您的 build.gradle 文件中添加以下依赖项

 implementation 'com.google.android:flexbox:2.0.1'

这就像一个魅力。

【讨论】:

  • 拯救了我的一天......最简单的解决方案
  • 这应该是公认的答案,赞!
【解决方案2】:

这是完整的例子 使用类似于 List GitHubLibrary TagLayout

custom Library
  • 示例代码:-

mFlowLayout.setAdapter(new TagAdapter<String>(mVals) { @Override public View getView(FlowLayout parent, int position, String s) { TextView tv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(s); return tv; } });

使用以下代码,您可以预先设置您想要的选择:-

mAdapter.setSelectedList(1,3,5,7,8,9);

将显示如下结果:-

【讨论】:

  • 如何添加多段标签?我想动态地做到这一点
【解决方案3】:

您可以使用FlowLayout 并将其作为ScrollView 的子项。 存储库中提供了流程布局的示例。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.wefika.flowlayout.FlowLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|top"
            android:minHeight="50dp">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />
        </com.wefika.flowlayout.FlowLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello world" />

    </LinearLayout>

</ScrollView>

您还可以使用示例中给出的以下方法以编程方式添加或删除视图。

 public void addItem(View view) {

        int color = getResources().getColor(R.color.holo_blue_dark);

        View newView = new View(this);
        newView.setBackgroundColor(color);

        FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(100, 100);
        params.rightMargin = 10;
        newView.setLayoutParams(params);

        mFlowLayout.addView(newView);
    }

    public void removeItem(View view) {

        mFlowLayout.removeView(getLastView());

    }

    public void toggleItem(View view) {

        View last = getLastView();

        if(last.getVisibility() == View.VISIBLE) {
            last.setVisibility(View.GONE);
        } else {
            last.setVisibility(View.VISIBLE);
        }

    }

    private View getLastView() {
        return mFlowLayout.getChildAt(mFlowLayout.getChildCount() - 1);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2016-03-06
    • 1970-01-01
    相关资源
    最近更新 更多