当前位置:操作系统 > 安卓/Android >>

Android GridView中GridAdapter用法详解

说到GridView Listview一定会提到他的adapter,现整理如下:

main.xml布局里面:

[html] <GridView 
    android:id="@+id/grid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="70dp" 
    android:numColumns="auto_fit"  
    android:verticalSpacing="10dp"  
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    /> 
<GridView
 android:id="@+id/grid"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:columnWidth="70dp"
 android:numColumns="auto_fit"
 android:verticalSpacing="10dp"
 android:horizontalSpacing="10dp"
 android:stretchMode="columnWidth"
 android:gravity="center"
 />
gridview_item.xml:

[html] <?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:layout_gravity="center" 
    > 
    <LinearLayout  
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content" 
        android:orientation="vertical" 
        android:gravity="center" 
        android:layout_gravity="center"> 
        <ImageView  
            android:id="@+id/grid_icon" 
            android:layout_height="80dip"  
            android:layout_width="80dip" 
            android:gravity="center" 
            android:layout_gravity="center"  
            > 
        </ImageView> 
        <TextView  
            android:id="@+id/grid_name" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:singleLine="true" 
            android:gravity="center" 
            android:layout_gravity="center" 
            android:textSize="13px" 
            android:layout_marginTop="4dip" 
            > 
        </TextView> 
    </LinearLayout>  
</FrameLayout>   
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:orientation="vertical"
 android:gravity="center"
 android:layout_gravity="center"
 >
 <LinearLayout
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:orientation="vertical"
  android:gravity="center"
  android:layout_gravity="center">
  <ImageView
   android:id="@+id/grid_icon"
   android:layout_height="80dip"
   android:layout_width="80dip"
   android:gravity="center"
   android:layout_gravity="center"
   >
  </ImageView>
  <TextView
   android:id="@+id/grid_name"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:singleLine="true"
   android:gravity="center"
   android:layout_gravity="center"
   android:textSize="13px"
   android:layout_marginTop="4dip"
   >
  </TextView>
 </LinearLayout>
</FrameLayout>  
java 代码:

[java] GridView grid = (GridView) findViewById(R.id.grid); 
grid.setAdapter(new GridAdapter(this, mNameList, mDrawableList)); 
GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new GridAdapter(this, mNameList, mDrawableList));
GridAdapter.java类:

[java] package com.android.test; 
 
import java.util.ArrayList; 
 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class GridAdapter extends BaseAdapter { 
    private ArrayList<String> mNameList = new ArrayList<String>(); 
    private ArrayList<Drawable> mDrawableList = new ArrayList<Drawable>(); 
    private LayoutInflater mInflater; 
    private Context mContext; 
    LinearLayout.LayoutParams params; 
 
    public GridAdapter(Context context, ArrayList<String> nameList, ArrayList<Drawable> drawableLi

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,