找了个例子练习隐藏小抽屉SlidingDrawer 不要忘记设置其handle和Content属性,前者是一个布局,而后者是这个布局所处的位置,还要在activity中重写小抽屉打开和关闭时的事件,也可以修改小抽屉的方向属性,使其变为垂直方向的。其值的设置方式是“@+id/id”,并且在后来的布局中id则是"@id/id",又一次可以练习BaseAdapter,重写它的各个方法,尤其返回值是View类型的方法中LayoutInflater类。
Activity:
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
public class SlidingDrawerpractice extends Activity
{
private GridView gvGridView;
private SlidingDrawer sdDrawer;
private ImageView imageView;
private int[] icons={R.drawable.alarm,R.drawable.calendar,R.drawable.camera,R.drawable.clock,R.drawable.music};
private String[] items={"alarm","calender","camera","clock","music"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.main);
/* 初始化对象 */
gvGridView=(GridView)findViewById(R.id.myContent1);
sdDrawer=(SlidingDrawer)findViewById(R.id.drawer1);
imageView=(ImageView)findViewById(R.id.myImage1);
MyGridViewAdapter adapter=new MyGridViewAdapter(this, items, icons);
gvGridView.setAdapter(adapter);
sdDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
// TODO Auto-generated method stub
imageView.setImageResource(R.drawable.icon);
}
});
sdDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
// TODO Auto-generated method stub
imageView.setImageResource(R.drawable.icon);
}
});
}
}
GridViewAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyGridViewAdapter extends BaseAdapter{
private Context ctxContext;
private String[] _items;
private int[] _icons;
public MyGridViewAdapter(Context ctx,String[] items,int[] icons)
{
this.ctxContext=ctx;
this._items=items;
this._icons=icons;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _items.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return _items[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=LayoutInflater.from(ctxContext);
convertView=inflater.inflate(R.layout.grid, null);
ImageView imageView=(ImageView) convertView.findViewById(R.id.icon);
imageView.setImageResource(_icons[position]);
TextView textView=(TextView)convertView.findViewById(R.id.text);
textView.setText(_items[position]);
return convertView;
}
}
Xml 配置文件:
<?xml version="1.0" encoding="utf-8"?>
<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/icon"
/>
</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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:gravity="center"
android:textColor="#0033ff"
/>
</LinearLayout>