android GridView的學習

來源:互聯網
上載者:User

 

android GridView的學習

 

1 :簡單的原始的GridView布局 : http://blog.csdn.net/jiabinjlu/article/details/6921008

           在該例子中,使用的是simpleAdapter來串連資料來源和GridView

      

 

2:通過重寫Adapter ,對GridView的布局做一些改動 :http://jackxlee.blog.51cto.com/2493058/674409

    在該例子中,使用的是自訂的Adapter來串連資料來源和GridView

 

package com.example.gridviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter{

 private Context context;
 
 public ImageAdapter(Context context){
  this.context = context;
 }
 
 private Integer[] images ={
   R.drawable.get1,
   R.drawable.get2,
   R.drawable.get3,
   R.drawable.get4,
   R.drawable.get5,
   R.drawable.get6
 };
 
 private String[] texts ={
   "音樂",
   "天空",
   "文檔",
   "晶片",
   "音響",
   "音響"
 };
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return images.length;
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  
  ImgTextWrapper wrapper;
  
  if(convertView == null){
   
   wrapper = new ImgTextWrapper();
   LayoutInflater inflater = LayoutInflater.from(context);
   //獲得對應的每一項的布局格式
   convertView = inflater.inflate(R.layout.gridview_item, null);
      //設定一個對象
   convertView.setTag(wrapper);
   //設定邊距
   convertView.setPadding(15, 15, 15, 15);
  }else {
   wrapper = (ImgTextWrapper) convertView.getTag();
  }
  
  wrapper.imageView = (ImageView) convertView.findViewById(R.id.imageView_item);
  //在對應的位元影像上,設定對應的圖片為其背景
  wrapper.imageView.setBackgroundResource(images[position]);
  wrapper.textView = (TextView) convertView.findViewById(R.id.textView_item);
  wrapper.textView.setText(texts[position]);
  
  return convertView;
 }

 //該類封裝了圖片和文字
 class ImgTextWrapper {
  ImageView imageView;
  TextView textView;
 }
 
 
}

 

然後在主程式中:

 

//找到容器
     gridView = (GridView) findViewById(R.id.gridView1);

  //ImageAdapter為自訂的Adapter,
  ImageAdapter adapter = new ImageAdapter(this); 

   //通過適配器串連資料來源和容器
  gridView.setAdapter(adapter);

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.