How to make gridview asymmetric?

In my application, I am trying to make my gridview asymmetric, but I can only install with colums 2 or 3 and so on. Another option to make gridview asymmetric? the following is my code for gridview, can anyone help me with this?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F3F3F3"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="150dp"

        />


    <GridView
        android:numColumns="2"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/allproduct_grid"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:layout_marginTop="10dp"
        />

</LinearLayout>

what I want:

enter image description here

+4
source share
3 answers

You do not have a built-in layout or widget to achieve this. So what you are looking for is a StaggeredGridView that allows its elements to have variable sizes.

You can do one of the following : -

1) this , . , , .

2) RecyclerView.StaggeredGridLayoutManager Google.

:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    recyclerView.setAdapter(adapter);

, .

+2

... getView ...

    if (position % 2 == 1) {    
     holder.testbtn.setVisibility(View.VISIBLE);    
     } else {
        holder.testbtn.setVisibility(View.GONE);
     }
0

All Articles