GridView的使用
public class MainActivity extends Activity { private GridView gridView; private int[] images = {R.drawable.img_item0,R.drawable.img_item1 ,R.drawable.img_item2,R.drawable.img_item3,R.drawable.img_item4, R.drawable.img_item5,R.drawable.img_item6,R.drawable.img_item7, R.drawable.img_item8}; private String[] desc = {"该减肥了","该运动运动了","","","","","","",""}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview); gridView = (GridView) findViewById(R.id.gv); MyAdapter adapter = new MyAdapter(images,desc); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Animation anim = new TranslateAnimation(-2, 2, 0, 0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); anim.setDuration(1000); anim.setRepeatCount(3); anim.setRepeatMode(Animation.REVERSE); ImageView image = (ImageView) view.findViewById(R.id.image); image.setAnimation(anim); anim.start(); } }); } class MyAdapter extends BaseAdapter{ int[] imgItemRes; String[] imgItemDesc; public MyAdapter(int[] imgItemRes,String[] imgItemDesc){ this.imgItemRes = imgItemRes; this.imgItemDesc = imgItemDesc; } @Override public int getCount() { return imgItemRes.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ LayoutInflater inflater = getLayoutInflater(); convertView = inflater.inflate(R.layout.gridviewitem, null); } ImageView image = (ImageView) convertView.findViewById(R.id.image); TextView text = (TextView) convertView.findViewById(R.id.text); image.setImageResource(imgItemRes[position]); text.setText(imgItemDesc[position]); return convertView; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <GridView android:id="@+id/gv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="60dp" android:numColumns="3" android:verticalSpacing="10dp" android:horizontalSpacing="15dp" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout> gridviewitem.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>