Android控件之GridView的使用

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

XML布局文件之activity_main.xml

<?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" />

 

XML布局文件之night_item.xml

<?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>

 

XML之资源文件strings.xml

<?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>

 

XML之整个应用文件AndroidManifest.xml

<?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>

 

JAVA源代码文件:

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;
                }
                
            }        
            
        });
        
    }

}

 

JAVA源代码之跳转的文件:

import android.app.Activity;
import android.os.Bundle;

public class TestActivity1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }  
}

 

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

Android控件之GridView的使用,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。