Gridview简单使用

来源:互联网 发布:扩展欧几里德算法用途 编辑:程序博客网 时间:2024/06/14 12:46

XML布局文件

<1、主布局>

<?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:orientation="vertical">    <GridView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/gridview"        android:numColumns="4"        android:verticalSpacing="10dp"        android:horizontalSpacing="10dp"/></LinearLayout>


<2、item布局>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ImageView        android:layout_width="70dp"        android:layout_height="70dp"        android:id="@+id/image"/>    <TextView        android:id="@+id/tv_show"        android:layout_width="70dp"        android:layout_height="wrap_content"        android:text="图片展示"        android:gravity="center"/></LinearLayout>


/3、适配器>

public class MyAdapter extends BaseAdapter {    private int image[];    private Context context;    public MyAdapter(int[] image, Context context) {        this.image = image;        this.context = context;    }    @Override    public int getCount() {        return image.length;    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        MyHolder myHolder;        if(convertView==null){            myHolder=new MyHolder();            convertView= LayoutInflater.from(context).inflate(R.layout.gridview_item,null);            myHolder.imageView= (ImageView) convertView.findViewById(R.id.image);            myHolder.textView= (TextView) convertView.findViewById(R.id.tv_show);            convertView.setTag(myHolder);        }else {            myHolder= (MyHolder) convertView.getTag();        }        myHolder.imageView.setImageResource(image[position]);        return convertView;    }    private class MyHolder{        ImageView imageView;        TextView textView;    }}

<4、主代码>      (这里放的都是一样的图片,你也可以自己选择资源ID)

public class MainActivity extends AppCompatActivity {    private GridView gridView;    private MyAdapter myAdapter;    private int image[]={R.drawable.checkbox_true,R.drawable.checkbox_true,R.drawable.checkbox_true,            R.drawable.checkbox_true,R.drawable.checkbox_true,R.drawable.checkbox_true,R.drawable.checkbox_true,            R.drawable.checkbox_true,R.drawable.checkbox_true,R.drawable.checkbox_true};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gridView= (GridView) findViewById(R.id.gridview);        myAdapter=new MyAdapter(image,this);        gridView.setAdapter(myAdapter);    }}


1 0
原创粉丝点击