为了账号安全,请及时绑定邮箱和手机立即绑定

android怎么实现类似滑动式抽屉的效果

标签:
Android

  今天在手机上实现了抽屉效果,其实很简单,但是效果却很酷。

           首先在layout 下设置xml布局文件 

       


  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="fill_parent"  

  4.     android:layout_height="fill_parent" >  

  5.   

  6.     <SlidingDrawer  

  7.         android:id="@+id/sliding"  

  8.         android:layout_width="match_parent"  

  9.         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>  


        SlidingDrawer就是重要的抽屉控件 ,handle是抽屉的拖动按钮,content是抽屉中的内容。


      然后建立 chouti的activity类:


[html] view plain copy

  1. import android.app.Activity;  

  2. import android.content.Intent;  

  3. import android.content.pm.ResolveInfo;  

  4. import android.os.Bundle;  

  5. import android.view.View;  

  6. import android.view.ViewGroup;  

  7. import android.widget.BaseAdapter;  

  8. import android.widget.GridView;  

  9. 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. }  


loadApps方法是得到主界面上的图片和文字。


   然后设置的自定义adapter中去。

 

    

    为了体现更好的效果,可以用两张滑动图片,一张朝上的,一张朝下的。根据监听器做相应的切换。

原文链接:http://www.apkbus.com/blog-914653-68369.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消