[android]代码库
public class EX04_27 extends Activity {
private GridView gv;
private SlidingDrawer sd;
private ImageView im;
private int[] icons = { R.drawable.alarm, R.drawable.calendar,
R.drawable.camera, R.drawable.clock, R.drawable.music,
R.drawable.tv };
private String[] items = { "Alarm", "Calendar", "Camera", "Clock", "Music",
"TV" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.main);
/* 初始化对象 */
gv = (GridView) findViewById(R.id.myContent1);
sd = (SlidingDrawer) findViewById(R.id.drawer1);
im = (ImageView) findViewById(R.id.myImage1);
/* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */
MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);
gv.setAdapter(adapter);
/* 设定SlidingDrawer被打开的事件处理 */
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
im.setImageResource(R.drawable.close);
}
});
/* 设置SlidingDrawer被关闭的事件处理 */
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
im.setImageResource(R.drawable.open);
}
});
}
}
----------------------------------------------------------
public class MyGridViewAdapter extends BaseAdapter {
private Context _con;
private String[] _items;
private int[] _icons;
/* 构造符 */
public MyGridViewAdapter(Context con, String[] items, int[] icons) {
_con = con;
_items = items;
_icons = icons;
}
@Override
public int getCount() {
return _items.length;
}
@Override
public Object getItem(int arg0) {
return _items[arg0];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(_con);
/* 使用grid.xml为每几个item的Layout */
View v = (View) factory.inflate(R.layout.grid, null);
/* 取得View */
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tv = (TextView) v.findViewById(R.id.text);
/* 设定显示的Image与文? */
iv.setImageResource(_icons[position]);
tv.setText(_items[position]);
return v;
}
}