How to create auto adjust grid layout using android RecyclerView

杀马特。学长 韩版系。学妹 提交于 2019-12-05 07:21:38

问题


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


回答1:


At last, I have done it using FlexboxLayout

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

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


来源:https://stackoverflow.com/questions/47586835/how-to-create-auto-adjust-grid-layout-using-android-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!