您的评价: |
|
今天在手机上实现了抽屉效果,其实很简单,但是效果却很酷。
首先在layout 下设置xml布局文件
SlidingDrawer就是重要的抽屉控件 ,handle是抽屉的拖动按钮,content是抽屉中的内容。
loadApps方法是得到主界面上的图片和文字。
首先在layout 下设置xml布局文件
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "fill_parent" |
04 | android:layout_height = "fill_parent" > |
05 |
06 | < SlidingDrawer |
07 | android:id = "@+id/sliding" |
08 | android:layout_width = "match_parent" |
09 | android:layout_height = "match_parent" |
10 | android:content = "@+id/allApps" |
11 | android:handle = "@+id/imageViewIcon" |
12 | android:orientation = "vertical" > |
13 |
14 | < GridView |
15 | android:id = "@+id/allApps" |
16 | android:layout_width = "wrap_content" |
17 | android:layout_height = "wrap_content" |
18 | android:background = "@drawable/bk" |
19 | android:columnWidth = "60dp" |
20 | android:gravity = "center" |
21 | android:horizontalSpacing = "10dp" |
22 | android:numColumns = "auto_fit" |
23 | android:padding = "10dp" |
24 | android:stretchMode = "columnWidth" |
25 | android:verticalSpacing = "10dp" /> |
26 |
27 | < ImageView |
28 | android:id = "@+id/imageViewIcon" |
29 | android:layout_width = "wrap_content" |
30 | android:layout_height = "wrap_content" |
31 | android:src = "@drawable/touch_handler" /> |
32 | </ SlidingDrawer > |
33 |
34 | </ RelativeLayout > |
然后建立 chouti的activity类:
01 | import android.app.Activity; |
02 | import android.content.Intent; |
03 | import android.content.pm.ResolveInfo; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.view.ViewGroup; |
07 | import android.widget.BaseAdapter; |
08 | import android.widget.GridView; |
09 | import android.widget.ImageView; |
10 | import android.widget.SlidingDrawer; |
11 |
12 | public class Chouti extends Activity { |
13 | private GridView gv; |
14 | private SlidingDrawer sd; |
15 | private ImageView iv; |
16 | private List<ResolveInfo> apps; |
17 |
18 | /** Called when the activity is first created. */ |
19 | @Override |
20 | public void onCreate(Bundle savedInstanceState) { |
21 | super .onCreate(savedInstanceState); |
22 | setContentView(R.layout.slidingdrawer); |
23 | loadApps(); |
24 | gv = (GridView) findViewById(R.id.allApps); |
25 | sd = (SlidingDrawer) findViewById(R.id.sliding); |
26 | iv = (ImageView) findViewById(R.id.imageViewIcon); |
27 | gv.setAdapter( new GridAdapter()); |
28 | sd.setOnDrawerOpenListener( new SlidingDrawer.OnDrawerOpenListener() // 开抽屉 |
29 | { |
30 | @Override |
31 | public void onDrawerOpened() { |
32 | iv.setImageResource(R.drawable.touch_handler); // 响应开抽屉事件 |
33 | // ,把图片设为向下的 |
34 | } |
35 | }); |
36 | sd.setOnDrawerCloseListener( new SlidingDrawer.OnDrawerCloseListener() { |
37 | @Override |
38 | public void onDrawerClosed() { |
39 | iv.setImageResource(R.drawable.touch_handler); // 响应关抽屉事件 |
40 | } |
41 | }); |
42 | } |
43 |
44 | private void loadApps() { |
45 | Intent intent = new Intent(Intent.ACTION_MAIN, null ); |
46 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
47 |
48 | apps = getPackageManager().queryIntentActivities(intent, 0 ); |
49 | } |
50 |
51 | public class GridAdapter extends BaseAdapter { |
52 | public GridAdapter() { |
53 |
54 | } |
55 |
56 | public int getCount() { |
57 | // TODO Auto-generated method stub |
58 | return apps.size(); |
59 | } |
60 |
61 | public Object getItem( int position) { |
62 | // TODO Auto-generated method stub |
63 | return apps.get(position); |
64 | } |
65 |
66 | public long getItemId( int position) { |
67 | // TODO Auto-generated method stub |
68 | return position; |
69 | } |
70 |
71 | public View getView( int position, View convertView, ViewGroup parent) { |
72 | // TODO Auto-generated method stub |
73 | ImageView imageView = null ; |
74 | if (convertView == null ) { |
75 | imageView = new ImageView(Chouti. this ); |
76 | imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); |
77 | imageView.setLayoutParams( new GridView.LayoutParams( 50 , 50 )); |
78 | } else { |
79 | imageView = (ImageView) convertView; |
80 | } |
81 |
82 | ResolveInfo ri = apps.get(position); |
83 | imageView.setImageDrawable(ri.activityInfo |
84 | .loadIcon(getPackageManager())); |
85 |
86 | return imageView; |
87 | } |
88 |
89 | } |
90 | } |
然后设置的自定义adapter中去。
看下运行后效果:
向上滑动imageview按钮后:
为了体现更好的效果,可以用两张滑动图片,一张朝上的,一张朝下的。根据监听器做相应的切换。