Search code examples
androidgridviewdrawerlayout

my Drawerlayout gridview display like list view


I use drawer layout to set the images in grid view but it display as list in every line.i want to display images in grid view.how to do it?can i do it or should something other than Drawer layout? my Drawer layout :

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>

    <GridView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        />
</android.support.v4.widget.DrawerLayout>

the grid view items:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
</LinearLayout>

and the adapter:

public class NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_menu, null);
        }
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());


        return convertView;
    }
}

Solution

  • You need to define the number of columns you need,

            android:numColumns="4" //column amount you need