【小功能4】android用SlidingDrawer实现抽拉的滑动效果(也叫抽屉)附源码
今天研究了一个别人写的仿多米音乐的源码,其中有一个功能是抽拉的效果,可以把两个界面的内容显示在一个activity中,利用抽拉实现多内容显示。自己通过摸索参考网上解释实现这一功能,并实现了自己想要的一个功能(为一个程序添加这个功能),接下来上图,没图说个MX。 SlidingDrawer是一个可以实现抽取式显示的控件,可以垂直,水平拉取,每个抽屉都包含两个东西:一个是拉手(handle),一个是抽屉里面的东西(content),SlidingDrawer需要放置在另外的一个layout之上,这意味着SlidingDrawer只能放置在 FrameLayout or a RelativeLayout里面。SlidingDrawer需要设置宽高为match_parent,而且在SlidingDrawer里面必须设置handle与content:。SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的 handle,其二是隐藏内容的View。它里面的控件必须设置布局,在布局文件中必须指定handle和content。 官方文档中的描述是: publicclassSlidingDrawerextendsViewGroupjava.lang.ObjectAndroid.view.Viewandroid.view.ViewGroupandroid.widget.SlidingDrawer 原文:SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen.SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags,and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts.This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance.The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensions.Inside an XML layout, SlidingDrawer must define the id of the handle and of the content: 主布局配置文件内容:layout/main.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" android:orientation="vertical"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="80px" android:background="#0000ff" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="该区域大小可通过抽屉控制(马国会)" android:textAppearance="?android:attr/textAppearanceMedium"/> </LinearLayout> <SlidingDrawer android:id="@+id/slidingdrawer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:content="@+id/content" android:handle="@+id/handle" android:orientation="horizontal" > <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/handle"/> <ListView android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </SlidingDrawer> </LinearLayout> ListView条目项显示内容布局文件。layout/listview_layout.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/listview_selected" android:padding="6px"> <TextView android:id="@+id/webName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" android:textColor="#000000"/> <TextView android:id="@+id/webDescript" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16px" android:textColor="#000000"/> </LinearLayout> </LinearLayout> 组件运行时的状态信息配置文件。drable/handle.xml(按钮在按住和正常状态显示不同图片) <?xmlversion="1.0"encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:state_window_focused="false"android:drawable="@drawable/handle_normal"/> <itemandroid:state_focused="true"android:drawable="@drawable/handle_focused"/> <itemandroid:state_pressed="true"android:drawable="@drawable/handle_pressed"/> </selector> 组件运行时的状态信息配置文件。drable/listview_selected.xml(listview选项选中效果) <?xmlversion="1.0"encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:state_pressed="true" android:drawable="@drawable/list_selector_background_pressed"/> </selector> java类文件,activity代码:com.magh.test.SlidingDrawerTestActivity.java packagecom.magh.test; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.LayoutInflater; importandroid.view.MotionEvent; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.LinearLayout; importandroid.widget.ListView; importandroid.widget.TextView; /** *@authorMaGuohui *@FileDescription:SlidingDrawer实现伸拉效果案例 *@version2012-12-17下午7:33:06 *@ChangeList: */ publicclassSlidingDrawerTestActivityextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ privateListViewmyListView; LinearLayoutmLinearLayout; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); actionProcess(); } privatevoidsetupViews(){ myListView=(ListView)findViewById(R.id.content); myListView.setAdapter(newListViewAdapter()); } privatevoidactionProcess(){//伸拉操作时执行不同事件 android.widget.SlidingDrawermDrawer=(android.widget.SlidingDrawer)findViewById(R.id.slidingdrawer); //mDrawer.open();//设置抽屉为打开状态 mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);//获取需要控制区域的布局 mDrawer.setOnDrawerOpenListener(newandroid.widget.SlidingDrawer.OnDrawerOpenListener(){ publicvoidonDrawerOpened(){//当抽屉打开时执行此操作 //TODOAuto-generatedmethodstub LinearLayout.LayoutParamslinearParams=(LinearLayout.LayoutParams)mLinearLayout .getLayoutParams(); linearParams.height=40; mLinearLayout.setLayoutParams(linearParams); } }); mDrawer.setOnDrawerCloseListener(newandroid.widget.SlidingDrawer.OnDrawerCloseListener(){ publicvoidonDrawerClosed(){//抽屉关闭时执行此操作 //TODOAuto-generatedmethodstub LinearLayout.LayoutParamslinearParams=(LinearLayout.LayoutParams)mLinearLayout .getLayoutParams(); linearParams.height=80; mLinearLayout.setLayoutParams(linearParams); } }); mDrawer.setOnTouchListener(newView.OnTouchListener(){ @Override publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto-generatedmethodstub returnfalse; } }); } privateclassListViewAdapterextendsBaseAdapter{//自定义ListView适配器 //这里返回10行,ListView有多少行取决于getCount()方法 @Override publicintgetCount(){ return8; } @Override publicObjectgetItem(intarg0){ returnnull; } @Override publiclonggetItemId(intarg0){ return0; } @Override publicViewgetView(intposition,Viewv,ViewGroupparent){ finalLayoutInflaterinflater=LayoutInflater .from(getApplicationContext()); if(v==null){ v=inflater.inflate(R.layout.listview_layout,null); } TextViewmyWebName=(TextView)v.findViewById(R.id.webName); TextViewmyWebDescript=(TextView)v .findViewById(R.id.webDescript); myWebName.setText("Android小功能案例"+position); myWebDescript.setText("自己动手,丰衣足食,马国会"+position); returnv; } } } /* *重要属性android:allowSingleTap:指示是否可以通过handle打开或关闭 *android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。 * android:content:隐藏的内容 *android:handle:handle(手柄) * *重要方法animateClose():关闭时实现动画 * close():即时关闭getContent():获取内容 *isMoving():指示SlidingDrawer是否在移动 * isOpened():指示SlidingDrawer是否已全部打开 *lock():屏蔽触摸事件setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener *onDrawerCloseListener):SlidingDrawer关闭时调用unlock():解除屏蔽触摸事件 *toggle():切换打开和关闭的抽屉SlidingDrawer */ 附件:http://down.51cto.com/data/2361988 本文转自 646676684 51CTO博客,原文链接:http://blog.51cto.com/2402766/1092350,如需转载请自行联系原作者