【问题标题】:How to create auto adjust grid layout using android RecyclerView如何使用 android RecyclerView 创建自动调整网格布局
【发布时间】:2023-03-27 18:54:01
【问题描述】:

我想使用 RecyclerView 创建像上图网格布局这样的自动调整列。

【问题讨论】:

  • 看看Flexbox-layout (by Google) 或者你可以尝试使用 RecyclerView 的 StaggeredGridLayoutManager 。
  • 但我认为 StaggeredGridLayoutManager 不适用于这种安排所以你最好使用 Flexbox。谢谢
  • 看这个教程,你可以找到很多关于这个主题的教程inducesmile.com/android/…

标签: android android-recyclerview recyclerview-layout


【解决方案1】:

终于用FlexboxLayout完成了

///Gradle 导入/// 编译'com.google.android:flexbox:0.3.1'

public class MainActivity extends AppCompatActivity {

    List<String> list_text=new ArrayList<>();
    FlexboxLayout container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        container=findViewById(R.id.v_container);
        list_text.add("Avik");
        list_text.add("Rocky bolboa");
        list_text.add("Master");
        list_text.add("avhihsked");
        list_text.add("Prem chopra");
        list_text.add("Rambo");
        list_text.add("Piyali");
        list_text.add("Sheha Singh");
        list_text.add("Sheha Sarker");
        list_text.add("Supriya");
        list_text.add("Manish Singh");
        list_text.add("Chacha ji");
        list_text.add("DEbarun da");
        list_text.add("Asaduk da");
        list_text.add("Sunidhi chauhan");
        list_text.add("Shreya");
        list_text.add("Sourav mitra");

        inflatelayout();

    }

    private void inflatelayout() {
        LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        buttonLayoutParams.setMargins(5,5,5,5);
        for(int i=0;i<list_text.size();i++){
            final TextView tv = new TextView(getApplicationContext());
            tv.setText(list_text.get(i));
            tv.setHeight(100);
            tv.setTextSize(16.0f);
            tv.setGravity(Gravity.CENTER);
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setBackground(getResources().getDrawable(R.drawable.rounded_corner_yellow));
            tv.setId(i + 1);
            tv.setLayoutParams(buttonLayoutParams);
            tv.setTag(i);
            tv.setPadding(20, 10, 20, 10);
            container.addView(tv);
        }

    }
}

/////rounded_corner_yellow.xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent" />

    <solid android:color="@android:color/transparent" />

    <padding
        android:left="10dp"
        android:right="10dp"
        android:top="3dp" />

    <corners android:radius="20dp" />
</shape>

//////activity_main.xml//////

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/v_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    app:alignContent="stretch"
    app:alignItems="stretch"
    app:flexWrap="wrap">
</com.google.android.flexbox.FlexboxLayout>

【讨论】:

  • 嗨,@bhabin 你会得到你需要的东西,但如果你使用我的代码,我会遇到我提到的保证金问题。然后让我知道是否可以解决这个问题
  • 我会用FlexBox你也应该试试
  • 嗨,@Bhavin 我已经找到了解决方案。请检查我的代码是否对您有帮助
  • 如何使用过渡动画来构词?
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 2021-07-05
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 2019-02-17
相关资源
最近更新 更多