这是四年前的项目,手机卫士的主界面很简单,整体也就是一个GridView,启动模式设置为单利模式。
GridView gv_home = (GridView) findViewById(R.id.gv_home);
gv_home.setAdapter(new HomeAda(Activity.this));
class HomeAda extends BaseAdapter {
private Context context;
public HomeAda(Context context) {
this.context = context;
}
private static final String[] names = { "手机防盗", "通讯卫士", "软件管理", "进程管理",
"流量统计", "手机杀毒", "系统优化", "高级工具", "系统设置" };
private static final int[] icons = { R.drawable.safe_selector,
R.drawable.callmsgsafe, R.drawable.app, R.drawable.taskmanager,
R.drawable.netmanager, R.drawable.trojan, R.drawable.sysoptimize,
R.drawable.atools, R.drawable.settings };
@Override
public int getCount() {
return names.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 把好看的布局 转化成 view对象.
View view = View.inflate(context, R.layout.list_home_item, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_home_name_item);
ImageView iv_icon = (ImageView) view
.findViewById(R.id.iv_home_icon_item);
tv_name.setText(names[position]);
iv_icon.setImageResource(icons[position]);
return view;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
适配器有几处可以优化的地方,例如
1 字符串数组可以写在 res/valuse文件夹下的string.xml文件里,
<string-array name = "home_item_name">
<item> 手机防盗</item>
<item> 通讯卫士</item>
<item> 软件管理</item>
<item> 进程管理</item>
</string-array>
代码里面获取该数组资源
String[] itemNames = context.getResource().getStringArray(R.array.home_item_name);
同理,数字数组也可以用同样的方法。
<string-array name=
"actions_images"
>
<item>
@drawable
/pencil1</item>
<item>
@drawable
/pencil2</item>
<item>
@drawable
/pencil3</item>
<item>
@drawable
/pencil4</item>
<item>
@drawable
/pencil5</item>
<item>
@drawable
/pencil6</item>
</string-array>
TypedArray ar = context.getResources().obtainTypedArray(R.array.actions_images);
int
len = ar.length();
int
[] icons =
new
int
[len];
for
(
int
i =
0
; i < len; i++)
icons [i] = ar.getResourceId(i,
0
);
ar.recycle();
数字和上述一样。
2 获取item的个数,要逻辑清晰,不能产生空指针
public int getCount() {
int count = 0;
if(names !=null && names.length>0){
count = names.length;
}
return count;
}
3 生成view的方法,可以通用步骤优化
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
// 把好看的布局 转化成 view对象.
if(convertView == null){
viewHolder = new ViewHolder();
convertView = View.inflate(context, R.layout.list_home_item, null);
viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_home_name_item);
viewHolder.iv_icon = (ImageView) convertView
.findViewById(R.id.iv_home_icon_item);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_name.setText(names[position]);
viewHolder.iv_icon.setImageResource(icons[position]);
return convertView;
}
class ViewHolder{
public TextView tv_name;
public ImageView iv_icon;
}
一个是布局复用,避免每次都创建新的,节省内存;
第二个是viewHolder的建立,用对象来保存控件的id值,避免每次都重新findViewById ,进一步节省内存开支。
item的点击阴影效果,可以配置xml布局的根节点的背景色。
android:background="@drawable/home_item_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@color/gray" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@android:color/transparent"/>
<!-- 默认 -->
</selector>
UI界面跳转,只需注册 条目点击事件即可。
5.0以后,google用 RecyclerView 代替了 Listview,可以多看看。 listview是复用item,在item中保存viewHolder, 而 recyclerview则是复用viewholder,在viewholder中保存item。