Android中常见的MVC模式

MVC模式的简要介绍

MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller)。 MVC模式的目的就是实现Web系统的职能分工。 Model层实现系统中的业务逻辑。 View层用于与用户的交互。 Controller层是Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作。


Android中的Launcher

通过查看Android中的Launcher的源码,大家会发现其中会有LauncherModel.java,Workspace.java,Launcher.java。

其中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
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <GridView android:id="@+id/myGrid"      
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="fill_parent"  
  10.     android:padding="10dip"  
  11.     android:verticalSpacing="10dip"  
  12.     android:horizontalSpacing="10dip"  
  13.     android:numColumns="5"  
  14.     android:stretchMode="columnWidth"  
  15.     android:gravity="center"  
  16.     />   
  17. </LinearLayout>  

 

顾名思义,GridView就是MVC中的View负责显示。

获取设备上安装的应用信息所有对应的方法,这就是对应的Model。

[java] view plaincopy
 
  1. public void bindAllApps(){  
  2.        //这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性  
  3.        //也就是应用的入口  
  4.        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  5.        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  6.        //符合上面条件的全部查出来,并且排序  
  7.        mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);  
  8.        Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));  
  9.    }  

方法中 mAllApps对应的类型为List<ResolveInfo> 。

 

在该例子中最要的就是Adapter,是Model和View中的桥梁,就是 Controller。

 

[java] view plaincopy
 
    1. private class GridItemAdapter extends BaseAdapter{  
    2.        private Context context;  
    3.        private List<ResolveInfo> resInfo;  
    4.          
    5.        //构造函数  
    6.        public GridItemAdapter(Context c,List<ResolveInfo> res){  
    7.            context = c;  
    8.            resInfo = res;  
    9.        }  
    10.        @Override  
    11.        public int getCount() {  
    12.            // TODO Auto-generated method stub  
    13.            return resInfo.size();  
    14.        }  
    15.        @Override  
    16.        public Object getItem(int position) {  
    17.            // TODO Auto-generated method stub  
    18.            return null;  
    19.        }  
    20.        @Override  
    21.        public long getItemId(int position) {  
    22.            // TODO Auto-generated method stub  
    23.            return 0;  
    24.        }  
    25.        @Override  
    26.        public View getView(int position, View convertView, ViewGroup parent) {  
    27.              
    28.            convertView = LayoutInflater.from(context)  
    29.            .inflate(R.layout.application_layout, null);  
    30.              
    31.            ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon);  
    32.            TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title);  
    33.              
    34.            ResolveInfo res = resInfo.get(position);  
    35.            app_icon.setImageDrawable(res.loadIcon(mPackageManager));  
    36.            app_tilte.setText(res.loadLabel(mPackageManager).toString());  
    37.            return convertView;  
    38.        }  
    39.          
    40.    }  
posted @ 2014-04-10 11:09  tfy1332  阅读(248)  评论(0编辑  收藏  举报