Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to create a GridVIew of LinearLayouts?

i need to have a gridview of linearlayouts. Each linearLayout must have a imageview and a relativelayout children with more childrens on it.

I'm searching for tutorials/examples of creating gridviews of LinearLayouts but i can't find nothing.

Someone haves a tutorial or can give me some examples or help to do this?

thanks

like image 611
NullPointerException Avatar asked Jan 18 '23 06:01

NullPointerException


1 Answers

Yes, it's possible and is quite simple indeed. When you use your GridView, provide an Adapter to it. In the adapter's getview method, you can create any view you like and return it. For example, you can inflate a view from XML - and that xml may contain a LinearLayout. Alternatively, you can create a linear layout on the fly in that method and add other components to it.

Have a look at this article on Google: http://developer.android.com/resources/tutorials/views/hello-gridview.html

Update: a small example

In your res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="0dip"
     android:paddingBottom="0dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

    <TextView android:id="@+id/TxtName"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textColor="@android:color/black"
         android:layout_weight="0.2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtPackage"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0.2"
         android:textColor="@android:color/black"
         android:padding="2dp"/>
</LinearLayout>

Then in your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //get the item corresponding to your position

    LinearLayout row = (LinearLayout) (convertView == null
               ? LayoutInflater.from(context).inflate(R.layout.item, parent, false)
               : convertView);
    ((TextView)row.findViewById(R.id.TxtName)).setText("first text");
    ((TextView)row.findViewById(R.id.TxtPackage)).setText("second text");
    return row;
}
like image 94
Aleks G Avatar answered Jan 20 '23 16:01

Aleks G