GridView组件的使用

来源:互联网 发布:金融和it 知乎 编辑:程序博客网 时间:2024/06/08 16:20

GridView组件的使用

            手机应用程序列表、九宫格等就可以使用该组件

1.在layout布局文件中(总体布局)创建两个组件GridView:负责展示图片 ImageView:负责显示图片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <GridView        android:id="@+id/imagelist"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:horizontalSpacing="20dp"        android:numColumns="3"        android:verticalSpacing="10dp" >    </GridView>    <ImageView        android:id="@+id/imageshow"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:adjustViewBounds="true"        /></LinearLayout>

2.在layout布局文件(微观布局)中创建grid.xml文件:进行每个小格子的布局。拷贝6张图片到drawable下

<?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/smallImage"        android:layout_width="80dp"        android:layout_height="50dp"/></LinearLayout>

3.在控制器Activity中进行绑定和处理

public class MainActivity extends Activity {private GridView smallImageGrid;private ImageView showImage;private int[] pics;//存放图片id的数组    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //查找组件        smallImageGrid=(GridView) this.findViewById(R.id.imagelist);        showImage=(ImageView) this.findViewById(R.id.imageshow);        //第一步 实例化图片ID的数组        pics=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};        //第二步 创建一个List<Map>集合,用于将图片ID转换为List集合        List<Map<String,Object>> pic_items=new ArrayList<Map<String,Object>>();        for(int pic_id:pics){         Map<String, Object> item=new HashMap<String, Object>();         item.put("pic", pic_id);         pic_items.add(item);        }        //第三步 创建SimpleAdaper适配器,以备与GridView组件进行绑定        SimpleAdapter adapter=new SimpleAdapter(this,pic_items,R.layout.grid,new String[]{"pic"},new int[]{R.id.smallImage});        //第四步  GridView组件与adapter适配器进行绑定        smallImageGrid.setAdapter(adapter);        //第五步 为showImage组件设置默认图片        showImage.setImageResource(pics[0]);        //第六步 注册事件监听        smallImageGrid.setOnItemClickListener(new GridViewHandler());    }            public class GridViewHandler implements OnItemClickListener{    /**     * AdapterView adpaterview:发生点击事件的AdapterView;     * View view:AdapterView中被用户点击的Item(GridView中的Item);     * int positon:被点击的Item在Adapter中的位置     * long id:被点击的Item的Id     */@Overridepublic void onItemClick(AdapterView<?> adapter, View view, int position,long id) {showImage.setImageResource(pics[position]);}        }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    } }



原创粉丝点击