[代码例程] Android效果之 抽屉效果
在Android开发过程中,我们喜欢使用特效,比如抽屉效果,这样可以给人很好的体验。点击一个按钮,就像拉抽屉一样展开界面,这样的效果正是我在这里所要说明的。比如在AVD或真机上,我们都有看过这种效果。比较常用的应用是LAUNCH应用。在这个应用中我们实现了拉抽屉呈现所有的程序。那么我们就来开始实现这个让我们梦寐以求的效果吧。我们先创建一个工程。在这里我命名为drawerDemo.在这个例子中我在main.xml文件中写入如下,那么大家就来看看代码吧:
Java代码:<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SlidingDrawer
android:id="@+id/slidingdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:handle="@+id/handle"
android:content="@+id/content">
<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>
复制代码看到这个SlidingDrawer控件。就像使用Button,TextView一样简单使用。然后在LauncherActivity.java代码如下:
Java代码:publicclassLauncherActivityextendsActivityimplementsOnItemClickListener{
privateGridViewmGridView;
privateContextmContext;
privatePackageManagermPackageManager;
privateList<ResolveInfo>mAllApps;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
publicvoidsetupViews(){
mContext=LauncherActivity.this;
mPackageManager=getPackageManager();//包管理器
mGridView=(GridView)findViewById(R.id.allapps);
bindAllApps();
mGridView.setAdapter(newGridItemAdapter(mContext,mAllApps));
mGridView.setNumColumns(4);
mGridView.setOnItemClickListener(this);
}
publicvoidbindAllApps(){
//这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性
//也就是应用的入口
IntentmainIntent=newIntent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//符合上面条件的全部查出来,并且排序
mAllApps=mPackageManager.queryIntentActivities(mainIntent,0);
Collections.sort(mAllApps,newResolveInfo.DisplayNameComparator(mPackageManager));
}
//gridview点击事件,点击进入相关应用
@Override
publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,longid){
//TODOAuto-generatedmethodstub
ResolveInfores=mAllApps.get(position);
//该应用的包名和主Activity
Stringpkg=res.activityInfo.packageName;
Stringcls=res.activityInfo.name;
ComponentNamecomponet=newComponentName(pkg,cls);
Intenti=newIntent();
i.setComponent(componet);
startActivity(i);
}
//不明白BaseAdapter的用法我高手进阶里有
privateclassGridItemAdapterextendsBaseAdapter{
privateContextcontext;
privateList<ResolveInfo>resInfo;
//构造函数
publicGridItemAdapter(Contextc,List<ResolveInfo>res){
context=c;
resInfo=res;
}
@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returnresInfo.size();
}
@Override
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnnull;
}
@Override
publiclonggetItemId(intposition){
//TODOAuto-generatedmethodstub
return0;
}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//不明白LayoutInflater的我android高手进阶里有
convertView=LayoutInflater.from(context).inflate(R.layout.application_layout,null);
ImageViewapp_icon=(ImageView)convertView.findViewById(R.id.app_icon);
TextViewapp_tilte=(TextView)convertView.findViewById(R.id.app_title);
ResolveInfores=resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
returnconvertView;
}
}
}