主Activity:
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); } }); } }
自定义Adapter:
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; } }
主xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="16sp" /> <SlidingDrawer android:id="@+id/drawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/layout1" android:content="@+id/myContent1" android:orientation="horizontal" > <LinearLayout android:id="@id/layout1" android:layout_width="35px" android:layout_height="fill_parent" android:background="@drawable/black" android:gravity="center" > <ImageView android:id="@+id/myImage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/open" /> </LinearLayout> <GridView android:id="@id/myContent1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="2" android:background="@drawable/black" android:gravity="center" /> </SlidingDrawer> </RelativeLayout>