【小功能4】android用SlidingDrawer实现抽拉的滑动效果(也叫抽屉)附源码

   今天研究了一个别人写的仿多米音乐的源码,其中有一个功能是抽拉的效果,能够把两个界面的内容显示在一个activity中,利用抽拉实现多内容显示。本身经过摸索参考网上解释实现这一功能,并实现了本身想要的一个功能(为一个程序添加这个功能),接下来上图,没图说个MX。java

 

未拉以前

拉的过程当中

拉出来以后

 

SlidingDrawer是一个能够实现抽取式显示的控件,能够垂直,水平拉取,每一个抽屉都包含两个东西:一个是拉手(handle),一个是抽屉里面的东西(content),SlidingDrawer须要放置在另外的一个layout之上,这意味着SlidingDrawer只能放置在 FrameLayout or a RelativeLayout里面。SlidingDrawer须要设置宽高为match_parent,并且在SlidingDrawer里面必须设置handle与content:。SlidingDrawer隐藏屏外的内容,并容许用户经过handle以显示隐藏内容。它能够垂直或水平滑动,它有俩个View组成,其一是能够拖动的 handle,其二是隐藏内容的View。它里面的控件必须设置布局,在布局文件中必须指定handle和content。android

官方文档中的描述是:web

  
  
  
  
  1. public class SlidingDrawerextends ViewGroupjava.lang.Object Android.view.View android.view.ViewGroup android.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:app

    主布局配置文件内容:layout/main.xml
ide

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/background"  
  6.     android:orientation="vertical" >  
  7.      <LinearLayout  
  8.         android:id="@+id/linearLayout"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="80px"  
  11.         android:background="#0000ff"   
  12.         >  
  13.          <TextView  
  14.              android:id="@+id/textView1"  
  15.              android:layout_width="wrap_content"  
  16.              android:layout_height="wrap_content"  
  17.              android:text="该区域大小可经过抽屉控制(马国会)"  
  18.              android:textAppearance="?android:attr/textAppearanceMedium" />  
  19.   
  20.      </LinearLayout>  
  21.         <SlidingDrawer  
  22.             android:id="@+id/slidingdrawer"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="fill_parent"  
  25.             android:content="@+id/content"  
  26.             android:handle="@+id/handle"  
  27.             android:orientation="horizontal"   
  28.             >  
  29.   
  30.             <Button  
  31.                 android:id="@+id/handle"  
  32.                 android:layout_width="wrap_content"  
  33.                 android:layout_height="fill_parent"  
  34.                 android:background="@drawable/handle" />  
  35.   
  36.             <ListView  
  37.                 android:id="@+id/content"  
  38.                 android:layout_width="fill_parent"  
  39.                 android:layout_height="wrap_content"   
  40.                   
  41.                 />  
  42.         </SlidingDrawer>         
  43.     </LinearLayout>  
  44.      ListView条目项显示内容布局文件。layout/listview_layout.xml 
  45.  
  46.  
  47.  
  48. <?xml version="1.0" encoding="utf-8"?>  
  49. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  50.     android:orientation="vertical"  
  51.     android:layout_width="fill_parent"  
  52.     android:layout_height="fill_parent"  
  53.     android:background="#ffffff"    >    
  54.     <LinearLayout  
  55.      android:orientation="vertical"  
  56.      android:layout_width="fill_parent"  
  57.         android:layout_height="fill_parent"  
  58.         android:background="@drawable/listview_selected"  
  59.         android:padding="6px">  
  60.  <TextView  
  61.   android:id="@+id/webName"    
  62.      android:layout_width="fill_parent"   
  63.      android:layout_height="wrap_content"   
  64.      android:textSize="20px"  
  65.      android:textColor="#000000"/>  
  66.  <TextView  
  67.   android:id="@+id/webDescript"    
  68.      android:layout_width="fill_parent"   
  69.      android:layout_height="wrap_content"   
  70.      android:textSize="16px"  
  71.      android:textColor="#000000"/>  
  72.      </LinearLayout>  
  73. </LinearLayout>  

    组件运行时的状态信息配置文件。drable/handle.xml(按钮在按住和正常状态显示不一样图片)布局

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <item android:state_window_focused="false"  android:drawable="@drawable/handle_normal" /> 
  4.     <item android:state_focused="true" android:drawable="@drawable/handle_focused" /> 
  5.     <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" /> 
  6. </selector> 

    组件运行时的状态信息配置文件。drable/listview_selected.xml(listview选项选中效果)动画

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <item android:state_pressed="true" 
  4.         android:drawable="@drawable/list_selector_background_pressed" /> 
  5. </selector> 

    java类文件,activity代码:com.magh.test.SlidingDrawerTestActivity.javaui

  
  
  
  
  1. package com.magh.test; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.view.LayoutInflater; 
  5. import android.view.MotionEvent; 
  6. import android.view.View; 
  7. import android.view.ViewGroup; 
  8. import android.widget.BaseAdapter; 
  9. import android.widget.LinearLayout; 
  10. import android.widget.ListView; 
  11. import android.widget.TextView; 
  12. /**  
  13.  * @author Ma Guohui 
  14.  * @FileDescription:SlidingDrawer实现伸拉效果案例 
  15.  * @version 2012-12-17 下午7:33:06 
  16.  * @ChangeList: 
  17.  */ 
  18. public class SlidingDrawerTestActivity extends Activity { 
  19.     /** Called when the activity is first created. */ 
  20.     private ListView myListView; 
  21.     LinearLayout mLinearLayout; 
  22.  
  23.     @Override 
  24.     public void onCreate(Bundle savedInstanceState) { 
  25.         super.onCreate(savedInstanceState); 
  26.         setContentView(R.layout.main); 
  27.         setupViews(); 
  28.                 actionProcess(); 
  29.     } 
  30.  
  31.     private void setupViews() { 
  32.         myListView = (ListView) findViewById(R.id.content); 
  33.         myListView.setAdapter(new ListViewAdapter()); 
  34.     } 
  35.  
  36.     private void actionProcess() {// 伸拉操做时执行不一样事件 
  37.         android.widget.SlidingDrawer mDrawer = (android.widget.SlidingDrawer) findViewById(R.id.slidingdrawer); 
  38.         // mDrawer.open(); //设置抽屉为打开状态 
  39.         mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout); // 获取须要控制区域的布局 
  40.  
  41.         mDrawer.setOnDrawerOpenListener(new android.widget.SlidingDrawer.OnDrawerOpenListener() { 
  42.  
  43.             public void onDrawerOpened() {// 当抽屉打开时执行此操做 
  44.  
  45.                 // TODO Auto-generated method stub 
  46.  
  47.                 LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout 
  48.                         .getLayoutParams(); 
  49.  
  50.                 linearParams.height = 40
  51.  
  52.                 mLinearLayout.setLayoutParams(linearParams); 
  53.  
  54.             } 
  55.  
  56.         }); 
  57.  
  58.         mDrawer.setOnDrawerCloseListener(new android.widget.SlidingDrawer.OnDrawerCloseListener() { 
  59.  
  60.             public void onDrawerClosed() {// 抽屉关闭时执行此操做 
  61.  
  62.                 // TODO Auto-generated method stub 
  63.  
  64.                 LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout 
  65.                         .getLayoutParams(); 
  66.  
  67.                 linearParams.height = 80
  68.                 mLinearLayout.setLayoutParams(linearParams); 
  69.  
  70.             } 
  71.         }); 
  72.  
  73.         mDrawer.setOnTouchListener(new View.OnTouchListener() { 
  74.  
  75.             @Override 
  76.             public boolean onTouch(View v, MotionEvent event) { 
  77.                 // TODO Auto-generated method stub 
  78.                 return false
  79.             } 
  80.         }); 
  81.  
  82.     } 
  83.  
  84.     private class ListViewAdapter extends BaseAdapter {// 自定义ListView适配器 
  85.         // 这里返回10行,ListView有多少行取决于getCount()方法 
  86.         @Override 
  87.         public int getCount() { 
  88.             return 8
  89.         } 
  90.  
  91.         @Override 
  92.         public Object getItem(int arg0) { 
  93.             return null
  94.         } 
  95.  
  96.         @Override 
  97.         public long getItemId(int arg0) { 
  98.             return 0
  99.         } 
  100.  
  101.         @Override 
  102.         public View getView(int position, View v, ViewGroup parent) { 
  103.             final LayoutInflater inflater = LayoutInflater 
  104.                     .from(getApplicationContext()); 
  105.             if (v == null) { 
  106.                 v = inflater.inflate(R.layout.listview_layout, null); 
  107.             } 
  108.             TextView myWebName = (TextView) v.findViewById(R.id.webName); 
  109.             TextView myWebDescript = (TextView) v 
  110.                     .findViewById(R.id.webDescript); 
  111.             myWebName.setText("Android小功能案例" + position); 
  112.             myWebDescript.setText("本身动手,丰衣足食,马国会" + position); 
  113.             return v; 
  114.         } 
  115.     } 
  116. /* 
  117.  * 重要属性 android:allowSingleTap:指示是否能够经过handle打开或关闭 
  118.  * android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。 
  119. * android:content:隐藏的内容 
  120.  * android:handle:handle(手柄) 
  121.  *  
  122.  * 重要方法 animateClose():关闭时实现动画
  123.  * close():即时关闭 getContent():获取内容 
  124.  * isMoving():指示SlidingDrawer是否在移动   * isOpened():指示SlidingDrawer是否已所有打开 
  125.  * lock():屏蔽触摸事件 setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener 
  126.  * onDrawerCloseListener):SlidingDrawer关闭时调用 unlock():解除屏蔽触摸事件 
  127.  * toggle():切换打开和关闭的抽屉SlidingDrawer 
  128.  */
相关文章
相关标签/搜索