1

I'm developing an Android app and in one screen layout I need to have a gridview with header, but there isn't any header for gridview. So I saw in instagram profile that they use listview like a gridview, but I couldn't find anything about it, so if somebody knows an alternative way to do this kind of thing, I would appreciate.

I want something like this: enter image description here

Thanks in advance

2 Answers 2

1

Try following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   <GridView
       android:id="@+id/gridViewCustom"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView1"
       android:layout_alignParentRight="true"
       android:layout_margin="4dp"
       android:columnWidth="80dp"
       android:gravity="center"
       android:numColumns="auto_fit"
       android:stretchMode="columnWidth" >
   </GridView>

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:text="THIS IS THE HEADER"
       android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

And in your Activity:

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

        final TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setVisibility(View.VISIBLE);

        GridView grid = (GridView) findViewById(R.id.gridViewCustom);
        grid.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if(firstVisibleItem == 0)//or it is maybe 1 I'm not sure. It means you are at the top of the gridview
                    txt.setVisibility(View.VISIBLE);
                else
                    txt.setVisibility(View.GONE);
            }
        });
    }
2
  • This doesn't work for what I need, I need the header and listview scroll together. Thanks for your try
    – jackcar
    Sep 11, 2014 at 13:54
  • I don't want to disappear with the header, only scroll it
    – jackcar
    Sep 11, 2014 at 14:30
0

I actually found a solution, it's make a list with an adapter with multiple items, so we can make a "gridview" with a list.

Hope this help somebody in the future.

In the link bellow you can download from git https://github.com/umano/MultiItemRowListAdapter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.