今天研究了一个别人写的仿多米音乐的源码,其中有一个功能是抽拉的效果,可以把两个界面的内容显示在一个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。
官方文档中的描述是:
- 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:
主布局配置文件内容:layout/main.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: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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns: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(按钮在按住和正常状态显示不同图片)
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_window_focused="false" android:drawable="@drawable/handle_normal" />
- <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
- <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
- </selector>
组件运行时的状态信息配置文件。drable/listview_selected.xml(listview选项选中效果)
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"
- android:drawable="@drawable/list_selector_background_pressed" />
- </selector>
java类文件,activity代码:com.magh.test.SlidingDrawerTestActivity.java
- package com.magh.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- /**
- * @author Ma Guohui
- * @FileDescription:SlidingDrawer实现伸拉效果案例
- * @version 2012-12-17 下午7:33:06
- * @ChangeList:
- */
- public class SlidingDrawerTestActivity extends Activity {
- /** Called when the activity is first created. */
- private ListView myListView;
- LinearLayout mLinearLayout;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setupViews();
- actionProcess();
- }
- private void setupViews() {
- myListView = (ListView) findViewById(R.id.content);
- myListView.setAdapter(new ListViewAdapter());
- }
- private void actionProcess() {// 伸拉操作时执行不同事件
- android.widget.SlidingDrawer mDrawer = (android.widget.SlidingDrawer) findViewById(R.id.slidingdrawer);
- // mDrawer.open(); //设置抽屉为打开状态
- mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout); // 获取需要控制区域的布局
- mDrawer.setOnDrawerOpenListener(new android.widget.SlidingDrawer.OnDrawerOpenListener() {
- public void onDrawerOpened() {// 当抽屉打开时执行此操作
- // TODO Auto-generated method stub
- LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout
- .getLayoutParams();
- linearParams.height = 40;
- mLinearLayout.setLayoutParams(linearParams);
- }
- });
- mDrawer.setOnDrawerCloseListener(new android.widget.SlidingDrawer.OnDrawerCloseListener() {
- public void onDrawerClosed() {// 抽屉关闭时执行此操作
- // TODO Auto-generated method stub
- LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout
- .getLayoutParams();
- linearParams.height = 80;
- mLinearLayout.setLayoutParams(linearParams);
- }
- });
- mDrawer.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- return false;
- }
- });
- }
- private class ListViewAdapter extends BaseAdapter {// 自定义ListView适配器
- // 这里返回10行,ListView有多少行取决于getCount()方法
- @Override
- public int getCount() {
- return 8;
- }
- @Override
- public Object getItem(int arg0) {
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int position, View v, ViewGroup parent) {
- final LayoutInflater inflater = LayoutInflater
- .from(getApplicationContext());
- if (v == null) {
- v = inflater.inflate(R.layout.listview_layout, null);
- }
- TextView myWebName = (TextView) v.findViewById(R.id.webName);
- TextView myWebDescript = (TextView) v
- .findViewById(R.id.webDescript);
- myWebName.setText("Android小功能案例" + position);
- myWebDescript.setText("自己动手,丰衣足食,马国会" + position);
- return v;
- }
- }
- }
- /*
- * 重要属性 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
- */