Android的GridView控件 (二)

来源:互联网 发布:mac 彻底删除软件 编辑:程序博客网 时间:2024/05/23 16:28


http://blog.csdn.net/lhp1331/article/details/7909418

GridView控件是可以用来显示二维排列的控件,这里在上一篇TabHost控件的基础上添加了一个GridView控件,用作Tab页的显示内容。

效果图:


帖代码:

[java] view plaincopy
  1. public class OrderClientActivity extends Activity {  
  2.     TabHost tabHost;  
  3.     String[] dishType=new String[]{"1","2","3","4","5","6","7"};  
  4.     GridView gridView;  
  5.       
  6.     String[] dishName=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12"};  
  7.     String picPath=Environment.getExternalStorageDirectory().getPath() + "/picture/";  
  8.       
  9.     GridViewAdapter gridViewAdapter;  
  10.       
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.orderform);  
  16.           
  17.         // 获取TabHost对象  
  18.         TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
  19.         // 如果没有继承TabActivity时,通过该种方法加载启动tabHost  
  20.         tabHost.setup();  
  21.           
  22.         gridView=(GridView)findViewById(R.id.view1);  
  23.         gridViewAdapter=new GridViewAdapter(this);  
  24.         gridView.setAdapter(gridViewAdapter);  
  25.           
  26.         for(int i=0;i<dishType.length;i++){  
  27.             TabHost.TabSpec tabSpec=tabHost.newTabSpec(dishType[i]);  
  28.             tabSpec.setIndicator(dishType[i],getResources().getDrawable(R.drawable.ic_launcher));  
  29.             tabSpec.setContent(R.id.view1);  
  30.             tabHost.addTab(tabSpec);  
  31.         }  
  32.           
  33.         tabHost.setCurrentTab(1);  
  34.         tabHost.setCurrentTab(0);  
  35.           
  36.         new AsyncLoadImage().execute(100);  
  37.     }  
  38.       
  39.     class AsyncLoadImage extends AsyncTask<Object, Bitmap, Object>{  
  40.   
  41.         @Override  
  42.         protected Object doInBackground(Object... params) {  
  43.             Bitmap bitmap;  
  44.               
  45.             Log.i("加载线程""");  
  46.               
  47.             // TODO Auto-generated method stub  
  48.             for(int i=0;i<dishName.length;i++){  
  49.                 Log.i("图片路径", picPath + dishName[i] + ".jpg");  
  50.                 bitmap=BitmapFactory.decodeFile(picPath + dishName[i] + ".jpg");  
  51.                   
  52.                 if(bitmap != null){  
  53.                     publishProgress(bitmap);  
  54.                     Log.i("图片解析""正确");  
  55.                 }  
  56.                 else  
  57.                     Log.i("图片解析""错误");  
  58.             }  
  59.             return null;  
  60.         }  
  61.   
  62.         @Override  
  63.         protected void onProgressUpdate(Bitmap... values) {  
  64.             // TODO Auto-generated method stub  
  65.             for(Bitmap b : values){  
  66.                 gridViewAdapter.addPic(b);  
  67.                 gridViewAdapter.notifyDataSetChanged();  
  68.             }  
  69.         }  
  70.       
  71.           
  72.     }  
  73.     class GridViewAdapter extends BaseAdapter{  
  74.   
  75.         Context context;  
  76.         private ArrayList<Bitmap> picList=new ArrayList<Bitmap>();  
  77.           
  78.         GridViewAdapter(Context context){  
  79.             this.context=context;  
  80.         }  
  81.         @Override  
  82.         public int getCount() {  
  83.             // TODO Auto-generated method stub  
  84.             return picList.size();  
  85.         }  
  86.   
  87.         @Override  
  88.         public Object getItem(int position) {  
  89.             // TODO Auto-generated method stub  
  90.             return picList.get(position);  
  91.         }  
  92.   
  93.         @Override  
  94.         public long getItemId(int position) {  
  95.             // TODO Auto-generated method stub  
  96.             return position;  
  97.         }  
  98.   
  99.         public void addPic(Bitmap bitmap){  
  100.             picList.add(bitmap);  
  101.         }  
  102.           
  103.         @Override  
  104.         public View getView(int position, View convertView, ViewGroup parent) {  
  105.             // TODO Auto-generated method stub  
  106.             Log.i("得到view", position + "");  
  107.               
  108.             ImageView image = null;  
  109.             if(convertView == null){  
  110.                 image=new ImageView(context);  
  111.                   
  112.             }  
  113.             else{  
  114.                 image=(ImageView)convertView;  
  115.             }  
  116.             image.setLayoutParams(new GridView.LayoutParams((getWindowManager().getDefaultDisplay().getWidth()-3)/2,(int) (getWindowManager().getDefaultDisplay().getWidth()/2*0.6)));//设置ImageView对象布局   
  117.             image.setAdjustViewBounds(true);//设置边界对齐   
  118.             image.setScaleType(ImageView.ScaleType.FIT_XY);//设置刻度的类型   
  119.             image.setPadding(0000);//设置间距   
  120.             image.setImageBitmap(picList.get(position));  
  121.             return image;  
  122.         }  
  123.           
  124.     }  
  125. }  
GridView需要通过Adapter来加载需要显示的内容,在这里我们自定义了BaseAdapter,必须实现几个方法,其中getCount()和getView()是必须实现的方法,getView()用来提供GridView显示的每一个Item。关于Adapter的详细情况可以参考 Android之Adapter用法总结

另外,实际的运行过程中,发现Gridview的图片加载很卡,所以我们改成了异步加载图片,用到了AsyncTask类,这是Android提供的一个轻量级的线程既能在后台线程中执行一些耗时操作,又有方法直接更新ui,关于AsyncTask的详细介绍可以参考:android AsyncTask介绍。

这里如果没有必要用到异步加载,可以在GridViewAdapter里直接给picList赋值。


布局文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:id="@+id/hometabs"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"    
  6.     android:layout_height="fill_parent">  
  7.     <!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->   
  8.     <TabHost android:id="@+id/tabhost"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="1">  
  12.         <LinearLayout  
  13.             android:orientation="vertical"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent">  
  16.             <HorizontalScrollView  
  17.                 android:layout_width="fill_parent"  
  18.                 android:layout_height="wrap_content"  
  19.                 android:scrollbars="none">  
  20.                 <!-- TabWidget的id属性必须为 @android:id/tabs-->              
  21.                 <TabWidget android:id="@android:id/tabs"   
  22.                 android:orientation="horizontal"  
  23.                 android:layout_width="fill_parent"  
  24.                 android:layout_height="wrap_content">  
  25.                 </TabWidget>  
  26.             </HorizontalScrollView>  
  27.               
  28.             <!-- FrameLayout的id属性必须为 @android:id/tabcontent-->  
  29.              <FrameLayout   
  30.                   android:id="@android:id/tabcontent"  
  31.                   android:layout_width="fill_parent"  
  32.                   android:layout_height="fill_parent">  
  33.                     
  34.                   <GridView   
  35.                       android:id="@+id/view1"  
  36.                       android:listSelector="#000000"  
  37.                       android:layout_width="fill_parent"  
  38.                       android:layout_height="wrap_content"  
  39.                       android:padding="0dip"  
  40.                       android:layout_margin="0dip"  
  41.                       android:numColumns="2"  
  42.                       android:horizontalSpacing="3dip"  
  43.                       android:verticalSpacing="3dip"  
  44.                       android:stretchMode="columnWidth"  
  45.                       android:gravity="fill"/>"  
  46.                       
  47.              </FrameLayout>  
  48.            
  49.          </LinearLayout>  
  50.     </TabHost>  
  51.     <LinearLayout   
  52.         android:orientation="horizontal"  
  53.         android:layout_width="fill_parent"  
  54.         android:layout_height="wrap_content">  
  55.         <Button  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:text="hha"/>  
  59.         <Button  
  60.         android:layout_width="wrap_content"  
  61.         android:layout_height="wrap_content"  
  62.         android:text="hha2"/>  
  63.         <Button  
  64.         android:layout_width="wrap_content"  
  65.         android:layout_height="wrap_content"  
  66.         android:text="hha3"/>  
  67.     </LinearLayout>  
  68.       
  69. </LinearLayout>  

在布局文件里看到,GridView设置了一个属性android:listSelector,这个属性用来设置GridView的每个item 的背景。如果没有设置这个属性,每个item在点击的时候都有一个蓝色边框,我们可以设置成我们想要的颜色,不想显示的话可以直接设置成背景色。
原创粉丝点击