android 抽屉 效果

1.   main.xml 里面

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


<!-- 这里可以指定抽屉的方向-->

<!-- 其中  content 对应的就是下面 LInearLayout 的id

     handler  对应的就是下面的一个按钮

-->
    <SlidingDrawer
        android:id="@+id/slidingdrawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/handle"
            android:layout_width="88dip"
            android:layout_height="44dip"
            android:background="@drawable/handle" />


        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00ff00" >


            <GridView
                android:id="@+id/allapps"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </SlidingDrawer>


</LinearLayout>


2.   主要的activity

   public class LauncherDemoActivity extends Activity implements
OnItemClickListener
{
private GridView mGridView;
private Context mContext;
private PackageManager mPackageManager;
private List<ResolveInfo> mAllApps;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


setupViews();
}


private void setupViews()
{
mContext = LauncherDemoActivity.this;
mPackageManager = getPackageManager(); // 包管理器
mGridView = (GridView) findViewById(R.id.allapps);

bindAllApps();
mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));
mGridView.setNumColumns(4);
mGridView.setOnItemClickListener(this);
}


private void bindAllApps()
{
// 这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性
// 也就是应用的入口
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);


// 符合上面条件的全部查出来,并且排序
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(
mPackageManager));
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id)
{
ResolveInfo res = mAllApps.get(position);


// 该应用的包名和主Activity
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);
}


// 不明白BaseAdapter的用法 我高手进阶里有
private class GridItemAdapter extends BaseAdapter
{
private Context context;
private List<ResolveInfo> resInfo;


// 构造函数
public GridItemAdapter(Context c, List<ResolveInfo> res)
{
context = c;
resInfo = res;
}


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


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


@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ResolveInfo res = resInfo.get(position);
if (null == convertView)
{
// 不明白LayoutInflater的我android高手进阶里有
convertView = LayoutInflater.from(context).inflate(
R.layout.application_layout, null);


}
ImageView app_icon = (ImageView) convertView
.findViewById(R.id.app_icon);
TextView app_tilte = (TextView) convertView
.findViewById(R.id.app_title);


app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
return convertView;
}

}
}

  

3.   注意:

     根据intent  进行查找 并进行排序:

    // 这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性
// 也就是应用的入口
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);


// 符合上面条件的全部查出来,并且排序
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(
mPackageManager));




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值