Android中常见的MVC模式
2014-04-10 11:09
344 查看
MVC模式的简要介绍
MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller)。 MVC模式的目的就是实现Web系统的职能分工。 Model层实现系统中的业务逻辑。 View层用于与用户的交互。 Controller层是Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作。
Android中的Launcher
通过查看Android中的Launcher的源码,大家会发现其中会有[b]LauncherModel.java,[b]Workspace.java,[b]Launcher.java。[/b][/b][/b]
其中LauncherModel为辅助文件封装了许多对数据库的操作(对应MVC中的Model);Workspace为一个抽象的桌面,将应用显示在用户面前,与用户进行交互(对应MVC中的View);Launcher是主要的Activity,里面有很多对用户的操作进行处理,并且将结果反馈在Workspace中(对应MVC中的Controller)。
Android中的其它View
在Android中有常见的ListView,GridView,Gallery等等一些控件能够很好的体现MVC模式,下面将一GridView显示设备上的所有应用为例子讲解MVC模式的应用。先上图。
在main.xml的布局文件中定义GridView:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView android:id="@+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:numColumns="5"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
顾名思义,GridView就是MVC中的View负责显示。
获取设备上安装的应用信息所有对应的方法,这就是对应的Model。
[java] view plaincopy
public 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));
}
方法中 mAllApps对应的类型为List<ResolveInfo> 。
在该例子中最要的就是Adapter,是Model和View中的桥梁,就是 Controller。
[java] view plaincopy
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() {
// TODO Auto-generated method stub
return resInfo.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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);
ResolveInfo res = resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
return convertView;
}
}
MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller)。 MVC模式的目的就是实现Web系统的职能分工。 Model层实现系统中的业务逻辑。 View层用于与用户的交互。 Controller层是Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作。
Android中的Launcher
通过查看Android中的Launcher的源码,大家会发现其中会有[b]LauncherModel.java,[b]Workspace.java,[b]Launcher.java。[/b][/b][/b]
其中LauncherModel为辅助文件封装了许多对数据库的操作(对应MVC中的Model);Workspace为一个抽象的桌面,将应用显示在用户面前,与用户进行交互(对应MVC中的View);Launcher是主要的Activity,里面有很多对用户的操作进行处理,并且将结果反馈在Workspace中(对应MVC中的Controller)。
Android中的其它View
在Android中有常见的ListView,GridView,Gallery等等一些控件能够很好的体现MVC模式,下面将一GridView显示设备上的所有应用为例子讲解MVC模式的应用。先上图。
在main.xml的布局文件中定义GridView:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView android:id="@+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:numColumns="5"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
顾名思义,GridView就是MVC中的View负责显示。
获取设备上安装的应用信息所有对应的方法,这就是对应的Model。
[java] view plaincopy
public 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));
}
方法中 mAllApps对应的类型为List<ResolveInfo> 。
在该例子中最要的就是Adapter,是Model和View中的桥梁,就是 Controller。
[java] view plaincopy
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() {
// TODO Auto-generated method stub
return resInfo.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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);
ResolveInfo res = resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
return convertView;
}
}
相关文章推荐
- Android中常见的MVC模式
- Android中常见的MVC模式
- Android中常见的MVC模式
- Android中Intent的各种常见作用
- android中进入设置页面(常见网络设置页面,设置页面,蓝牙页面等等)的action
- Android布局常见异常错误
- Android开发常见错误-ing
- Android开发中的MVC模式理解
- Android开发面试经——3.常见Java基础笔试题
- Android常见的漏洞与分析(阿里聚安全博客文章)
- [zz]Android常见错误处理
- 常见的Android 网络请求的封装;
- Android的几个常见小错误ZZ
- android关于导入工程报错、常见问题解决
- Android开发面试经——1.常见人事面试问题
- Android中的颜色设置和常见RGB颜色表
- 老司机带路——15个Android撸代码常见的坑
- Android 常见的四种对话框实例讲解
- Android中常见的正则表达式工具类