简体   繁体   English

如何使用Android RecyclerView创建自动调整网格布局

[英]How to create auto adjust grid layout using android RecyclerView

如何实现这种网格布局

I want to create auto adjust columns like above image Grid Layout using RecyclerView. 我想使用RecyclerView创建自动调整列,如上面的图像网格布局。

在此处输入图片说明 At last, I have done it using FlexboxLayout 最后,我使用FlexboxLayout完成了

///Gradle import/// compile 'com.google.android:flexbox:0.3.1' /// Gradle import ////编译'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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM