標籤:九宮格 android gridview adapter
參考部落格:http://blog.csdn.net/xyz_lmn/article/details/6906255
總結:
1、GridView(網格視圖)按照行列來顯示內容,每個網格可以用已有的布局或自訂的布局來填充,並且GridView每行可以顯示多個網格,即有列數之說。
2、GridView需要結合適配器(Adapter)一起使用,使用GridView類的執行個體對象中的setAdapter方法初始化網格視圖,即gridView.setAdapter(myAdapter)。
3、將GridView中的每一個網格需要是資料打包成一個集合,將此集合作為參數來初始化適配器(Adapter)執行個體對象,在適配器(Adapter)的getView方法中來擷取集合中的資料,將此資料來初始化對應網格中的控制項。
4、適配器(Adapter)中的getView方法在設定適配器和更新適配器資料時被調用,即gridView.setAdapter(myAdapter)和myAdapter.notifyDataSetChanged()時被調用
5、要實現網格之間隔離開來的,首先需要設定用於填充網格的布局背景與網格所在的布局的背景顏色不一致,即mylayout.xml布局的背景與activity_main.xml布局的背景色不一致,而且還需要設定設定網格與網格之間的間距,這樣看起來網格才是分離的。
5、若要實現被選中(點擊)的網格呈現不同的顏色,就需要知道被點擊的網格的Item號,並調用myAdapter.notifyDataSetChanged()去更新適配器資料,最後在適配器中的getView方法中來判斷網格的Item號,如果相等就設定自己所需呈現的背景色(指的是視圖的背景色)。
6、Android實現全螢幕顯示方法,我們都知道在Android中某些功能的實現往往有兩種方法:一種是在xml檔案中設定相應的屬性,另一種是用代碼實現。同樣Android實現全屏也可以通過以下兩種方法實現
(1)、在AndroidManifest.xml的設定檔裡的<activity>標籤添加屬性
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
(2)、在Activity的onCreate()方法中的super()和setContentView()兩個方法之間加入下面兩條語句
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉標題列this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉資訊列
7、GridView的一些特殊屬性
android:numColumns=”auto_fit” //GridView的列數設定為自動
android:columnWidth=”90dp " //每列的寬度,也就是Item的寬度
android:stretchMode=”columnWidth"//縮放與列寬大小同步
android:verticalSpacing=”10dp” //兩行之間的邊距
android:horizontalSpacing=”10dp” //兩列之間的邊距
android:cacheColorHint="#00000000" //去除拖動時預設的黑色背景
android:listSelector="#00000000" //去除選中時的黃色底色
android:scrollbars="none" //隱藏GridView的捲軸
android:fadeScrollbars="true" //設定為true就可以實現捲軸的自動隱藏和顯示
android:fastScrollEnabled="true" //GridView出現快速滾動的按鈕(至少滾動4頁才會顯示)
android:fadingEdge="none" //GridView衰落(褪去)邊緣顏色為空白,預設值是vertical。(可以理解為上下邊緣的提示色)
android:fadingEdgeLength="10dip" //定義的衰落(褪去)邊緣的長度
android:stackFromBottom="true" //設定為true時,你做好的列表就會顯示你列表的最下面
android:transcriptMode="alwaysScroll" //當你動態添加資料時,列表將自動往下滾動最新的條目可以自動滾動到可視範圍內
android:drawSelectorOnTop="false" //點擊某條記錄不放,顏色會在記錄的後面成為背景色,內容的文字可見(預設為false)
AndroidManifest.xml——沒有做任何修改,建立工程預設的
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wxl.ninebox" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.wxl.ninebox.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> </application></manifest>
mylayout.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fffccc"android:id="@+id/mylayout" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" android:layout_gravity="center_horizontal" android:background="@drawable/ic_launcher"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginBottom="5dp" android:textColor="#000000" android:textSize="12sp"/></FrameLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context=".MainActivity" > <GridView android:id="@+id/gridView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="3" android:verticalSpacing="30dip" android:horizontalSpacing="10dip" android:layout_gravity="center" > </GridView></LinearLayout>
MainActivity.java
package com.wxl.ninebox;import java.util.ArrayList;import java.util.HashMap;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.TextView;import android.app.Activity;import android.content.Context;import android.graphics.Color;public class MainActivity extends Activity {GridView gridView;View view;MyAdapter myAdapter;ArrayList<HashMap<String, Object>> arrayList;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); gridView = (GridView)this.findViewById(R.id.gridView); arrayList = new ArrayList<HashMap<String,Object>>(); for (int i = 1; i < 10; i++) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("image", R.drawable.ic_launcher); hashMap.put("text", "九宮格"+i); arrayList.add(hashMap); } myAdapter = new MyAdapter(arrayList, this); gridView.setAdapter(myAdapter); gridView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubmyAdapter.setSelection(arg2);myAdapter.notifyDataSetChanged();}}); } public class MyAdapter extends BaseAdapter { ArrayList<HashMap<String, Object>> arrayList; Context context; HashMap<String, Object> hashMap; int selectItem = -1; public MyAdapter(ArrayList<HashMap<String, Object>> arrayList,Context context) {// TODO Auto-generated constructor stub this.arrayList = arrayList; this.context = context;} public void setSelection(int position) { selectItem = position; } @Overridepublic int getCount() {// TODO Auto-generated method stubif (null == arrayList){return 0;}else{return arrayList.size();}}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn arrayList.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@SuppressWarnings("unchecked")@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubview = LayoutInflater.from(context).inflate(R.layout.mylayout, arg2,false);ImageView imageView = (ImageView)view.findViewById(R.id.imageView);TextView textView = (TextView)view.findViewById(R.id.textView);hashMap = (HashMap<String, Object>) getItem(arg0);imageView.setImageResource((Integer) hashMap.get("image"));textView.setText((CharSequence) hashMap.get("text"));if (selectItem == arg0) {view.setBackgroundColor(Color.GREEN);}return view;}//設定適配器或更新適配器調用 } }
Android UI編程(1)——九宮格(GridView)