Can I create a GridVIew LinearLayouts?

I need to have gridview of linearlayouts. Each linearLayout must have an image and relate children with a large number of children on it.

I am looking for tutorials / examples of creating gridviews LinearLayouts, but I can not find anything.

Does anyone have a tutorial or can give me some examples or help me do this?

thanks

+4
source share
1 answer

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

Take a look at this google article: http://developer.android.com/resources/tutorials/views/hello-gridview.html

Update: a small example

In 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; } 
+8
source

Source: https://habr.com/ru/post/1401032/


All Articles