转载请注明出处:http://write.blog.csdn.net/mdeditor
这是我的第一篇博客,之前一直在映像笔记做了一些自己的不懂得的地方的总结和别人一些好的文章,但是有一些不足之处就是不能和别人分享,讨论。最近看了一些大牛的博客,感触颇多,技术在于分享,讨论。但愿在自己工作中遇到的问题总结,能给大家一些帮助。我写的不足之处,请大家多指点。
最近公司做了一个项目要显示这样的布局
项目经理给我原型图时,我看到这个布局,首先想到用GridView, GridView 之前也用过,但是里面布局基本一样,没有做过这样的。 还要 点击添加按键 需要动态添加 家庭。后来想到办法了在BaseAdapter的方法中
public View getView(final int position, View convertView, ViewGroup parent)
判断类型,加载不同的xml布局,很快就实现这个功能了。很是高兴,但是在测试人员测试的时候发现一个问题,加载到第9个时,会崩溃。我自己复现了一下bug,找到打印日志。if(convertView == null) 没有进去 ,而是复用之前的convertView。这个问题最终发现是少重写这2个方法
•重 getViewTypeCount() – 返回你有多少个不同的布局
•重写 getItemViewType(int) – 由position返回view type id
这个2个方法之前没用过,引起重视。
不多说了附上代码
1.这个是适配器的
public class home_page_adapter extends BaseAdapter{
private List<Map<String,Object>> mList;
private Context mContext;
private int mCurrentType;
private LayoutInflater inflater;
public home_page_adapter(Context context, List<Map<String,Object>> list)
{
super();
// TODO Auto-generated constructor stub
mContext = context;
mList = list;
}
@Override
public int getItemViewType(int position) {
return Integer.parseInt(mList.get(position).get("Type").toString());
}
@Override
public int getViewTypeCount() {
return Constants_Type.TypeCount;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
if(position>=getCount()){
return null;
}
return position;
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder
{
TextView mGroupName;
TextView mUserName;
TextView mHomeName;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder mViewHolder = null;
Map<String,Object> map = mList.get(position);
mCurrentType = Integer.parseInt(map.get("Type").toString());
if(convertView == null)
{
mViewHolder = new ViewHolder();
switch(mCurrentType)
{
case Constants_Type.AddType:
convertView = LayoutInflater.from(mContext).inflate(R.layout.home_page_add_adapter, parent, false);
break;
case Constants_Type.InviteType:
convertView = LayoutInflater.from(mContext).inflate(R.layout.home_page_invite_adapter, parent,false);
mViewHolder.mGroupName = (TextView) convertView.findViewById(R.id.invite_adater_home_name);
mViewHolder.mUserName = (TextView) convertView.findViewById(R.id.invite_adater_people_name);
mViewHolder.mGroupName.setTag(position);
mViewHolder.mUserName.setTag(position);
break;
case Constants_Type.HomeType:
convertView = LayoutInflater.from(mContext).inflate(R.layout.home_page_home_adapter, parent, false);
mViewHolder.mHomeName = (TextView) convertView.findViewById(R.id.home_name);
mViewHolder.mHomeName.setTag(position);
break;
default:
break;
}
convertView.setTag(mViewHolder);
}
else
{
mViewHolder = (ViewHolder) convertView.getTag();
}
switch (Integer.parseInt(map.get("Type").toString()))
{
case Constants_Type.AddType:
Log.e("AddType ", " position "+position);
break;
case Constants_Type.InviteType:
mViewHolder.mGroupName.setText(map.get("group_name").toString());
mViewHolder.mUserName.setText("来自 "+map.get("user_name").toString());
Log.e("InviteType ", " position "+position);
break;
case Constants_Type.HomeType:
String mGroupName = map.get("group_name").toString();
mViewHolder.mHomeName.setText(mGroupName);
break;
default:
break;
}
return convertView;
}
2.样式类型定义
public class Constants_Type
{
public static final int HomeType = 0;
public static final int InviteType = 1;
public static final int AddType = 2;
public static final int TypeCount =3;
}
3 初始化数据
/* 数据初始化 */
private void initData()
{
/*
* 邀请函列表
*/
mInviteList = LookApp.the().getCore().getmListInvite();
for(AvInviteListItem mInviteListItem : mInviteList)
{
Map<String,Object> map = new HashMap<String,Object>();
map.put("group_id", mInviteListItem.getmStrGroupId());
map.put("group_name", mInviteListItem.getmStrGroupName());
map.put("user_name", mInviteListItem.getmStrUserName());
map.put("Type", Constants_Type.InviteType);
mHomeList.add(map);
}
/*
* 创建家庭圈
*/
Map<String,Object> map = new HashMap<String,Object>();
map.put("Type", Constants_Type.AddType);
mHomeList.add(map);
/*
* 家庭圈列表
*/
Map<String,Object> mapGroup;
mGroupList = LookApp.the().getCore().getmGroupList();
for(AvGroupListItem mGroupListItem : mGroupList)
{
mapGroup = new HashMap<String,Object>();
mapGroup.put("group_id", mGroupListItem.getmStrGroupId());
mapGroup.put("qr_code", mGroupListItem.getmStrQrCode());
mapGroup.put("group_url", mGroupListItem.getmStrGroupUrl());
mapGroup.put("group_photo_path", mGroupListItem.getmStrGroupPhotoPath());
mapGroup.put("group_name", mGroupListItem.getmStrGroupName());
mapGroup.put("Type", Constants_Type.HomeType);
mHomeList.add(mapGroup);
}
mhome_page_adapter = new home_page_adapter(getActivity(), mHomeList);
mHomePageGirdView.setAdapter(mhome_page_adapter);
}
第一次写博客感觉确实不容易,给那些经常写博客的人点赞