Android 抽屉

Android 抽屉_第1张图片



MainActivity.java

package com.xzqsdta;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class MainActivity extends Activity {

    private GridView gv;
    private SlidingDrawer sd;
    private ImageView iv;
    private List<ResolveInfo> apps;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        findViewById();
        loadApps();
        processBiz();
        addListener();

    }

    /**
     * 初始化控件
     */
    private void findViewById() {
        gv = (GridView) findViewById(R.id.allApps);
        sd = (SlidingDrawer) findViewById(R.id.sliding);
        iv = (ImageView) findViewById(R.id.imageViewIcon);
    }

    /**
     * 查詢系統的app信息
     */
    private void loadApps() {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        apps = getPackageManager().queryIntentActivities(intent, 0);
    }

    /**
     * 主业务逻辑处理
     */
    private void processBiz() {
        GridAdapter adapter = new GridAdapter(this, apps);
        gv.setAdapter(adapter);
    }

    /**
     * 添加监听事件
     */
    private void addListener() {

        sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
            public void onDrawerOpened() {
                iv.setImageResource(R.drawable.ic_launcher1);
            }
        });

        sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {
            public void onDrawerClosed() {
                iv.setImageResource(R.drawable.ic_launcher);
            }
        });

        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ResolveInfo res = apps.get(position);
                String pkg = res.activityInfo.packageName;
                String cls = res.activityInfo.name;
                ComponentName componet = new ComponentName(pkg, cls);
                Intent i = new Intent();
                i.setComponent(componet);
                startActivity(i);
            }
        });
    }
}

GridAdapter.java

package com.xzqsdta;

import java.util.List;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridAdapter extends BaseAdapter {

    private Context context;
    private List<ResolveInfo> apps;

    public GridAdapter(Context context, List<ResolveInfo> apps) {
        this.context = context;
        this.apps = apps;
    }

    @Override
    public int getCount() {
        return apps.size();
    }

    @Override
    public Object getItem(int position) {
        return apps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
        } else {
            imageView = (ImageView) convertView;
        }
        ResolveInfo ri = apps.get(position);
        imageView.setImageDrawable(ri.activityInfo.loadIcon(context.getPackageManager()));
        return imageView;
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sliding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:content="@+id/allApps"
    android:handle="@+id/imageViewIcon"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/allApps"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:fastScrollEnabled="true"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

    <ImageView
        android:id="@+id/imageViewIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#d0d7df"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" />

</SlidingDrawer>



你可能感兴趣的:(Android 抽屉)