Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing from a listview to a gridview

I have an activity with a list, whose items are made of an image+text. I need to allow the user to change the view and have a gridview instead of it (whose elements are still made of the same image+text).

The user can do it through an icon menu:

public boolean onOptionsItemSelected(MenuItem item)
{
    if(item.getItemId()== R.id.change_view)
    {
        // ?
    }
}

I tried to just set the new adapter(see below) but it doesn't work..do I have to create a new activity to do that?

if(item.getItemId()== R.id.change_view)
{
    setContentView(R.layout.grid_view);
    gridViewAdapter = new GridViewAdapter(this,R.layout.bookmark_list_item,MyApp.getItems().findAll());
    list.setAdapter(gridViewAdapter);
    list.setVisibility(View.VISIBLE);
}
like image 534
SegFault Avatar asked Sep 29 '13 09:09

SegFault


1 Answers

There are several ways you could achieve that.

  1. One solution is to have both the ListView and GridView stacked in a FrameLayout, and when you want to switch between these views, set the visibility GONE to one view and VISIBLE to another, then viceversa.

  2. Put both the ListView and GridView in a ViewFlipper

  3. Or, use a ViewSwitcher

  4. And finally, use just a GridView, but when you want to transition to a list view, set programmatically the number of columns to 1.

like image 114
Andy Res Avatar answered Sep 25 '22 07:09

Andy Res