Android学习之SlidingDrawer抽屉组件

main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/MyLayout" 
	android:orientation="vertical"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent">
	<SlidingDrawer 
		android:id="@+id/slidingdrawer"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:orientation="horizontal"
		android:handle="@+id/handle"
		android:content="@+id/content">
		<ImageView
			android:id="@+id/handle"
			android:src="@drawable/ico_left"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<LinearLayout
			android:id="@+id/content"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical">
		</LinearLayout>
	</SlidingDrawer>
</LinearLayout>

 

 

 

 

 

MySlidingDrawerDemo.java:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import 

android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import 

android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import 

android.widget.SlidingDrawer.OnDrawerScrollListener;
import android.widget.Toast;

public class MySlidingDrawerDemo extends Activity {
	

private String data[] = { "选项1", "选项2", "选项3",
			"选项4", "选项5"};					// 定义显示的

数据
	private ListView listView;  									// 定义ListView组件
	private 

SlidingDrawer slidingDrawer ;							// 定义SlidingDrawer
	private ImageView handle ;		

								// 定义图片显示

	@Override
	public void onCreate(Bundle 

savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		LinearLayout 

layout = (LinearLayout) super.findViewById(R.id.content) ;
		this.listView = new ListView(this) ;					

	// 实例化组件
		listView.setAdapter(new ArrayAdapter<String>(this,			// 将数据包装
				

android.R.layout.simple_expandable_list_item_1, 	// 每行显示一条数据
				this.data));				

						// 设置组件内容
		layout.addView(this.listView) ;						

		// 增加组件
		this.slidingDrawer = (SlidingDrawer) super.findViewById(R.id.slidingdrawer) ;
		this.handle = 

(ImageView) super.findViewById(R.id.handle) ;// 取得组件
		this.slidingDrawer.setOnDrawerOpenListener(new 

OnDrawerOpenListenerImpl()) ;	// 设置监听										
		

this.slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListenerImpl()) ;	// 设置监听							

						
		this.slidingDrawer.setOnDrawerScrollListener(new OnDrawerScrollListenerImpl

()) ;// 设置监听
	}
	private class OnDrawerOpenListenerImpl implements OnDrawerOpenListener {
		@Override
		

public void onDrawerOpened() {
			handle.setImageResource(R.drawable.ico_right) ;			// 窗口打开监听
		}
	

}
	private class OnDrawerCloseListenerImpl implements OnDrawerCloseListener {
		@Override
		public void 

onDrawerClosed() {
			handle.setImageResource(R.drawable.ico_left) ;			// 窗口关闭监听
		}
	}
	

private class OnDrawerScrollListenerImpl implements OnDrawerScrollListener {
		@Override
		public void onScrollEnded() {	

							// 拖动结束
			Toast.makeText(MySlidingDrawerDemo.this, "窗口拖动结

束。", Toast.LENGTH_SHORT).show();
		}
		@Override
		public void onScrollStarted() {				

				// 拖动开始
			Toast.makeText(MySlidingDrawerDemo.this, "正在拖动窗口。", 

Toast.LENGTH_SHORT).show();
		}
	}
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *