Android中抽屉(SlidingDrawer)的使用介绍及实例记录
xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/common_home_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="true"
android:drawableLeft="@drawable/ic_menu_home"
android:drawablePadding="5dip"
android:gravity="center_vertical"
android:text="高级查询"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5dp" >
<EditText
android:id="@+id/editText"
android:layout_width="240dip"
android:layout_height="50dip"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:clickable="true" >
</EditText>
<Button
android:id="@+id/button"
android:layout_width="69dip"
android:layout_height="50dip"
android:layout_weight="1"
android:background="@drawable/btn_event_search_select" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbInterestEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/tab_bg_white_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="最近搜索"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="@+id/rbRecommend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/tab_bg_white_selector"
android:button="@null"
android:gravity="center"
android:text="查看记录"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbBarFirst"
android:layout_width="wrap_content"
android:layout_height="5dip"
android:layout_weight="1"
android:background="@drawable/smile_bar_selector"
android:button="@null"
android:checked="true" />
<RadioButton
android:id="@+id/rbBarSecond"
android:layout_width="wrap_content"
android:layout_height="5dip"
android:layout_weight="1"
android:background="@drawable/smile_bar_selector"
android:button="@null" />
</RadioGroup>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="300dp" >
</ListView>
</LinearLayout>
<SlidingDrawer
android:id="@+id/slidingdrawer"
android:layout_width="fill_parent"
android:layout_height="280dp"
android:content="@+id/content"
android:handle="@+id/handle"
android:animateOnClick="true"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:id="@+id/handle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout5"
android:background="@color/advanced_filter_bg"
android:clickable="true"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="高级筛选"
android:textColor="@color/white_color"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:background="@color/white_color"
android:orientation="vertical"
android:visibility="gone" >
<LinearLayout
android:id="@+id/LinearLayout6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:paddingRight="50dip"
android:text="城市" />
<Button
android:id="@+id/button2"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="北京" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:paddingRight="50dip"
android:text="时间" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2011年11月01日" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="至" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2011年11月07日" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:paddingRight="50dip"
android:text="类型" />
<Button
android:id="@+id/button5"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="骑行 徒步 旅游" />
</LinearLayout>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
java代码:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
public class AdvancedQueryActivity extends Activity implements OnClickListener {
private TextView title,advanced_filter;
private RadioButton r1,r2,r3,r4;
private LayoutInflater mInflater = null;
private boolean state = false;
private ListView listView;
private List<String> data;
private SlidingDrawer mDrawer;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
this.setContentView(R.layout.advanced_query_view);
initLayout();
}
private void initLayout() {
// TODO Auto-generated method stub
mInflater=this.getLayoutInflater();
data = new ArrayList<String>();
r1 = (RadioButton)findViewById(R.id.rbInterestEvent);
r2 = (RadioButton)findViewById(R.id.rbRecommend);
r3 = (RadioButton)findViewById(R.id.rbBarFirst);
r4 = (RadioButton)findViewById(R.id.rbBarSecond);
r1.setOnClickListener(this);
r2.setOnClickListener(this);
r3.setOnClickListener(this);
r4.setOnClickListener(this);
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(new MyAdapter());
advanced_filter = (TextView)findViewById(R.id.handle);
advanced_filter.setOnClickListener(this);
title=(TextView)findViewById(R.id.common_home_page);
title.setOnClickListener(this);
title.setText("高级查询");
mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer);
mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {//抽屉打开时监听
@Override
public void onDrawerOpened() {
// TODO Auto-generated method stub
}
});
// mDrawer.setOnDrawerScrollListener(new OnDrawerScrollListener(){//动态监听
// @Override
// public void onScrollEnded() {//结束拖动监听
// advanced_filter.setText("结束拖动");
// }
// @Override
// public void onScrollStarted() {//开始拖动监听
// advanced_filter.setText("开始拖动");
// }
// });
mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {//抽屉关闭监听
@Override
public void onDrawerClosed() {
// TODO Auto-generated method stub
}
});
addDataToUI();
}
private void addDataToUI() {
// TODO Auto-generated method stub
data.add("1");
data.add("2");
data.add("3");
data.add("4");
data.add("5");
data.add("6");
data.add("7");
data.add("8");
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = mInflater.inflate(R.layout.record_item_view, null);
return convertView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.common_home_page:
break;
case R.id.handle:
if (state == false) {
mDrawer.animateOpen();//以动画的形式展开
state = true;
} else {
mDrawer.animateClose();//以动画的形式关闭抽屉
state=false;
}
break;
//-----------------------以下为监听单选按钮RadioButton的状态切换---------------------------------
case R.id.rbInterestEvent:
r1.setChecked(true);
r3.setChecked(true);
break;
case R.id.rbRecommend:
r2.setChecked(true);
r4.setChecked(true);
break;
case R.id.rbBarFirst:
r1.setChecked(true);
r3.setChecked(true);
break;
case R.id.rbBarSecond:
r2.setChecked(true);
r4.setChecked(true);
break;
}
}
}
其中几个重要属性
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。