Android:GridView

简介:

android:stretchMode="columnWidth"

<!--如何填满空余的位置,模拟器采用WVGA800*480,每排4列,有4*100+5*3=415,还余65px的空间,如果是columnWidth,则这剩余的65将分摊给4列,每列增加16/17px。如果采用SpacingWidth,则分摊给3个间隔空隙 -->



1
2
3
4
5
6
7
8
9
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
public  class  MainActivity  extends  Activity
{
     /**
      * GridView的xml:
      * 设置垂直和水平方向的item间隔
      * android:verticalSpacing="10dp"
      * android:horizontalSpacing="10dp"
      *
      * android:numColumns="auto_fit" //列数
      * android:columnWidth="60dp" //列宽
      * android:stretchMode="columnWidth" //列数设置依据
      */
                                                         
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
                                                             
         GridView gridView = (GridView) findViewById(R.id.gridView1);
         gridView.setAdapter( new  BaseAdapter()
         {
                                                                 
             @Override
             public  View getView( int  position, View convertView, ViewGroup parent)
             {
                 ImageView imageView;
                 if  (convertView ==  null )
                 {
                     //用代码创建ImageView
//                    imageView = new ImageView(mContext);
//                    imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
//                    imageView.setAdjustViewBounds(false);
//                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                    imageView.setPadding(8, 8, 8, 8);
                                                                         
                     LayoutInflater inflater = getLayoutInflater();
                     imageView = (ImageView) inflater.inflate(R.layout.imageview,  null );
                 }
                 else
                 {
                     imageView = (ImageView) convertView;
                 }
                                                                     
                 imageView.setImageResource(mThumbIds[position]);
                 return  imageView;
             }
                                                                 
             @Override
             public  long  getItemId( int  position)
             {
                 return  0 ;
             }
                                                                 
             @Override
             public  Object getItem( int  position)
             {
                 return  null ;
             }
                                                                 
             @Override
             public  int  getCount()
             {
                 return  mThumbIds.length;
             }
         });
                                                             
     }
                                                         
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu)
     {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return  true ;
     }
                                                         
     private  Integer[] mThumbIds =
     { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7, };
                                                         
}



系统范例:

1
2
3
4
5
6
7
8
9
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public  class  Grid2  extends  Activity {
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.grid_2);
         GridView g = (GridView) findViewById(R.id.myGrid);
         g.setAdapter( new  ImageAdapter( this ));
     }
     public  class  ImageAdapter  extends  BaseAdapter {
         public  ImageAdapter(Context c) {
             mContext = c;
         }
         public  int  getCount() {
             return  mThumbIds.length;
         }
         public  Object getItem( int  position) {
             return  position;
         }
         public  long  getItemId( int  position) {
             return  position;
         }
         public  View getView( int  position, View convertView, ViewGroup parent) {
             ImageView imageView;
             if  (convertView ==  null ) {
                 imageView =  new  ImageView(mContext);
                 imageView.setLayoutParams( new  GridView.LayoutParams( 45 45 ));
                 imageView.setAdjustViewBounds( false );
                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                 imageView.setPadding( 8 8 8 8 );
             else  {
                 imageView = (ImageView) convertView;
             }
             imageView.setImageResource(mThumbIds[position]);
             return  imageView;
         }
         private  Context mContext;
         private  Integer[] mThumbIds = {
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
                 R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7,
         };
     }
}



其他笔记:

1.滚动gridview:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 滚动品牌栏
private  void  smoothNextBrand()
{
     float  padding = getResources().getDimension(R.dimen.grid_brand_vspace);
     int  offset = Utils_Math.dip2px( this , padding);
                 
     int  h = mItemH_brand + offset; // +
                                   // mGridView_brand.getVerticalSpacing();//此方法api必须大于16
     mGridView_brand.smoothScrollBy(h,  1000 );
     mBrandAdapter.notifyDataSetChanged();
}
// 滚动模板栏
private  void  smoothNextMode()
{
     final  int  h = mItemH_mode; // + mGridView_mode.getVerticalSpacing();
     mGridView_mode.smoothScrollBy(h,  1000 );
     mModeAdapter.notifyDataSetChanged();
}



2.监听滚动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private  void  initUI()
{
     mGridView_mode.setOnScrollListener( this );
}
@Override
public  void  onScrollStateChanged(AbsListView view,  int  scrollState)
{
     if  (scrollState == OnScrollListener.SCROLL_STATE_IDLE)
     {
               
     }
}
@Override
public  void  onScroll(AbsListView view,  int  firstVisibleItem,  int  visibleItemCount,  int  totalItemCount)
{
}




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1202969,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
Android开发
Android 分享机顶盒项目的封装类《GridView》(二)(转)
Android 分享机顶盒项目的封装类《GridView》(二)(转)
51 2
|
7月前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
77 2
|
7月前
|
前端开发 API Android开发
26. 【Android教程】网格视图 GridView
26. 【Android教程】网格视图 GridView
102 2
|
8月前
|
XML Java Android开发
Android 分享机顶盒项目的封装类《GridView》(三)(转)
Android 分享机顶盒项目的封装类《GridView》(三)(转)
54 2
|
8月前
|
Android开发
Android教程之Android 用户界面-表格视图(GridView)
Android教程之Android 用户界面-表格视图(GridView)
59 1
|
Android开发
解决安卓GridView的横向滚动问题
解决安卓GridView的横向滚动问题
160 0
|
Android开发
安卓GridView的简单使用
安卓GridView的简单使用
179 0
|
Android开发
Android自定义ListView和GridView解决滑动冲突和显示不全
Android自定义ListView和GridView解决滑动冲突和显示不全
176 0
|
Android开发
Android: 解决GridView、ListView在ScrollView中出现冲突的问题
Android: 解决GridView、ListView在ScrollView中出现冲突的问题
331 0