SlidingDrawer--抽屉效果

第一次接触抽屉效果,感觉蛮好的,记录一下,首先是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/back"
    >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:padding="@dimen/padding_medium"
        android:text="SlidingDrawerDemo"
        android:textStyle="bold"
        android:textSize="30dp"
        tools:context=".MainActivity" />
    <SlidingDrawer
        android:id="@+id/slidingDrawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:handle="@+id/handle"
        android:content="@+id/content"
        android:layout_alignParentBottom="true"
        >
        <Button
            android:id="@+id/handle"
            android:text=""
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/open"
            android:textSize="25dp"
            />
        <GridView
            android:id="@+id/content"
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"   
            android:columnWidth="60dp"  
            android:gravity="center"  
            android:horizontalSpacing="10dp"  
            android:numColumns="auto_fit"  
            android:padding="10dp"  
            android:stretchMode="columnWidth"  
            android:verticalSpacing="10dp"
            />
    </SlidingDrawer>

</RelativeLayout>

有两个属性需要注意、handle属性是点击的按钮、content属性是抽屉的内容,两个属性要与下面的相对应

activity很简单,显示所有应用程序

package com.example.slidingdrawerdemo;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;

public class MainActivity extends Activity {
	private GridView gv = null;
	private SlidingDrawer sd;  //抽屉效果
	private Button iv;  
	private List<ResolveInfo> apps; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Intent intent = new Intent(Intent.ACTION_MAIN, null);  
        intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  
        apps = getPackageManager().queryIntentActivities(intent, 0);  //获取应用下所有activity
        
        gv = (GridView) findViewById(R.id.content);  
        sd = (SlidingDrawer) findViewById(R.id.slidingDrawer);  
        iv = (Button) findViewById(R.id.handle);  
        gv.setAdapter(new GridAdapter());  
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 开抽屉  
        {  
              
            public void onDrawerOpened() {  
                iv.setBackgroundResource(R.drawable.open);// 响应开抽屉事件  
                                                                // ,把图片设为向下的  
            }  
        });  
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {  
              
            public void onDrawerClosed() 
            {  
                iv.setBackgroundResource(R.drawable.open);// 响应关抽屉事件  
            }  
        });  
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public class GridAdapter extends BaseAdapter {  
        public GridAdapter() 
        {  
        }  
  
        public int getCount() 
        {  
            // TODO Auto-generated method stub  
            return apps.size();  
        }  
  
        public Object getItem(int position) {  
            // TODO Auto-generated method stub  
            return apps.get(position);  
        }  
  
        public long getItemId(int position) {  
            // TODO Auto-generated method stub  
            return position;  
        }  
  
        public View getView(int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            ImageView imageView = null;  
            if (convertView == null) 
            {  
                imageView = new ImageView(MainActivity.this);  
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
                imageView.setLayoutParams(new GridView.LayoutParams(50, 50));  
            } 
            else 
            {  
                imageView = (ImageView) convertView;  
            }  
  
            ResolveInfo ri = apps.get(position);  
            imageView.setImageDrawable(ri.activityInfo  
                    .loadIcon(getPackageManager()));  
  
            return imageView;  
        }  
  
    }  
    
}

尝试一下吧!

参考文献:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=234598

你可能感兴趣的:(SlidingDrawer--抽屉效果)