一起学Android之GridView

厌伴老儒烹瓠叶,强随举子踏槐花。这篇文章主要讲述一起学Android之GridView相关的知识,希望能为你提供帮助。
本文以一个简单的小例子,简述android开发中GridView的常见应用,仅供学习分享使用。
概述GiridView是一个表格显示资源的控件,可以在两个可滚动的方向上显示。列表项的资源会通过ListAdapter自动添加到布局控件中GirdView上。
如下图所示:
【一起学Android之GridView】

一起学Android之GridView

文章图片

 
数据绑定步骤如下图所示:
一起学Android之GridView

文章图片

  涉及知识点
  • android:numColumns="auto_fit"  设置GridView中的列数,可以是数字,auto_fit表示系统自动根据屏幕的大小进行适应性显示。
  • android:verticalSpacing="10dp” 用于设置行与行之间的垂直间隔
  • android:horizontalSpacing="10dp" 用于设置列与列之间的水平间隔
  • android:stretchMode="columnWidth" 设置列中元素的拉伸模式
  • setOnItemClickListener 设置列元素的监听事件
示例效果图如下图所示:
一起学Android之GridView

文章图片

核心代码GridView如下:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context="com.hex.demogridview.MainActivity"> 8< GridView 9android:id="@+id/gv_info" 10android:columnWidth="90dp" 11android:verticalSpacing="10dp" 12android:horizontalSpacing="10dp" 13android:numColumns="auto_fit" 14android:stretchMode="columnWidth" 15android:layout_width="match_parent" 16android:layout_height="match_parent"> < /GridView> 17 < /RelativeLayout>

列表项代码(图片和文本)
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical"> 6< ImageView 7android:id="@+id/img_info" 8android:src="https://www.songbingjia.com/android/@drawable/a011" 9android:layout_width="match_parent" 10android:layout_height="wrap_content"/> 11< TextView 12android:id="@+id/tv_info" 13android:text="abc" 14android:textSize="20sp" 15android:textAlignment="center" 16android:textColor="@color/colorAccent" 17android:layout_width="match_parent" 18android:layout_height="wrap_content"/> 19 < /LinearLayout>

后台代码(Adapter类)
1 classMyAdapter extends BaseAdapter { 2private Context mContext; 3 4public MyAdapter(Context context) { 5this.mContext = context; 6} 7 8@Override 9public int getCount() { 10return mList != null ? mList.size() : 0; 11} 12 13@Override 14public Object getItem(int position) { 15return mList.get(position); 16} 17 18@Override 19public long getItemId(int position) { 20return position; 21} 22 23@Override 24public View getView(int position, View convertView, ViewGroup parent) { 25ViewHolder viewHolder; 26if (convertView == null) { 27// if it\'s not recycled, initialize some attributes 28LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); //getLayoutInflater(); 29convertView = inflater.inflate(R.layout.grid_item,null); 30viewHolder=new ViewHolder(); 31viewHolder.img = (ImageView) convertView.findViewById(R.id.img_info); 32viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_info); 33 34convertView.setTag(viewHolder); 35} else { 36viewHolder = (ViewHolder) convertView.getTag(); 37} 38viewHolder.img.setImageResource((int)mList.get(position).get("img")); 39viewHolder.tv.setText((String) mList.get(position).get("txt")); 40return convertView; 41} 42}

后台代码(Activity)
1 public class MainActivity extends AppCompatActivity { 2privateint[] imgs; 3private List< Map< String,Object> > mList; 4privateGridView gvInfo; 5@Override 6protected void onCreate(Bundle savedInstanceState) { 7super.onCreate(savedInstanceState); 8setContentView(R.layout.activity_main); 9gvInfo= (GridView) this.findViewById(R.id.gv_info); 10 11imgs=new int[]{R.drawable.a011,R.drawable.a012,R.drawable.a013,R.drawable.a014,R.drawable.a015,R.drawable.a016,
R.drawable.a017,R.drawable.a018,R.drawable.a019,R.drawable.a0110,R.drawable.a0111}; 12mList=new ArrayList< Map< String, Object> > (); 13for(int i=0; i< 11; i++) { 14Map< String, Object> map = new HashMap< String, Object> (); 15map.put("img", imgs[i]); 16map.put("txt", "第" + i + "个"); 17mList.add(map); 18} 19MyAdapter adapter =new MyAdapter(this); 20gvInfo.setAdapter(adapter); 21gvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() { 22@Override 23public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { 24TextView textView= (TextView) view.findViewById(R.id.tv_info); 25String msg = textView.getText().toString(); 26Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); 27} 28}); 29 30}

备注学习一门新的语言,坚持的源头是兴趣,坚持的前方是希望。

    推荐阅读