AdapterView学习

AdapterView是ViewGroup的子类
      可包含多个列表项用于显示一组数据
      AdapterView类中的addView方法被禁用,
        其数据提供通过适配器对象来完成。
     两个直接子类的区别在于核心事件不同


     GridView控件
     GridView与ListView一样都是容器控件
     继承自AbsListView
     用网格的形式显示列表项


     ColumWidth    设置列宽度
     Gravity     设置对齐方式
     horizontalSpacing     设置个元素之间的水平距离
     numColumns   设置列数
     stretchMode 设置拉伸模式
              NO_STRETCH 不拉伸
     STRETCH_SPACING  仅拉伸元素之间的间距
     STRETCH_SPACING_UNIFORM 表格元素本身、元素之间的间距一起拉伸
     STRETCH_COLUMN_WIDTH 仅拉伸表格元素本身

     verticalSpacing      设置个元素间的垂直距离


小案例:

         主界面:

package com.example.gridview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	private GridView gvGoods;
	private SimpleAdapter adapter;
	private void setupView(){
		gvGoods = (GridView)findViewById(R.id.gvGoods);
		
		String[] from = {"title","thumbId"};
		int[] to = {R.id.tvTitle,R.id.ivThumbnail};
		adapter = new SimpleAdapter(this,getData(),R.layout.item,from,to);
	  gvGoods.setAdapter(adapter);
	}
	private List<HashMap<String,Object>> getData(){
		ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
		int[] thumbs = {R.drawable.a,R.drawable.b,R.drawable.c,
				R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i,
				R.drawable.j};
		String[] title = {"one","two","three","four","five"
				,"six","seven","eight","nine"};
		
		for(int i =0 ;i<9;i++){
			HashMap<String, Object> item = new HashMap<String,Object>();
			
			item.put("title", title[i]);
			item.put("thumbId", thumbs[i]);
			data.add(item);
		}
		return data;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setupView();
	}
}

布局:

       

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="120dp"
    android:stretchMode="spacingWidth"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:padding="10dp"
    android:id="@+id/gvGoods"
     xmlns:android="http://schemas.android.com/apk/res/android"
     />


音乐item.xml:

        

<?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:id="@+id/ivThumbnail"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="fitCenter"/>
    <TextView 
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="15sp"/>

</LinearLayout>

你可能感兴趣的:(AdapterView学习)