anroid组件之隐式抽屉SlidingDrawer

android的抽屉效果有SlidingDrawer组件,主要作为菜单显示更多信息,节省屏幕空间。使用简单,用法如下:

 

1、在xml布局文件中,添加一个SlidingDrawer的标签,内嵌一个ImageView和一个ListView,其中ImageView用来做抽开抽屉的手柄,ListView的内容可以作为抽屉打开有显示的内容,xml如下:

<SlidingDrawer 
    android:id="@+id/slidingdrawer"
    android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	android:orientation="horizontal"
  	android:handle="@+id/hanler"<!--这里的handle很重要,就是下面的图片ID-->
  	android:content="@+id/content"<!--这里的content很重要,就是下面的ListView-->
    >
    <ImageView
        android:id="@+id/hanler"
        android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:src="@drawable/ic_launcher"/>
    <LinearLayout 
        android:id="@+id/content"
        android:layout_width="fill_parent"
  		android:layout_height="wrap_content"
        ></LinearLayout>
</SlidingDrawer>

 

2、在java代码中,主要做2件事:

 

一、给ListView设置数据源Adapter,这个不是SlidingDrawer必须的,然后把listView添加到抽屉组件中

二、给SlidingDrawer设置监听事件,有打开的、关闭、拖动

 

详细代码我都贴出来:

 

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		drawer=(SlidingDrawer) findViewById(R.id.slidingdrawer);
		layout=(LinearLayout) findViewById(R.id.content);
		handler=(ImageView) findViewById(R.id.hanler);
		//创建listview
		listView=new ListView(this);
		//geilistview设置数据源Adapter
		listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
		layout.addView(listView);
		
		drawer.setOnDrawerOpenListener(new onDrawerOpenListenerImpl());
		drawer.setOnDrawerCloseListener(new onDrawerCloseListenerImpl());
		drawer.setOnDrawerScrollListener(new onDrawerScrollListenerImpl());
	}

 

     这个是抽屉的打开事件

 

private class onDrawerOpenListenerImpl implements OnDrawerOpenListener{
		@Override
		public void onDrawerOpened() {
			Toast.makeText(MainActivity.this, "抽屉正在打开", Toast.LENGTH_SHORT).show();
		}
		
	}

 

   这个是抽屉的关闭事件

private class onDrawerCloseListenerImpl implements OnDrawerCloseListener{
@Override
public void onDrawerClosed() {
Toast.makeText(MainActivity.this, "抽屉关闭", Toast.LENGTH_SHORT).show();
}
}

 

       抽屉的拖动事件

private class onDrawerScrollListenerImpl implements OnDrawerScrollListener{
 
@Override
public void onScrollEnded() {
Toast.makeText(MainActivity.this, "抽屉拖动结束", Toast.LENGTH_SHORT).show();
}
 
@Override
public void onScrollStarted() {
Toast.makeText(MainActivity.this, "抽屉正在拖动", Toast.LENGTH_SHORT).show();
}
 
}

 

你可能感兴趣的:(slidingdrawer)