码迷,mamicode.com
首页 > 移动开发 > 详细

Android控件之GridView的使用

时间:2014-05-19 16:53:30      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   code   c   

GridView主要是类似表格的形式显示数据。这里主要是模仿别人的Demo来实现了一下。

XML布局文件之activity_main.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />
bubuko.com,布布扣

 

XML布局文件之night_item.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dip" >

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >
    </ImageView>

    <TextView
        android:id="@+id/itemText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/itemImage"
        android:layout_centerHorizontal="true"
        android:text="TextView01" >
    </TextView>

</RelativeLayout>
bubuko.com,布布扣

 

XML之资源文件strings.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, GvActivity!</string>
    <string name="app_name">GridView的使用</string>
    <string name="action_settings">Settings</string>
    <string name="test_name1">跳转到TestActivity1</string>
    <string name="test_name2">跳转到TestActivity2</string>
    <string name="test_name3">跳转到TestActivity3</string>
    <string name="test_name4">跳转到TestActivity4</string>
</resources>
bubuko.com,布布扣

 

XML之整个应用文件AndroidManifest.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gridview"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gridview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TestActivity1" android:label="@string/test_name1"/>
        <activity android:name=".TestActivity2" android:label="@string/test_name2"/>
        <activity android:name=".TestActivity3" android:label="@string/test_name3"/>
        <activity android:name=".TestActivity4" android:label="@string/test_name4"/>
    </application>

</manifest>
bubuko.com,布布扣

 

JAVA源代码文件:

bubuko.com,布布扣
import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private String texts[] = null;
    private int images[] = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            
        images=new int[]{
                  R.drawable.smile1
              , R.drawable.smile2
              , R.drawable.smile3
              , R.drawable.smile4};
        texts = new String[]{ 
                  "宫式布局1"
                , "宫式布局2"
                , "宫式布局3"
                , "宫式布局4"};
        
        GridView gridview= (GridView) this.findViewById(R.id.gridview);
        ArrayList<HashMap<String, Object>> lastImageItem = new ArrayList<HashMap<String, Object>>();
        /**
         * 初始化数据
         */
        for(int i=0; i<4; i++){
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("imageImage", images[i]);
            map.put("imageText", texts[i]);
            lastImageItem.add(map);
        }
        /**
         * lastImageItem 数据源是一个HashMap的集合
         * R.layout.night_item 指定一个布局文件
         * from night_item.xml布局文件中所对应的View的name
         * to night_item.xml布局文件中所对应的View的id
         */
        SimpleAdapter simpleItems = new SimpleAdapter(this
                , lastImageItem
                , R.layout.night_item
                , new String[] { "itemImage", "itemText" }
                , new int[] { R.id.itemImage, R.id.itemText });
        
        gridview.setAdapter(simpleItems);
        
        gridview.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos,
                    long rowid) {
                //首先得到选中的集合
                HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(pos);
                //可以得到view的值
                String itemText = (String) item.get("itemText");
                Object object = item.get("itemImage");
                Toast.makeText(MainActivity.this, itemText, Toast.LENGTH_LONG).show();
                //同时知道所点击的位置,是点击了哪个文件
                switch (images[pos]) {
                case R.drawable.smile1:
                    startActivity(new Intent(MainActivity.this,TestActivity1.class));
                    finish();
                    break;
                case R.drawable.smile2:
                    startActivity(new Intent(MainActivity.this,TestActivity2.class));
                    finish();
                    break;
                case R.drawable.smile3:
                    startActivity(new Intent(MainActivity.this,TestActivity3.class));
                    finish();
                    break;
                case R.drawable.smile4:
                    startActivity(new Intent(MainActivity.this,TestActivity4.class));
                    finish();
                    break;
                }
                
            }        
            
        });
        
    }

}
bubuko.com,布布扣

 

JAVA源代码之跳转的文件:

bubuko.com,布布扣
import android.app.Activity;
import android.os.Bundle;

public class TestActivity1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }  
}
bubuko.com,布布扣

 

运行不起来,不知道哪里错了!

bubuko.com,布布扣

Android控件之GridView的使用,布布扣,bubuko.com

Android控件之GridView的使用

标签:android   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/chenjianxiang/p/3734997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!