Today I learned about the slidingdrawer control and completed a simple demo to share with you:
1. Create two layout files under layout. The main layout file main. xml used in the current activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <SlidingDrawer android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/slidingDrawer" android:handle="@+id/layout1" android:content="@+id/gridView" android:orientation="horizontal"> <LinearLayout android:id="@+id/layout1" android:layout_width="30px" android:layout_height="wrap_content" android:background="@drawable/main" android:gravity="center"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/save"> </ImageView> </LinearLayout> <GridView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gridView" android:numColumns="3" android:background="@drawable/main" android:gravity="center"/> </SlidingDrawer></RelativeLayout>
Layout file grid. xml used in the adapter
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <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="#FFFFFF"/></LinearLayout>
2. The slidingdrawertestactivity. Java code of the current activity is as follows:Public class slidingdrawertestactivity extends activity {private gridview; private slidingdrawer; private imageview; private int [] icons = {R. drawable. alarm, R. drawable. calendar, R. drawable. clock, R. drawable. music, R. drawable. TV}; private string [] items = {"alarm", "calendar", "Clock", "Music", "TV" };@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedin Stancestate); setcontentview (R. layout. main); gridview = (gridview) findviewbyid (R. id. gridview); slidingdrawer = (slidingdrawer) findviewbyid (R. id. slidingdrawer); imageview = (imageview) findviewbyid (R. id. imageview); gridviewadapter = new gridviewadapter (this, items, icons); gridview. setadapter (gridviewadapter); sjavasingdrawer. setondraweropenlistener (New slidingdrawer. ondraweropenlistener () {@ ove Rridepublic void ondraweropened () {imageview. setimageresource (R. drawable. open) ;}}); slidingdrawer. setondrawercloselistener (New s0000ingdrawer. ondrawercloselistener () {@ overridepublic void ondrawerclosed () {imageview. setimageresource (R. drawable. close) ;}}); slidingdrawer. setondrawerscrolllistener (New s0000ingdrawer. ondrawerscrolllistener () {@ overridepublic void onscrollstarted () {log. D ("Print test", "onscrolls Run the tarted () method ") ;}@ overridepublic void onscrollended () {log. D ("Print test", "onscrollended () method execution") ;}}); gridview. setonitemclicklistener (New gridview. onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {Switch (icons [arg2]) // only icons [arg2] can be used, and items [ARGs] cannot be used (only in Java 7)
{Case R. drawable. alarm: Toast. maketext (getapplicationcontext (), "you clicked" + items [arg2], toast. length_short ). show (); break; case R. drawable. calendar: Toast. maketext (getapplicationcontext (), "you clicked" + items [arg2], toast. length_short ). show (); break; case R. drawable. clock: Toast. maketext (getapplicationcontext (), "you clicked" + items [arg2], toast. length_short ). show (); break; case R. drawable. music: Toast. maketext (getapplicationcontext (), "you clicked" + items [arg2], toast. length_short ). show (); break; case R. drawable. TV: Toast. maketext (getapplicationcontext (), "you clicked" + items [arg2], toast. length_short ). show (); break; default: break ;}}});}}
The gridviewadapter. Java code of the adapter file is as follows:
public class GridViewAdapter extends BaseAdapter{ private Context context; private String []items; private int[] icons; public GridViewAdapter(Context context,String[] items, int[] icons) {this.context=context;this.items=items;this.icons=icons;}@Overridepublic int getCount() {return items.length;}@Overridepublic Object getItem(int position) {return items[position];}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {LayoutInflater factory=LayoutInflater.from(context);View view=(View)factory.inflate(R.layout.grid,null);ImageView imageView=(ImageView)view.findViewById(R.id.icon);TextView textView=(TextView)view.findViewById(R.id.text);imageView.setImageResource(icons[position]);textView.setText(items[position]);return view;}}
The slide drawer is run as follows