Going from a list to gridview

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

The user can do this through the icon menu:

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

I tried just installing a new adapter (see below), but it does not work. Do I need to create a new action for this?

 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); } 
+7
android listview gridview
source share
2 answers

I finally decided something like this:

For the layout of my activity, I have:

 <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ViewStub android:id="@+id/list" android:inflatedId="@+id/showlayout" android:layout="@layout/list_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"/> <ViewStub android:id="@+id/grid" android:inflatedId="@+id/showlayout" android:layout="@layout/grid_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"/> </merge> 

then I defined the layout for the list and the grid (as well as for my elements) and controlled the passage between them, inflating the layouts, and then in the following way:

 private void changeView() { //if the current view is the listview, passes to gridview if(list_visibile) { listview.setVisibility(View.GONE); gridview.setVisibility(View.VISIBLE); list_visibile = false; setAdapters(); } else { gridview.setVisibility(View.GONE); listview.setVisibility(View.VISIBLE); list_visibile = true; setAdapters(); } } 

full code is available in this article: http://pillsfromtheweb.blogspot.it/2014/12/android-passare-da-listview-gridview.html

0
source share

There are several ways to achieve this.

  • One solution is to put the ListView and GridView in a FrameLayout , and when you want to switch between these views, set the GONE visibility to one view and VISIBLE to another, then vice versa.

  • Put both ListView and GridView in ViewFlipper

  • Or use ViewSwitcher

  • And finally, use only the GridView , but when you want to go to the list view, programmatically set the number of columns to 1.

+11
source share

All Articles