Pagine

Saturday, 6 December 2014

Android: Pass from ListView to GridView at runtime

In this tutorial we explain how to build a view in Android that allows to display some items as a list or a grid, and pass from a view to the other one fastly.

The application's default view is the listview.

Below there's the implementation of the MainActivity.java


The layout of the activity uses two ViewStud, one for the list and one for the grid, to load at runtime the desired layout. The code is contained in activity_main.xml:


There have been defined two different layouts for the list and the grid, and their corresponding items.
For simplicity, we show only the files related to the list, as the ones related to the grid are similar to them.

For the layout of the list, the relative ViewStub refers to the file list_view.xml:


The layout for the single item of the list is defined in the file component_list.xml:


The adapter is defined in the file ListViewAdapter.java:


It is possible to download the complete example from our repository: ChangeView.zip




The final result is the following one:



2 comments:

  1. Thanks a lot.....after trying a lot for various days I just got the solutions ....You just gave me a mental sanity.I will never forget this coder....

    ReplyDelete