Привет друзья,
Сегодня я делюсь очень простым полезным постом «Custom Grid View в Android».
Весь код приведен ниже, просто скопируйте и наслаждайтесь. Вы также можете скачать zip-папку с github…
MainActivity.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.manish.customgridview; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.GridView; /** * * @author manish.s * */ public class MainActivity extends Activity { GridView gridView; ArrayList<Item> gridArray = new ArrayList<Item>(); CustomGridViewAdapter customGridAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //set grid view item Bitmap homeIcon = BitmapFactory.decodeResource( this .getResources(), R.drawable.home); Bitmap userIcon = BitmapFactory.decodeResource( this .getResources(), R.drawable.personal); gridArray.add( new Item(homeIcon, "Home" )); gridArray.add( new Item(userIcon, "User" )); gridArray.add( new Item(homeIcon, "House" )); gridArray.add( new Item(userIcon, "Friend" )); gridArray.add( new Item(homeIcon, "Home" )); gridArray.add( new Item(userIcon, "Personal" )); gridArray.add( new Item(homeIcon, "Home" )); gridArray.add( new Item(userIcon, "User" )); gridArray.add( new Item(homeIcon, "Building" )); gridArray.add( new Item(userIcon, "User" )); gridArray.add( new Item(homeIcon, "Home" )); gridArray.add( new Item(userIcon, "xyz" )); gridView = (GridView) findViewById(R.id.gridView1); customGridAdapter = new CustomGridViewAdapter( this , R.layout.row_grid, gridArray); gridView.setAdapter(customGridAdapter); } } |
CustomGridViewAdapter.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package com.manish.customgridview; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; /** * * @author manish.s * */ public class CustomGridViewAdapter extends ArrayAdapter<Item> { Context context; int layoutResourceId; ArrayList<Item> data = new ArrayList<Item>(); public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) { super (context, layoutResourceId, data); this .layoutResourceId = layoutResourceId; this .context = context; this .data = data; } @Override public View getView( int position, View convertView, ViewGroup parent) { View row = convertView; RecordHolder holder = null ; if (row == null ) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false ); holder = new RecordHolder(); holder.txtTitle = (TextView) row.findViewById(R.id.item_text); holder.imageItem = (ImageView) row.findViewById(R.id.item_image); row.setTag(holder); } else { holder = (RecordHolder) row.getTag(); } Item item = data.get(position); holder.txtTitle.setText(item.getTitle()); holder.imageItem.setImageBitmap(item.getImage()); return row; } static class RecordHolder { TextView txtTitle; ImageView imageItem; } } |
activity_main.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" > <GridView android:id= "@+id/gridView1" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_margin= "4dp" android:columnWidth= "80dp" android:gravity= "center" android:numColumns= "auto_fit" android:stretchMode= "columnWidth" > </GridView> </RelativeLayout> |
row_grid.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "vertical" android:padding = "5dp" > < ImageView android:id = "@+id/item_image" android:layout_width = "50dp" android:layout_height = "50dp" android:layout_marginRight = "10dp" android:src = "@drawable/home" > </ ImageView > < TextView android:id = "@+id/item_text" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "5dp" android:textSize = "15sp" > </ TextView > </ LinearLayout > |
AndroidManifest.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? xml version = "1.0" encoding = "utf-8" ?> package = "com.manish.customgridview" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" android:targetSdkVersion = "17" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@style/AppTheme" > < activity android:name = "com.manish.customgridview.MainActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
Скачать почтовый индекс
Благодаря!
Ссылка: Пример просмотра пользовательской сетки в Android | Изображение и текст в GridView в Android | Демонстрация Gridview в Android от нашего партнера по JCG Маниша Шриваставы в блоге Android 4 на вашем сайте .