android抽屉效果的实现

抽屉效果的实现主要依靠SlidingDrawer控件,下面为xml代码

 
  
  
  
  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical" > 
  5.  
  6.     <SlidingDrawer 
  7.         android:id="@+id/slidingDrawer1" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:orientation="horizontal" 
  11.         android:content="@+id/content" 
  12.         android:handle="@+id/handle" > 
  13.  
  14.         <Button 
  15.             android:id="@+id/handle" 
  16.             android:layout_width="wrap_content" 
  17.             android:layout_height="wrap_content" 
  18.             android:text="Handle" /> 
  19.  
  20.         <LinearLayout 
  21.             android:id="@+id/content" 
  22.             android:layout_width="match_parent" 
  23.             android:layout_height="match_parent" > 
  24.             <GridView android:id="@+id/gridview" 
  25.                 android:layout_width="wrap_content" 
  26.                 android:layout_height="wrap_content" 
  27.                 android:numColumns="4"></GridView> 
  28.         </LinearLayout> 
  29.     </SlidingDrawer> 
  30.  
  31. </LinearLayout> 

下面为java代码:

 
  
  
  
  
  1. package com.example.chouti; 
  2.  
  3. import java.util.List; 
  4.  
  5. import android.os.Bundle; 
  6. import android.app.Activity; 
  7. import android.content.ComponentName; 
  8. import android.content.Context; 
  9. import android.content.Intent; 
  10. import android.content.pm.PackageManager; 
  11. import android.content.pm.ResolveInfo; 
  12. import android.view.LayoutInflater; 
  13. import android.view.Menu; 
  14. import android.view.View; 
  15. import android.view.ViewGroup; 
  16. import android.widget.AdapterView; 
  17. import android.widget.AdapterView.OnItemClickListener; 
  18. import android.widget.AdapterView.OnItemSelectedListener; 
  19. import android.widget.BaseAdapter; 
  20. import android.widget.GridView; 
  21. import android.widget.ImageView; 
  22. import android.widget.TextView; 
  23.  
  24. public class MainActivity extends Activity { 
  25.     GridView gridView; 
  26.     List<ResolveInfo> mAppInfos; 
  27.     Context context; 
  28.     private PackageManager packageManager; 
  29.     @Override 
  30.     protected void onCreate(Bundle savedInstanceState) { 
  31.         super.onCreate(savedInstanceState); 
  32.         setContentView(R.layout.activity_main); 
  33.         gridView=(GridView) findViewById(R.id.gridview); 
  34.         getApp(); 
  35.         GridItemAdapter adapter=new GridItemAdapter(context,mAppInfos); 
  36.         gridView.setAdapter(adapter); 
  37.          
  38.         gridView.setOnItemClickListener(new OnItemClickListener() { 
  39.             //点击进入相关应用 
  40.             @Override 
  41.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
  42.                     long arg3) { 
  43.                 // TODO Auto-generated method stub 
  44.                 ResolveInfo resolveInfo=mAppInfos.get(arg2); 
  45.                 //得到包名和class名 
  46.                 String pkn=resolveInfo.activityInfo.packageName; 
  47.                 String cln=resolveInfo.activityInfo.name; 
  48.                 //已知某应用的包名跟类名后,启动该特定的应用 
  49.                 Intent intent=new Intent(); 
  50.                 ComponentName componentName=new ComponentName(pkn, cln); 
  51.                 intent.setComponent(componentName); 
  52.                 startActivity(intent); 
  53.             } 
  54.         }); 
  55.     } 
  56.     private void getApp() { 
  57.         // TODO Auto-generated method stub 
  58.     //包管理器 
  59.     packageManager=getPackageManager(); 
  60.     Intent intent=new Intent(Intent.ACTION_MAIN); 
  61.     intent.addCategory(Intent.CATEGORY_LAUNCHER); 
  62.     mAppInfos=packageManager.queryIntentActivities(intent, 0); 
  63.     } 
  64.      
  65.     class GridItemAdapter extends BaseAdapter{ 
  66.         Context context; 
  67.         List<ResolveInfo> mAppInfos; 
  68.          
  69.     public GridItemAdapter(Context context, List<ResolveInfo> mAppInfos) { 
  70.             super(); 
  71.             this.context = context; 
  72.             this.mAppInfos = mAppInfos; 
  73.         } 
  74.  
  75.     @Override 
  76.     public int getCount() { 
  77.         // TODO Auto-generated method stub 
  78.         return mAppInfos.size(); 
  79.     } 
  80.  
  81.     @Override 
  82.     public Object getItem(int position) { 
  83.         // TODO Auto-generated method stub 
  84.         return position; 
  85.     } 
  86.  
  87.     @Override 
  88.     public long getItemId(int position) { 
  89.         // TODO Auto-generated method stub 
  90.         return position; 
  91.     } 
  92.  
  93.     @Override 
  94.     public View getView(int position, View convertView, ViewGroup parent) { 
  95.         // TODO Auto-generated method stub 
  96.          
  97.         ResolveInfo resolveInfo=mAppInfos.get(position); 
  98.         LayoutInflater layoutInflater=getLayoutInflater(); 
  99.         View convertView1=layoutInflater.inflate(R.layout.item, null); 
  100.         ImageView imageView=(ImageView)convertView1.findViewById(R.id.imageView1); 
  101.         TextView textView=(TextView)convertView1. findViewById(R.id.textView1); 
  102.         imageView.setImageDrawable(resolveInfo.loadIcon(packageManager)); 
  103.         textView.setText(resolveInfo.loadLabel(packageManager)); 
  104.         return convertView1; 
  105.          
  106.     } 
  107.      

效果图

android抽屉效果的实现

你可能感兴趣的:(android抽屉效果的实现)