GridView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridView相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="com.hanqi.testapp2.TestActivity10">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gv_1"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:horizontalSpacing="30dp"></GridView>

</LinearLayout>
 1 package com.hanqi.testapp2;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.BaseAdapter;
 8 import android.widget.GridView;
 9 import android.widget.ImageView;
10 
11 import java.util.ArrayList;
12 import java.util.List;
13 
14 public class TestActivity10 extends AppCompatActivity {
15 
16     GridView gv_1;
17 
18     List<Integer> liv;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_test10);
24 
25         gv_1 = (GridView)findViewById(R.id.gv_1);
26 
27         //1、数据
28         liv = new ArrayList<>();
29         liv.add(R.drawable.f1);
30         liv.add(R.drawable.f2);
31         liv.add(R.drawable.f3);
32         liv.add(R.drawable.f4);
33         liv.add(R.drawable.f5);
34         liv.add(R.drawable.f6);
35         liv.add(R.drawable.f7);
36         liv.add(R.drawable.f8);
37 
38         gv_1.setAdapter(new ImageAdapter());
39     }
40 
41     //适配器
42     class ImageAdapter extends BaseAdapter
43     {
44         @Override
45         public int getCount() {
46             return liv.size();
47         }
48 
49         @Override
50         public Object getItem(int position) {
51             return liv.get(position);
52         }
53 
54         @Override
55         public long getItemId(int position) {
56             return 0;
57         }
58 
59         @Override
60         public View getView(int position, View convertView, ViewGroup parent) {
61 
62             //1、得到数据  图片的id
63             Integer ivid = liv.get(position);
64 
65             //2、准备View
66             if (convertView == null) {
67                 convertView = new ImageView(TestActivity10.this);
68             }
69 
70             //3、适配
71             //转换
72             ImageView imageView = (ImageView)convertView;
73 
74             imageView.setImageResource(ivid);
75 
76 //            imageView.setMaxWidth(70);
77 //            imageView.setMaxHeight(70);
78 
79             imageView.setLayoutParams(new GridView.LayoutParams(110,110));
80 
81             return imageView;
82         }
83     }
84 }

技术分享

以上是关于GridView的主要内容,如果未能解决你的问题,请参考以下文章

GridView - 片段 - LongClick

片段中的网格视图

如何在Android中加载带有动画的cardview GridView?

在片段内使用 GridView 时应用程序崩溃

滚动包含 x 个 GridView 的整个片段

如何在选项卡片段内调用gridView适配器