实现代码
Adapter的代码
其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局
TYPE_1...是三种类型.
在使用不同布局的时候,getItemViewType和getViewType不能少,一般是不用这两个函数的重载的
- class RunRankAdapter extends BaseAdapter {
- ArrayList<HashMap<String, Object>> ls;
- Context mContext;
- LinearLayout linearLayout = null;
- LayoutInflater inflater;
- TextView tex;
- final int VIEW_TYPE = 3;
- final int TYPE_1 = 0;
- final int TYPE_2 = 1;
- final int TYPE_3 = 2;
- public RunRankAdapter(Context context,
- ArrayList<HashMap<String, Object>> list) {
- ls = list;
- mContext = context;
- }
- @Override
- public int getCount() {
- return ls.size();
- }
- @Override
- public Object getItem(int position) {
- return ls.get(position);
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- // 每个convert view都会调用此方法,获得当前所需要的view样式
- @Override
- public int getItemViewType(int position) {
- int p = position;
- if (p == 0)
- return TYPE_1;
- else if (p == 1)
- return TYPE_2;
- else
- return TYPE_3;
- }
- @Override
- public int getViewTypeCount() {
- return 3;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder1 holder1 = null;
- ViewHolder2 holder2 = null;
- ViewHolder3 holder3 = null;
- int type = getItemViewType(position);
- if (convertView == null) {
- inflater = LayoutInflater.from(mContext);
- // 按当前所需的样式,确定new的布局
- switch (type) {
- case TYPE_1:
- convertView = inflater.inflate(R.layout.item_runrank1,
- parent, false);
- holder1 = new ViewHolder1();
- holder1.rank1 = (TextView) convertView
- .findViewById(R.id.tv_list1_rank);
- holder1.time1 = (TextView) convertView
- .findViewById(R.id.tv_list1_time);
- convertView.setTag(holder1);
- break;
- case TYPE_2:
- convertView = inflater.inflate(R.layout.item_runrank2,
- parent, false);
- holder2 = new ViewHolder2();
- holder2.rank2 = (TextView) convertView
- .findViewById(R.id.tv_list2_rank);
- holder2.time2 = (TextView) convertView
- .findViewById(R.id.tv_list2_time);
- convertView.setTag(holder2);
- break;
- case TYPE_3:
- convertView = inflater.inflate(R.layout.item_runrank3,
- parent, false);
- holder3 = new ViewHolder3();
- holder3.rank3 = (TextView) convertView
- .findViewById(R.id.tv_list3_rank);
- holder3.time3 = (TextView) convertView
- .findViewById(R.id.tv_list3_time);
- convertView.setTag(holder3);
- break;
- default:
- break;
- }
- } else {
- switch (type) {
- case TYPE_1:
- holder1 = (ViewHolder1) convertView.getTag();
- break;
- case TYPE_2:
- holder2 = (ViewHolder2) convertView.getTag();
- break;
- case TYPE_3:
- holder3 = (ViewHolder3) convertView.getTag();
- break;
- }
- }
- // 设置资源
- switch (type) {
- case TYPE_1:
- holder1.rank1.setText("" + (position + 1));
- holder1.time1.setText(ls.get(position).get("time").toString());
- break;
- case TYPE_2:
- holder2.rank2.setText("" + (position + 1));
- holder2.time2.setText(ls.get(position).get("time").toString());
- holder2.totaldistance2.setText(ls.get(position)
- break;
- case TYPE_3:
- holder3.rank3.setText("" + (position + 1));
- holder3.time3.setText(ls.get(position).get("time").toString());
- break;
- }
- return convertView;
- }
- public class ViewHolder1 {
- TextView rank1;
- TextView time1;
- }
- public class ViewHolder2 {
- TextView rank2;
- TextView time2;
- }
- public class ViewHolder3 {
- TextView rank3;
- TextView time3;
- }
- }
使用方式:
声明并定义自己的Adapter,在ListView中设置他的Adapter
- private RunRankAdapter runRankAdapter;
- ArrayList<HashMap<String, Object>> runRank;
- ListView lv_runRank;
- runRank = new ArrayList<HashMap<String, Object>>();
- runRankAdapter = new RunRankAdapter(this, runRank);
- lv_runRank = (ListView) findViewById(R.id.lv_runRank);
- lv_runRank.setAdapter(runRankAdapter);
还有一种方法:
在主布局文件中包含一个LinearLayout. 并且设置android:orientation="vertical"
定义两种不同的布局文件,然后代码中,根据不同条件调用.addview向LinearLayout加载就可以了
在实践中出现的问题:
发现在加载的时候,会出现classcastexception,在网上找到的回答是listview复用出现的问题,http://www.eoeandroid.com/thread-263957-1-1.html
因为在ListView中的convertView会被重用,所以当你根据type去强转convertView.getTag()的时候,你并不知道此时convertView.getTag()返回的是哪一个holder,所以强转自然会出错。对于listView是如何实现复用的,你可以去网上看看,有好例子。
关于你这个问题,我举个例子。
比如你一屏显示了10条记录,显示的holder类型顺序是0,1,2,3,0,1,2,3,0,1,当要显示第11跳记录的时候,因为没有可服用的,此时会重新创建convertView,所以第11个是convertView是null,此时不会出错,当显示第12个的时候,因为第1个(holder0)看不见了,此时系统会去复用它,则当要创建第12个getView时,传入的convertView!=null,即用的是holder0的convertView,但是现在要显示的类型是3,则在convertView.getTag()的时候,就出现错误了。
//有convertView,按样式,取得不用的布局
switch (type) {
case ARTICLE :
try {
holder1 = (ViewHolder1) convertView.getTag();
if (holder1 == null) {
holder1 = new ViewHolder1();
convertView = LayoutInflater.from(discussescontext).inflate(R.layout.my_favourate_detail_item, null);
holder1.wvFavouriteDetails = (WebView)convertView.findViewById(R.id.wv_my_favourite_details);
holder1.tv_subject_my_favourite_details = (TextView)convertView.findViewById(R.id.tv_subject_my_favourite_details);
holder1.tv_more_my_favourite_details1= (TextView)convertView.findViewById(R.id.tv_more_my_favourite_details1);
holder1.tv_more_my_favourite_details2= (TextView)convertView.findViewById(R.id.tv_more_my_favourite_details2);
convertView.setTag(holder1);
}
} catch (Exception e) {
holder1 = new ViewHolder1();
convertView = LayoutInflater.from(discussescontext).inflate(R.layout.my_favourate_detail_item, null);
holder1.wvFavouriteDetails = (WebView)convertView.findViewById(R.id.wv_my_favourite_details);
holder1.tv_subject_my_favourite_details = (TextView)convertView.findViewById(R.id.tv_subject_my_favourite_details);
holder1.tv_more_my_favourite_details1= (TextView)convertView.findViewById(R.id.tv_more_my_favourite_details1);
holder1.tv_more_my_favourite_details2= (TextView)convertView.findViewById(R.id.tv_more_my_favourite_details2);
convertView.setTag(holder1);
}
break;
case COMMENT:
try {
holder2 = (ViewHolder2) convertView.getTag();
if (holder2 == null) {
holder2 = new ViewHolder2();
convertView = LayoutInflater.from(discussescontext).inflate(R.layout.discussespic_item, null);
holder2.discussespic = (ImageView) convertView.findViewById(R.id.iv_discussespic);
holder2.discussestext = (TextView) convertView.findViewById(R.id.tv_discussestext);
holder2.nametext = (TextView) convertView.findViewById(R.id.tv_nametext);
holder2.discussespic = (com.haojiazhang.widget.CircularImage)convertView.findViewById(R.id.iv_discussespic);
convertView.setTag(holder2);
}
} catch (Exception e) {
holder2 = new ViewHolder2();
convertView = LayoutInflater.from(discussescontext).inflate(R.layout.discussespic_item, null);
holder2.discussespic = (ImageView) convertView.findViewById(R.id.iv_discussespic);
holder2.discussestext = (TextView) convertView.findViewById(R.id.tv_discussestext);
holder2.nametext = (TextView) convertView.findViewById(R.id.tv_nametext);
holder2.discussespic = (com.haojiazhang.widget.CircularImage)convertView.findViewById(R.id.iv_discussespic);
convertView.setTag(holder2);
}
break;
default:
break;
}
}