如何使用Android RecyclerView创建自动调整网格布局 [英] How to create auto adjust grid layout using android RecyclerView

查看:420
本文介绍了如何使用Android RecyclerView创建自动调整网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RecyclerView创建自动调整列,如上面的图像网格布局.

I want to create auto adjust columns like above image Grid Layout using RecyclerView.

推荐答案

最后,我已经使用FlexboxLayout完成了

At last, I have done it using FlexboxLayout

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

///Gradle import/// compile '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

/////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//////

///////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>

这篇关于如何使用Android RecyclerView创建自动调整网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆