Android之SlidingDrawer(滑動式抽屜)

來源:互聯網
上載者:User

 一、簡介

  SlidingDrawer隱藏屏外的內容,並允許使用者通過handle以顯示隱藏內容。它可以垂直或水平滑動,它有倆個View組成,其一是可以拖動的handle,其二是隱藏內容的View.它裡面的控制項必須設定布局,在布局檔案中必須指定handle和content.

  SlidingDrawer效果想必大家也見到過,它就是1.5模擬器上進入應用程式列表的效果。下面是

            

 

例如下面

  

< SlidingDrawer android:layout_width="fill_parent"  android:layout_height="fill_parent" android:handle="@+id/handle"  android:content="@+id/content" android:orientation="vertical"  android:id="@+id/slidingdrawer">  < ImageButton android:id="@id/handle" android:layout_width="50dip"  android:layout_height="44dip" android:src="@drawable/up" />  < LinearLayout android:id="@id/content"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:background="#ffffff">  < TextView android:text="這是一個滑動式抽屜的樣本"  android:id="@+id/tv"  android:textSize="18px"  android:textColor="#000000"  android:gravity="center_vertical|center_horizontal"  android:layout_width="match_parent"  android:textStyle="bold"  android:layout_height="match_parent">< /TextView>  < /LinearLayout>  < /SlidingDrawer>

二、重要屬性

  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。

四、完整執行個體

  1.布局檔案

       slidingdrawer.xml

 

   < ?xml version="1.0" encoding="utf-8"?>  < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:orientation="vertical" android:background="@drawable/default_bg">  < SlidingDrawer android:layout_width="fill_parent"  android:layout_height="fill_parent" android:handle="@+id/handle"  android:content="@+id/content" android:orientation="vertical"  android:id="@+id/slidingdrawer">  < ImageButton android:id="@id/handle" android:layout_width="50dip"  android:layout_height="44dip" android:src="@drawable/up" />  < LinearLayout android:id="@id/content"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:background="#ffffff">  < TextView android:text="這是一個滑動式抽屜的樣本"  android:id="@+id/tv"  android:textSize="18px"  android:textColor="#000000"  android:gravity="center_vertical|center_horizontal"  android:layout_width="match_parent"  android:textStyle="bold"  android:layout_height="match_parent">< /TextView>  < /LinearLayout>  < /SlidingDrawer>  < /LinearLayout>

  2.Java代碼

  

    package com.wjq;  import android.app.Activity;    import android.os.Bundle;  import android.widget.ImageButton;  import android.widget.SlidingDrawer;  import android.widget.TextView;  public class SlidingDrawerDemo extends Activity {  private SlidingDrawer mDrawer;  private ImageButton imbg;  private Boolean flag=false;  private TextView tv;  /* (non-Javadoc)  * @see android.app.Activity#onCreate(android.os.Bundle)  */  @Override  protected void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.sildingdrawer);  imbg=(ImageButton)findViewById(R.id.handle);  mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer);  tv=(TextView)findViewById(R.id.tv);  mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()  {  @Override  public void onDrawerOpened() {  flag=true;  imbg.setImageResource(R.drawable.down);  }  });  mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener(){  @Override  public void onDrawerClosed() {  flag=false;  imbg.setImageResource(R.drawable.up);  }  });  mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener(){  @Override  public void onScrollEnded() {  tv.setText("結束拖動");  }  @Override  public void onScrollStarted() {  tv.setText("開始拖動");  }  });  }  }

本文轉自http://dev.10086.cn/cmdn/wiki/index.php?edition-view-5028-1.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.