Passing from a listview to a gridview

前端 未结 2 1660
小蘑菇
小蘑菇 2021-02-02 03:44

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 ma

相关标签:
2条回答
  • 2021-02-02 04:10

    I finally resolved with 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've defined the layout for the list and the grid (and also for their items), and managed the passage between them inflating the layouts and then by this method:

    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();
        }
    }
    

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

    0 讨论(0)
  • 2021-02-02 04:16

    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.

    0 讨论(0)
提交回复
热议问题