http://blog.csdn.net/lhp1331/article/details/7909418
GridView控件是可以用来显示二维排列的控件,这里在上一篇TabHost控件的基础上添加了一个GridView控件,用作Tab页的显示内容。
效果图:
帖代码:
- public class OrderClientActivity extends Activity {
- TabHost tabHost;
- String[] dishType=new String[]{"1","2","3","4","5","6","7"};
- GridView gridView;
- String[] dishName=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12"};
- String picPath=Environment.getExternalStorageDirectory().getPath() + "/picture/";
- GridViewAdapter gridViewAdapter;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.orderform);
- // 获取TabHost对象
- TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
- // 如果没有继承TabActivity时,通过该种方法加载启动tabHost
- tabHost.setup();
- gridView=(GridView)findViewById(R.id.view1);
- gridViewAdapter=new GridViewAdapter(this);
- gridView.setAdapter(gridViewAdapter);
- for(int i=0;i<dishType.length;i++){
- TabHost.TabSpec tabSpec=tabHost.newTabSpec(dishType[i]);
- tabSpec.setIndicator(dishType[i],getResources().getDrawable(R.drawable.ic_launcher));
- tabSpec.setContent(R.id.view1);
- tabHost.addTab(tabSpec);
- }
- tabHost.setCurrentTab(1);
- tabHost.setCurrentTab(0);
- new AsyncLoadImage().execute(100);
- }
- class AsyncLoadImage extends AsyncTask<Object, Bitmap, Object>{
- @Override
- protected Object doInBackground(Object... params) {
- Bitmap bitmap;
- Log.i("加载线程", "");
- // TODO Auto-generated method stub
- for(int i=0;i<dishName.length;i++){
- Log.i("图片路径", picPath + dishName[i] + ".jpg");
- bitmap=BitmapFactory.decodeFile(picPath + dishName[i] + ".jpg");
- if(bitmap != null){
- publishProgress(bitmap);
- Log.i("图片解析", "正确");
- }
- else
- Log.i("图片解析", "错误");
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Bitmap... values) {
- // TODO Auto-generated method stub
- for(Bitmap b : values){
- gridViewAdapter.addPic(b);
- gridViewAdapter.notifyDataSetChanged();
- }
- }
- }
- class GridViewAdapter extends BaseAdapter{
- Context context;
- private ArrayList<Bitmap> picList=new ArrayList<Bitmap>();
- GridViewAdapter(Context context){
- this.context=context;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return picList.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return picList.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public void addPic(Bitmap bitmap){
- picList.add(bitmap);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- Log.i("得到view", position + "");
- ImageView image = null;
- if(convertView == null){
- image=new ImageView(context);
- }
- else{
- image=(ImageView)convertView;
- }
- image.setLayoutParams(new GridView.LayoutParams((getWindowManager().getDefaultDisplay().getWidth()-3)/2,(int) (getWindowManager().getDefaultDisplay().getWidth()/2*0.6)));//设置ImageView对象布局
- image.setAdjustViewBounds(true);//设置边界对齐
- image.setScaleType(ImageView.ScaleType.FIT_XY);//设置刻度的类型
- image.setPadding(0, 0, 0, 0);//设置间距
- image.setImageBitmap(picList.get(position));
- return image;
- }
- }
- }
另外,实际的运行过程中,发现Gridview的图片加载很卡,所以我们改成了异步加载图片,用到了AsyncTask类,这是Android提供的一个轻量级的线程既能在后台线程中执行一些耗时操作,又有方法直接更新ui,关于AsyncTask的详细介绍可以参考:android AsyncTask介绍。
这里如果没有必要用到异步加载,可以在GridViewAdapter里直接给picList赋值。
布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/hometabs"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->
- <TabHost android:id="@+id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <HorizontalScrollView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:scrollbars="none">
- <!-- TabWidget的id属性必须为 @android:id/tabs-->
- <TabWidget android:id="@android:id/tabs"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TabWidget>
- </HorizontalScrollView>
- <!-- FrameLayout的id属性必须为 @android:id/tabcontent-->
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <GridView
- android:id="@+id/view1"
- android:listSelector="#000000"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="0dip"
- android:layout_margin="0dip"
- android:numColumns="2"
- android:horizontalSpacing="3dip"
- android:verticalSpacing="3dip"
- android:stretchMode="columnWidth"
- android:gravity="fill"/>"
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hha"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hha2"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hha3"/>
- </LinearLayout>
- </LinearLayout>
在布局文件里看到,GridView设置了一个属性android:listSelector,这个属性用来设置GridView的每个item 的背景。如果没有设置这个属性,每个item在点击的时候都有一个蓝色边框,我们可以设置成我们想要的颜色,不想显示的话可以直接设置成背景色。