我们来看一下官方文档中对这个控件 的定义:
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:
其实android1.5的应用程序列表就应用了这个控件。
我们来看一下它特有的xml属性
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Attribute Name | Related Method | Description | |||||||||
android:allowSingleTap | Indicates whether the drawer can be opened/closed by a single tap on the handle. | ||||||||||
android:animateOnClick | Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. | ||||||||||
android:bottomOffset | Extra offset for the handle at the bottom of the SlidingDrawer. | ||||||||||
android:content | Identifier for the child that represents the drawer's content. | ||||||||||
android:handle | Identifier for the child that represents the drawer's handle. | ||||||||||
android:orientation | Orientation of the SlidingDrawer. | ||||||||||
android:topOffset | Extra offset for the handle at the top of the SlidingDrawer. |
先来看一下最终的效果截个图:
两张图分别对应SlidingDrawer关闭时和打开时的视图
看一下layout的定义
- <?xml version="1.0" encoding="utf-8"?>
- <SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/drawer" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:handle="@+id/handle"
- android:content="@+id/content">
- <ImageView android:id="@id/handle" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:src="@drawable/up"></ImageView>
- <ListView android:id="@id/content" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:background="#ff00ff"></ListView>
- </SlidingDrawer>
这里在SlidingDrawer元素里必须指定它的handle和content属性
再看一下Activity的实现:
- package kevin.droid;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.SlidingDrawer;
- import android.widget.SlidingDrawer.OnDrawerCloseListener;
- import android.widget.SlidingDrawer.OnDrawerOpenListener;
- import android.widget.Toast;
- public class SlidingDemo extends Activity implements OnItemClickListener,
- OnDrawerOpenListener, OnDrawerCloseListener {
- private SlidingDrawer drawer;
- private ImageView handle;
- private ListView content;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sliding);
- drawer = (SlidingDrawer) this.findViewById(R.id.drawer);
- handle = (ImageView) this.findViewById(R.id.handle);
- content = (ListView) this.findViewById(R.id.content);
- content.setAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, new String[] { "one",
- "two", "three" }));
- content.setOnItemClickListener(this);
- // 设置SlidingDrawer打开或者关闭时的监听器
- drawer.setOnDrawerOpenListener(this);
- drawer.setOnDrawerCloseListener(this);
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- Toast.makeText(this, "you clicked position " + (position + 1),
- Toast.LENGTH_SHORT).show();
- }
- // SlidingDrawer关闭时回调
- @Override
- public void onDrawerClosed() {
- handle.setImageResource(R.drawable.up);
- }
- // SlidingDrawer打开时回调
- @Override
- public void onDrawerOpened() {
- handle.setImageResource(R.drawable.down);
- }
- }
它最重要的两个方法就是在代码中提到的
void |
setOnDrawerCloseListener(
SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)
Sets the listener that receives a notification when the drawer becomes close.
|
void |
setOnDrawerOpenListener(
SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)
Sets the listener that receives a notification when the drawer becomes open.
|
ExpandableListView
喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进,素材采用了Q版三国杀武将的图片,很有爱哈哈,下面直接上效果图以及源代码~!
main.xml的布局很简单啦,只是一个ExpandableListView 就OK了
但值得简单说下的是 android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
android:listSelector="#00000000" ,可以去除选中时的黄色底色
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:orientation="vertical" >
6 <ExpandableListView
7 android:id="@+id/list"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:background="#ffffff"
11 android:cacheColorHint="#00000000"
12 android:listSelector="#00000000"
13 >
14 </ExpandableListView>
15 </LinearLayout>
16
java代码:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableList extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
// 设置组视图的图片
int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};
// 设置组视图的显示文字
private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };
// 子视图显示文字
private String[][] generals = new String[][] {
{ "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },
{ "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },
{ "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }
};
// 子视图图片
public int[][] generallogos = new int[][] {
{ R.drawable.xiahoudun, R.drawable.zhenji,
R.drawable.xuchu, R.drawable.guojia,
R.drawable.simayi, R.drawable.yangxiu },
{ R.drawable.machao, R.drawable.zhangfei,
R.drawable.liubei, R.drawable.zhugeliang,
R.drawable.huangyueying, R.drawable.zhaoyun },
{ R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
R.drawable.zhouyu, R.drawable.sunshangxiang } };
// 自己定义一个获得文字信息的方法
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(
ExpandableList. this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
// 重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return generalsTypes.length;
}
@Override
public Object getGroup( int groupPosition) {
// TODO Auto-generated method stub
return generalsTypes[groupPosition];
}
@Override
public long getGroupId( int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getChildrenCount( int groupPosition) {
// TODO Auto-generated method stub
return generals[groupPosition].length;
}
@Override
public Object getChild( int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return generals[groupPosition][childPosition];
}
@Override
public long getChildId( int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView( int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
ExpandableList. this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableList. this);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(50, 0, 0, 0);
ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public View getChildView( int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
ExpandableList. this);
ll.setOrientation(0);
ImageView generallogo = new ImageView(
ExpandableList. this);
generallogo
.setImageResource(generallogos[groupPosition][childPosition]);
ll.addView(generallogo);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable( int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
};
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setAdapter(adapter);
// 设置item点击的监听器
expandableListView.setOnChildClickListener( new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
ExpandableList. this,
"你点击了" + adapter.getChild(groupPosition, childPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}