Gallery画廊,SlidingDrawer侧滑

packagecom.example.gallerytext;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.Gallery;importandroid.widget.Toast;publicclassMainActivityextendsActivity{ Gallery gallery;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery=(Gallery) findViewById(R.id.gallery); GalleryAdapter galleryAdapter =newGalleryAdapter(MainActivity.this); gallery.setAdapter(galleryAdapter);//相应的点击事件gallery.setOnItemClickListener(newAdapterView.OnItemClickListener() {@OverridepublicvoidonItemClick(AdapterView adapterView, View view,inti,longl){ Toast.makeText(MainActivity.this,"您点击的是"+ i, Toast.LENGTH_LONG).show(); } }); } }xml里面写Gallery属性继承BaseAdapter适配器packagecom.example.gallerytext;importandroid.content.Context;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.Gallery;importandroid.widget.ImageView;/*** Created by ZWH on 2016/5/16.*/publicclassGalleryAdapterextendsBaseAdapter{privateContext mContext;//设置要展示的图片资源int[] images = {R.drawable.ic_launcher,R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4,R.drawable.f5,R.drawable.f6,R.drawable.f7,R.drawable.f8,R.drawable.f9,R.drawable.f10,};publicGalleryAdapter(Context context){this.mContext = context;    }@OverridepublicintgetCount(){returnimages.length;    }@OverridepublicObjectgetItem(inti){returni;    }@OverridepubliclonggetItemId(inti){returni;    }@OverridepublicViewgetView(inti, View view, ViewGroup viewGroup){//在此最好判断一下view是否为空ImageView image =newImageView(mContext);        image.setImageResource(images[i]);        image.setAdjustViewBounds(true);//设置宽image.setLayoutParams(newGallery.LayoutParams(  ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));returnimage;    }}安卓中1.5后加入了SlidingDrawer【隐藏式抽屉】,设计原理在你的UI布局有限的情况下,放不下太多的控件的时候,可以考虑用这个隐藏式抽屉。用SlidingDrawer注意两点,一个是android:handle(委托要展开的图片加载Layout配置) 和android:content(要展开的Layout Content):javapackagecom.example.slid;importandroid.app.Activity;importandroid.content.res.Configuration;importandroid.os.Bundle;importandroid.widget.GridView;importandroid.widget.ImageView;importandroid.widget.SlidingDrawer;publicclassMainActivityextendsActivity{privateGridView gridView;privateSlidingDrawer slidingDrawer;privateImageView imageView;privateint[] icons={          R.drawable.widget01, R.drawable.widget02,          R.drawable.widget03, R.drawable.widget04,          R.drawable.widget05, R.drawable.widget06,        R.drawable.widget07, R.drawable.widget08,        R.drawable.widget09,    };privateString[] items={"家园卫士","安装包","游戏盒子","链接电脑","记事本","垃圾箱","流量管理","软件修复","系统设置"};@OverridepublicvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          gridView = (GridView)findViewById(R.id.mycontent);          slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer);//初始化  imageView = (ImageView)findViewById(R.id.my_image);//拉菜单的小箭头  MyGridViewAdapter adapter =newMyGridViewAdapter(this, items, icons);//继承了BaseAdapter基础适配器  gridView.setAdapter(adapter);//设置适配  slidingDrawer.setOnDrawerOpenListener(newSlidingDrawer.OnDrawerOpenListener() {//设置打开抽屉的监听publicvoidonDrawerOpened(){//设置小箭头的方向imageView.setImageResource(R.drawable.right1);              }          });          slidingDrawer.setOnDrawerCloseListener(newSlidingDrawer.OnDrawerCloseListener() {//设置关闭抽屉监听publicvoidonDrawerClosed(){                  imageView.setImageResource(R.drawable.left1);              }          });      }      }packagecom.example.slid;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.TextView;publicclassMyGridViewAdapterextendsBaseAdapter{privateContext context;privateString[] items;privateint[] icons;publicMyGridViewAdapter(Context context, String[] items,int[] icons){this.context = context;this.items = items;this.icons = icons;      }publicintgetCount(){returnitems.length;      }publicObjectgetItem(intarg0){returnitems[arg0];      }publiclonggetItemId(intposition){returnposition;      }publicViewgetView(intposition, View convertView, ViewGroup parent){          Shuju shuju=null;if(convertView==null){            LayoutInflater layoutInflater = LayoutInflater.from(context);              convertView = (View)layoutInflater .inflate(R.layout.wode,null);            shuju=newShuju();            shuju.imageView = (ImageView)convertView.findViewById(R.id.image_view);              shuju.textview = (TextView)convertView.findViewById(R.id.text_view);            convertView.setTag(shuju);        }else{            shuju= (Shuju) convertView.getTag();        }//第二种加载布局方式shuju.imageView.setImageResource(icons[position]);          shuju.textview.setText(items[position]);returnconvertView;      }classShuju{    TextView textview;    ImageView imageView;    }  }

你可能感兴趣的:(Gallery画廊,SlidingDrawer侧滑)