Android學習筆記24:網格視圖

來源:互聯網
上載者:User

使用GridView可以實現如表格一樣的網格視圖,網格即可以使用文字,也可以使用圖片。

用一個XMl檔案作為Gridview的網格

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:padding="5pt"><ImageViewandroid:id="@+id/image1"android:layout_width="50dp" android:layout_height="50dp" /></LinearLayout>

下面的那個圖片顯示,用了一個ImageSwitcher控制項

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"><!-- 定義一個GridView組件 --><GridView  android:id="@+id/grid01"android:layout_width="fill_parent" android:layout_height="wrap_content" android:horizontalSpacing="pt"android:verticalSpacing="2pt"android:numColumns="4"android:gravity="center"/><!-- 定義一個ImageSwitcher組件 --><ImageSwitcher android:id="@+id/switcher"android:layout_width="320dp"android:layout_height="320dp"android:layout_gravity="center_horizontal"/></LinearLayout>

為GridView設定一個OnItemSelectedListener和OnItemClickedListener監聽器,實現圖片的更換,構造一個SimpleAdapter作為GridView的資料來源

public class GridViewTest extends Activity{int[] imageIds = new int[]{R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7 , R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10, R.drawable.bomb11 , R.drawable.bomb12, R.drawable.bomb13, R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16};@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//建立一個List對象,List對象的元素是MapList<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();for (int i = 0; i < imageIds.length; i++){Map<String, Object> listItem = new HashMap<String, Object>();listItem.put("image" , imageIds[i]);listItems.add(listItem);}//擷取顯示圖片的ImageSwitcherfinal ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.switcher);//設定圖片更換的動畫效果switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));//為ImageSwitcher設定圖片切換的動畫效果switcher.setFactory(new ViewFactory(){@Overridepublic View makeView(){ImageView imageView = new ImageView(GridViewTest.this);imageView.setBackgroundColor(0xff0000);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));return imageView;}});//建立一個SimpleAdapterSimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems //使用/layout/cell.xml檔案作為介面布局, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});GridView grid = (GridView)findViewById(R.id.grid01);//為GridView設定Adaptergrid.setAdapter(simpleAdapter);//添加清單項目被選中的監聽器grid.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position , long id){//顯示當前被選中的圖片switcher.setImageResource(imageIds[position % imageIds.length]);}@Overridepublic void onNothingSelected(AdapterView<?> parent){}});//添加清單項目被單擊的監聽器grid.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id){//顯示被單擊的圖片的圖片switcher.setImageResource(imageIds[position % imageIds.length]);}});}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.